From b616d061bb02e3d03c1263296ead4bca8d033f2e Mon Sep 17 00:00:00 2001 From: Kazgatari <56477241+Kazgatari@users.noreply.github.com> Date: Fri, 24 Jul 2026 16:37:32 +0200 Subject: [PATCH] fixed top 20 projects year slider (forgot to add year in query) --- .../test_trend_strategies.py | 14 +++++++ .../inse/university_ranking/trend_logic.py | 39 ++++++++++++------- 2 files changed, 39 insertions(+), 14 deletions(-) diff --git a/service/src/main/inse/university_ranking/test_trend_strategies.py b/service/src/main/inse/university_ranking/test_trend_strategies.py index e79e02fa..e4c063ef 100644 --- a/service/src/main/inse/university_ranking/test_trend_strategies.py +++ b/service/src/main/inse/university_ranking/test_trend_strategies.py @@ -157,6 +157,19 @@ async def test_get_top_topics_for_entity(): assert "items" in res +@pytest.mark.anyio +async def test_get_top_topics_for_entity_projects(): + res = await get_top_topics_for_entity( + entity_id="https://openalex.org/I62318514", + entity_type="institution", + start_year=2018, + end_year=2022, + search_type="projects", + ) + assert isinstance(res, dict) + assert "items" in res + + @pytest.mark.anyio async def test_get_trend_data_delegation(): res = await get_trend_data( @@ -165,3 +178,4 @@ async def test_get_trend_data_delegation(): ) assert res is not None assert len(res.series) > 0 + diff --git a/service/src/main/inse/university_ranking/trend_logic.py b/service/src/main/inse/university_ranking/trend_logic.py index 7015e4de..caf30bc9 100644 --- a/service/src/main/inse/university_ranking/trend_logic.py +++ b/service/src/main/inse/university_ranking/trend_logic.py @@ -296,16 +296,11 @@ async def get_top_topics_for_entity( project_ids_by_index: Dict[str, List[str]] = {} - # 1. BUND - use scan to retrieve all project IDs without 10k limits + # 1. BUND - filter by institution/country and date range if include_german: - bund_filters = [] + bund_filters = [TrendQueryBuilder.build_date_range_filter("funding_start", start_year, end_year)] if entity_type == "institution": - bund_filters.append({ - "bool": { - "should": [{"match": {"Zuwendungsempfänger": {"query": alias, "operator": "and"}}} for alias in aliases], - "minimum_should_match": 1, - } - }) + bund_filters.append(TrendQueryBuilder.build_bund_receiver_clause(aliases)) try: p_ids = [] async for hit in elasticsearch.scan( @@ -321,7 +316,7 @@ async def get_top_topics_for_entity( except Exception as e: logger.warning("Failed to fetch BUND project IDs in get_top_topics_for_entity: %s", e) - # 2. DFG - scan institutes & projects + # 2. DFG - filter by institute projects and date range if include_german: try: dfg_pids = [] @@ -338,7 +333,7 @@ async def get_top_topics_for_entity( dfg_pids.append(str(p["project_id"])) if dfg_pids or entity_type != "institution": - dfg_filters = [] + dfg_filters = [TrendQueryBuilder.build_date_range_filter("funding_start", start_year, end_year)] if dfg_pids: dfg_filters.append({"ids": {"values": dfg_pids}}) @@ -357,7 +352,7 @@ async def get_top_topics_for_entity( except Exception as e: logger.warning("Failed to fetch DFG project IDs in get_top_topics_for_entity: %s", e) - # 3. EU CORDIS + # 3. EU CORDIS - filter orgs and verify project start dates eu_indices = [ (ESIndex.CORDIS_EU_HORIZON_EUROPE_ORGANIZATIONS, ESIndex.CORDIS_EU_HORIZON_EUROPE_PROJECTS, ESIndex.PROJECTS_CORDIS_EU_HEUROPE_TOPICS.value), (ESIndex.CORDIS_EU_HORIZON_2020_ORGANIZATIONS, ESIndex.CORDIS_EU_HORIZON_2020_PROJECTS, ESIndex.PROJECTS_CORDIS_EU_H2020_TOPICS.value), @@ -380,9 +375,25 @@ async def get_top_topics_for_entity( "aggs": {"project_ids": {"terms": {"field": "projectID", "size": 65536}}}, }, ) - eu_pids = [b["key"] for b in eu_org_res.get("aggregations", {}).get("project_ids", {}).get("buckets", [])] - if eu_pids: - project_ids_by_index[topic_idx] = eu_pids + raw_eu_pids = [b["key"] for b in eu_org_res.get("aggregations", {}).get("project_ids", {}).get("buckets", [])] + if raw_eu_pids: + # Filter project IDs by start date range + dated_eu_pids = [] + proj_date_filter = [ + {"terms": {"id": raw_eu_pids}}, + TrendQueryBuilder.build_date_range_filter("startDate", start_year, end_year), + ] + async for hit in elasticsearch.scan( + index=proj_idx.value, + query={"bool": {"filter": proj_date_filter}}, + source=["id"], + cluster=ESCluster.INSE, + ): + pid = hit.get("_source", {}).get("id") or hit.get("_id") + if pid: + dated_eu_pids.append(str(pid)) + if dated_eu_pids: + project_ids_by_index[topic_idx] = dated_eu_pids except Exception as e: logger.warning("Failed to fetch EU project IDs for %s: %s", org_idx.value, e)