From b27e960e0aeac6dcaf252d2b7980717f29e947bd Mon Sep 17 00:00:00 2001 From: genja Date: Tue, 2 Jun 2026 11:08:01 +0200 Subject: [PATCH 1/4] elastic client updated --- .../main/inse/integrations/elasticsearch.py | 32 ++++++++++- service/src/main/inse/logic.py | 56 ++++++++++--------- 2 files changed, 59 insertions(+), 29 deletions(-) diff --git a/service/src/main/inse/integrations/elasticsearch.py b/service/src/main/inse/integrations/elasticsearch.py index e3ad986f..00656fb9 100644 --- a/service/src/main/inse/integrations/elasticsearch.py +++ b/service/src/main/inse/integrations/elasticsearch.py @@ -5,15 +5,15 @@ from .. import config def search( index: str, - query_field: str, - query_value: str + query_field: str = None, + query_value: str = None, + body: dict = None, ) -> dict[str, Any]: headers = { "Authorization": f"ApiKey {config.settings.elasticsearch_api_key}", "Content-Type": "application/json" } - payload = { "query": { "query_string": { @@ -22,6 +22,8 @@ def search( } } } + if body != None: + payload = body with httpx.Client() as client: response = client.post( @@ -31,3 +33,27 @@ def search( ) response.raise_for_status() return response.json() +def get( + index: str, + doc_id:str, + body: dict = None, + +) -> dict[str, Any]: + + headers = { + "Authorization": f"ApiKey {config.settings.elasticsearch_api_key}", + "Content-Type": "application/json" + } + payload = body + + # get document directly + with httpx.Client() as client: + response = client.get( + f"{config.settings.elasticsearch_url}/{index}/_doc/{doc_id}", + json=payload, + headers=headers + ) + response.raise_for_status() + return response.json() + + diff --git a/service/src/main/inse/logic.py b/service/src/main/inse/logic.py index 69cfe1ef..5bd6d268 100644 --- a/service/src/main/inse/logic.py +++ b/service/src/main/inse/logic.py @@ -40,10 +40,6 @@ from inse.models.topic_domain import TopicDomain logger = logging.getLogger(__name__) topic_cache_filename: str = "topic_hierarchy_cache.json" -es_client = Elasticsearch( - hosts=[config.settings.elasticsearch_url], - api_key=config.settings.elasticsearch_api_key, -) S = TypeVar('S', bound=schema.Base) M = TypeVar('M', bound=model.BaseModel) @@ -309,13 +305,15 @@ def generate_person_bio(author_id: str, include: Optional[set[str]] = None) -> s def get_person_by_id(author_id: str, include: Optional[str] = None) -> AuthorsListResponse: + body = { + "_source":include.split(",") if include else True, + } try: - hit = es_client.get( + hit = elasticsearch.get( index="openalex-authors", - id=author_id, - source=include.split(",") if include else True, + body=body, + doc_id=author_id ) - except NotFoundError: raise HTTPException( status_code=404, @@ -357,24 +355,27 @@ def search_persons_by_name(author_name: str, limit: Optional[int] = 10, include: } } }, - { - "match": { - "display_name": { - "query": author_name, - "fuzziness": 1 - } - } - } + # { + # "match": { + # "display_name": { + # "query": author_name, + # "fuzziness": 1 + # } + # } + # } ], "minimum_should_match": 1 } } + body = { + "size":limit, + "query":search_query, + "_source":include.split(",") if include else True, + } try: - response = es_client.search( + response = elasticsearch.search( index="openalex-authors", - query=search_query, - source=include.split(",") if include else True, - size=limit + body=body ) except Exception as e: @@ -411,14 +412,17 @@ def search_persons_by_institution( } } } - print (include,institution_name) + body = { + "size":limit, + "query":search_query, + "_source":include.split(",") if include else True, + "sort":[{"works_count": {"order": "desc"}}] + } + print(body) try: - response = es_client.search( + response = elasticsearch.search( index="openalex-authors", - source=include.split(",") if include else True, - query=search_query, - size=limit, - sort=[{"works_count": {"order": "desc"}}] + body=body ) except Exception as e: From c409c1860d42db534b5f3cf4338983d1b2174364 Mon Sep 17 00:00:00 2001 From: Steven Tischer Date: Thu, 4 Jun 2026 09:33:31 +0200 Subject: [PATCH 2/4] fix unsolved merge conflicts --- service/src/main/inse/logic.py | 24 ++---------------------- 1 file changed, 2 insertions(+), 22 deletions(-) diff --git a/service/src/main/inse/logic.py b/service/src/main/inse/logic.py index ce13ef88..5906facb 100644 --- a/service/src/main/inse/logic.py +++ b/service/src/main/inse/logic.py @@ -345,17 +345,11 @@ def generate_person_bio(author_id: str, include: Optional[set[str]] = None) -> s return bio - -<<<<<<< HEAD def get_person_by_id(author_id: str, include: Optional[str] = None) -> AuthorsListResponse: body = { "_source":include.split(",") if include else True, } -======= -def get_person_by_id( - author_id: str, include: Optional[str] = None -) -> AuthorsListResponse: ->>>>>>> dev + try: hit = elasticsearch.get( index="openalex-authors", @@ -389,7 +383,6 @@ def search_persons_by_name( "display_name": {"query": author_name, "boost": 3} } }, -<<<<<<< HEAD # { # "match": { # "display_name": { @@ -398,9 +391,6 @@ def search_persons_by_name( # } # } # } -======= - {"match": {"display_name": {"query": author_name, "fuzziness": 1}}}, ->>>>>>> dev ], "minimum_should_match": 1, } @@ -413,13 +403,10 @@ def search_persons_by_name( try: response = elasticsearch.search( index="openalex-authors", -<<<<<<< HEAD body=body -======= query=search_query, source=include.split(",") if include else True, size=limit, ->>>>>>> dev ) except Exception as e: @@ -451,28 +438,21 @@ def search_persons_by_institution( }, } } -<<<<<<< HEAD body = { "size":limit, "query":search_query, "_source":include.split(",") if include else True, "sort":[{"works_count": {"order": "desc"}}] } - print(body) -======= - print(include, institution_name) ->>>>>>> dev + try: response = elasticsearch.search( index="openalex-authors", -<<<<<<< HEAD body=body -======= source=include.split(",") if include else True, query=search_query, size=limit, sort=[{"works_count": {"order": "desc"}}], ->>>>>>> dev ) except Exception as e: From 547a82336e89d45bba5912f658262a09b690a1e1 Mon Sep 17 00:00:00 2001 From: Steven Tischer Date: Thu, 4 Jun 2026 09:37:18 +0200 Subject: [PATCH 3/4] fix: add missing comma in elasticsearch search body parameter --- service/src/main/inse/logic.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/service/src/main/inse/logic.py b/service/src/main/inse/logic.py index 5906facb..dda0e94f 100644 --- a/service/src/main/inse/logic.py +++ b/service/src/main/inse/logic.py @@ -403,7 +403,7 @@ def search_persons_by_name( try: response = elasticsearch.search( index="openalex-authors", - body=body + body=body, query=search_query, source=include.split(",") if include else True, size=limit, @@ -448,7 +448,7 @@ def search_persons_by_institution( try: response = elasticsearch.search( index="openalex-authors", - body=body + body=body, source=include.split(",") if include else True, query=search_query, size=limit, From e0015c3ea91e749871acf74a2a3c10df992d3181 Mon Sep 17 00:00:00 2001 From: Steven Tischer Date: Thu, 4 Jun 2026 09:55:54 +0200 Subject: [PATCH 4/4] fix(backend/person_search): search failing due to unsupported search parameters --- .../main/inse/integrations/elasticsearch.py | 30 ++++++++++++++----- service/src/main/inse/logic.py | 16 +--------- 2 files changed, 23 insertions(+), 23 deletions(-) diff --git a/service/src/main/inse/integrations/elasticsearch.py b/service/src/main/inse/integrations/elasticsearch.py index 00656fb9..3d744ad9 100644 --- a/service/src/main/inse/integrations/elasticsearch.py +++ b/service/src/main/inse/integrations/elasticsearch.py @@ -7,6 +7,10 @@ def search( index: str, query_field: str = None, query_value: str = None, + query: dict = None, + source = None, + size: int = None, + sort: list = None, body: dict = None, ) -> dict[str, Any]: @@ -14,16 +18,26 @@ def search( "Authorization": f"ApiKey {config.settings.elasticsearch_api_key}", "Content-Type": "application/json" } - payload = { - "query": { - "query_string": { - "default_field": query_field, - "query": query_value + + if body is not None: + payload = body + elif query is not None: + payload = {"query": query} + if source is not None: + payload["_source"] = source + if size is not None: + payload["size"] = size + if sort is not None: + payload["sort"] = sort + else: + payload = { + "query": { + "query_string": { + "default_field": query_field, + "query": query_value + } } } - } - if body != None: - payload = body with httpx.Client() as client: response = client.post( diff --git a/service/src/main/inse/logic.py b/service/src/main/inse/logic.py index dda0e94f..688c81d9 100644 --- a/service/src/main/inse/logic.py +++ b/service/src/main/inse/logic.py @@ -395,15 +395,9 @@ def search_persons_by_name( "minimum_should_match": 1, } } - body = { - "size":limit, - "query":search_query, - "_source":include.split(",") if include else True, - } try: response = elasticsearch.search( index="openalex-authors", - body=body, query=search_query, source=include.split(",") if include else True, size=limit, @@ -438,19 +432,11 @@ def search_persons_by_institution( }, } } - body = { - "size":limit, - "query":search_query, - "_source":include.split(",") if include else True, - "sort":[{"works_count": {"order": "desc"}}] - } - try: response = elasticsearch.search( index="openalex-authors", - body=body, - source=include.split(",") if include else True, query=search_query, + source=include.split(",") if include else True, size=limit, sort=[{"works_count": {"order": "desc"}}], )