From efa43910c1467fecafc207b6f2cc816623ef0058 Mon Sep 17 00:00:00 2001 From: Kazgatari <56477241+Kazgatari@users.noreply.github.com> Date: Fri, 17 Jul 2026 21:51:51 +0200 Subject: [PATCH] changed trend to query directly for institution first --- .../university_ranking/trend_strategies.py | 184 +++++++++--------- 1 file changed, 92 insertions(+), 92 deletions(-) diff --git a/service/src/main/inse/university_ranking/trend_strategies.py b/service/src/main/inse/university_ranking/trend_strategies.py index 1698df9f..e8474b02 100644 --- a/service/src/main/inse/university_ranking/trend_strategies.py +++ b/service/src/main/inse/university_ranking/trend_strategies.py @@ -868,14 +868,64 @@ class ProjectsTrendStrategy(TrendStrategy): from inse.patents.search_client import PatentSearchClient patent_client = PatentSearchClient() + # Resolve DFG project IDs for institutions using dfg_institutes_2.0 root_name_en matches + dfg_institution_projects = {} + async def resolve_dfg_projects_for_inst(inst_id): + aliases = resolved_aliases.get(inst_id, []) + if not aliases: + return inst_id, [] + + should_clauses = [] + for alias in aliases: + should_clauses.append({ + "match": { + "root_name_en": { + "query": alias, + "operator": "and" + } + } + }) + try: + res = await patent_client._search( + index=ESIndex.DFG_INSTITUTES.value, + payload={ + "query": { + "bool": { + "should": should_clauses, + "minimum_should_match": 1 + } + }, + "size": 1000 + } + ) + hits = res.get("hits", {}).get("hits", []) + p_ids = [] + for hit in hits: + source = hit.get("_source", {}) + for p in source.get("projects", []): + pid = p.get("project_id") + if pid: + p_ids.append(str(pid)) + return inst_id, list(set(p_ids)) + except Exception as e: + logger.warning("Failed to query dfg_institutes_2.0 for %s: %s", inst_id, e) + return inst_id, [] + + if query_institutions: + dfg_resolve_results = await asyncio.gather(*( + resolve_dfg_projects_for_inst(inst_id) for inst_id in query_institutions + )) + for inst_id, p_ids in dfg_resolve_results: + dfg_institution_projects[inst_id] = p_ids + # 2. Fetch matched project IDs for each topic (if fuzzy is True) topic_project_ids = {} indices_mapping = { - "projects_cordiseu_heurope_openalex_topics": (ESIndex.CORDIS_EU_HORIZON_EUROPE_ORGANIZATIONS, ESIndex.CORDIS_EU_HORIZON_EUROPE_PROJECTS), - "projects_cordiseu_h2020_openalex_topics": (ESIndex.CORDIS_EU_HORIZON_2020_ORGANIZATIONS, ESIndex.CORDIS_EU_HORIZON_2020_PROJECTS), - "projects_cordiseu_fp7_openalex_topics": (ESIndex.CORDIS_EU_FP7_ORGANIZATIONS, ESIndex.CORDIS_EU_FP7_PROJECTS), - "projects_bund_openalex_topics": (ESIndex.PROJECTS_BUND_V1, None), - "projects_dfg_openalex_topics": (ESIndex.PROJECTS_DFG_ALL, None) + ESIndex.PROJECTS_CORDIS_EU_HEUROPE_TOPICS.value: (ESIndex.CORDIS_EU_HORIZON_EUROPE_ORGANIZATIONS, ESIndex.CORDIS_EU_HORIZON_EUROPE_PROJECTS), + ESIndex.PROJECTS_CORDIS_EU_H2020_TOPICS.value: (ESIndex.CORDIS_EU_HORIZON_2020_ORGANIZATIONS, ESIndex.CORDIS_EU_HORIZON_2020_PROJECTS), + ESIndex.PROJECTS_CORDIS_EU_FP7_TOPICS_MAPPING.value: (ESIndex.CORDIS_EU_FP7_ORGANIZATIONS, ESIndex.CORDIS_EU_FP7_PROJECTS), + ESIndex.PROJECTS_BUND_TOPICS.value: (ESIndex.PROJECTS_BUND_V1, None), + ESIndex.PROJECTS_DFG_TOPICS.value: (ESIndex.DFG_PROJECTS, None) } if fuzzy: @@ -1051,100 +1101,50 @@ class ProjectsTrendStrategy(TrendStrategy): if include_dfg: dfg_filters = [] + dfg_inst_pids = None if entity_type == "institution": - aliases = resolved_aliases.get(entity_val, []) - dfg_entity_shoulds = [] - for field in DFG_INSTITUTION_FIELDS: - dfg_entity_shoulds.append({ + dfg_inst_pids = dfg_institution_projects.get(entity_val, []) + if not dfg_inst_pids: + include_dfg = False + + if include_dfg: + if fuzzy: + p_ids = topic_project_ids[topic].get(ESIndex.PROJECTS_DFG_TOPICS.value, []) + if dfg_inst_pids is not None: + common_ids = list(set(p_ids) & set(dfg_inst_pids)) + if common_ids: + dfg_filters.append({"ids": {"values": common_ids}}) + else: + include_dfg = False + else: + if p_ids: + dfg_filters.append({"ids": {"values": p_ids}}) + else: + include_dfg = False + else: + if dfg_inst_pids is not None: + dfg_filters.append({"ids": {"values": dfg_inst_pids}}) + + dfg_filters.append({ "bool": { "should": [ - { - "match": { - field: { - "query": alias, - "operator": "and" - } - } - } - for alias in aliases + {"match": {"title_de": keyword}}, + {"match": {"title_en": keyword}}, + {"match": {"description": keyword}}, + {"match": {"subject_area_de": keyword}}, + {"match": {"subject_area_en": keyword}}, + {"match": {"abstract_de": keyword}}, + {"match": {"abstract_en": keyword}}, + {"match": {"final_report_abstract_de": keyword}}, + {"match": {"final_report_abstract_en": keyword}} ], "minimum_should_match": 1 } }) - dfg_entity_shoulds.append({ - "nested": { - "path": "persons", - "query": { - "bool": { - "should": [ - { - "match": { - "persons.affiliation_de": { - "query": alias, - "operator": "and" - } - } - } - for alias in aliases - ] + [ - { - "match": { - "persons.affiliation_en": { - "query": alias, - "operator": "and" - } - } - } - for alias in aliases - ], - "minimum_should_match": 1 - } - } - } - }) - dfg_filters.append({ - "bool": { - "should": dfg_entity_shoulds, - "minimum_should_match": 1 - } - }) - - if fuzzy: - p_ids = topic_project_ids[topic].get("projects_dfg_openalex_topics", []) - if p_ids: - dfg_filters.append({"ids": {"values": p_ids}}) - dfg_query = {"query": {"bool": {"filter": dfg_filters}}} - else: - dfg_query = None - else: - dfg_query = { - "query": { - "bool": { - "filter": [ - { - "bool": { - "should": [ - {"match": {"title_de": keyword}}, - {"match": {"title_en": keyword}}, - {"match": {"description": keyword}}, - {"match": {"subject_area_de": keyword}}, - {"match": {"subject_area_en": keyword}}, - {"match": {"abstract_de": keyword}}, - {"match": {"abstract_en": keyword}}, - {"match": {"final_report_abstract_de": keyword}}, - {"match": {"final_report_abstract_en": keyword}} - ], - "minimum_should_match": 1 - } - } - ] + dfg_filters - } - } - } - - if dfg_query: + + if include_dfg: dfg_payload = { - **dfg_query, + "query": {"bool": {"filter": dfg_filters}}, "size": 0, "aggs": { "projects_by_year": { @@ -1157,7 +1157,7 @@ class ProjectsTrendStrategy(TrendStrategy): } async def process_dfg(): try: - res = await patent_client._search(index=ESIndex.PROJECTS_DFG_ALL.value, payload=dfg_payload) + res = await patent_client._search(index=ESIndex.DFG_PROJECTS.value, payload=dfg_payload) return "terms", res.get("aggregations", {}).get("projects_by_year", {}).get("buckets", []) except Exception as e: logger.warning("Failed to query DFG projects: %s", e)