From b27e960e0aeac6dcaf252d2b7980717f29e947bd Mon Sep 17 00:00:00 2001 From: genja Date: Tue, 2 Jun 2026 11:08:01 +0200 Subject: [PATCH] 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: