diff --git a/service/src/main/inse/patents/org/elastic_search_client.py b/service/src/main/inse/patents/org/elastic_search_client.py index 157fefe1..1dc73042 100644 --- a/service/src/main/inse/patents/org/elastic_search_client.py +++ b/service/src/main/inse/patents/org/elastic_search_client.py @@ -256,6 +256,8 @@ class OrgPatentsSearchClient: page: int = 0, size: int = DEFAULT_PAGE_SIZE, source: str | None = None, + from_year: int | None = None, + to_year: int | None = None, ) -> tuple[list[OrgPatentEntry], int]: short_id = institution_id.replace("https://openalex.org/", "").upper() filters: list[dict[str, Any]] = [{"term": {"institution_id": short_id}}] @@ -270,6 +272,14 @@ class OrgPatentsSearchClient: if source in ("EPO", "USPTO"): filters.append({"term": {"source": source}}) + year_range: dict[str, int] = {} + if from_year is not None: + year_range["gte"] = from_year + if to_year is not None: + year_range["lte"] = to_year + if year_range: + filters.append({"range": {"publication_year": year_range}}) + payload = { "from": page * size, "size": size, @@ -304,11 +314,27 @@ class OrgPatentsSearchClient: self, institution_id: str, size: int = 10, + from_year: int | None = None, + to_year: int | None = None, ) -> list[dict[str, Any]]: short_id = institution_id.replace("https://openalex.org/", "").upper() + filters: list[dict[str, Any]] = [{"term": {"institution_id": short_id}}] + year_range: dict[str, int] = {} + if from_year is not None: + year_range["gte"] = from_year + if to_year is not None: + year_range["lte"] = to_year + if year_range: + filters.append({"range": {"publication_year": year_range}}) + + query = ( + {"term": {"institution_id": short_id}} + if len(filters) == 1 + else {"bool": {"filter": filters}} + ) payload = { "size": 0, - "query": {"term": {"institution_id": short_id}}, + "query": query, "aggs": { "matched_authors_nested": { "nested": {"path": "matched_authors"}, diff --git a/service/src/main/inse/patents/org/test_elastic_search_client.py b/service/src/main/inse/patents/org/test_elastic_search_client.py index 6ac6ac7c..c5692f67 100644 --- a/service/src/main/inse/patents/org/test_elastic_search_client.py +++ b/service/src/main/inse/patents/org/test_elastic_search_client.py @@ -53,3 +53,43 @@ async def test_top_authors_by_institution_aggregates_matched_authors(): payload["aggs"]["matched_authors_nested"]["nested"]["path"] == "matched_authors" ) + + +@pytest.mark.anyio +async def test_top_authors_by_institution_filters_by_publication_year(): + requests: list[httpx.Request] = [] + + def handler(request: httpx.Request) -> httpx.Response: + requests.append(request) + return httpx.Response( + 200, + json={ + "aggregations": { + "matched_authors_nested": { + "authors": {"buckets": []} + } + } + }, + ) + + client = OrgPatentsSearchClient( + base_url="https://example.test", + api_key="key", + transport=httpx.MockTransport(handler), + ) + + await client.top_authors_by_institution( + "https://openalex.org/I123", + from_year=2020, + to_year=2024, + ) + + payload = json.loads(requests[0].content) + assert payload["query"] == { + "bool": { + "filter": [ + {"term": {"institution_id": "I123"}}, + {"range": {"publication_year": {"gte": 2020, "lte": 2024}}}, + ] + } + }