diff --git a/service/src/main/inse/integrations/elasticsearch.py b/service/src/main/inse/integrations/elasticsearch.py index e3ad986f..3d744ad9 100644 --- a/service/src/main/inse/integrations/elasticsearch.py +++ b/service/src/main/inse/integrations/elasticsearch.py @@ -5,8 +5,13 @@ from .. import config def search( index: str, - query_field: str, - query_value: 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]: headers = { @@ -14,14 +19,25 @@ def search( "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 + } } } - } with httpx.Client() as client: response = client.post( @@ -31,3 +47,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 88c95c16..688c81d9 100644 --- a/service/src/main/inse/logic.py +++ b/service/src/main/inse/logic.py @@ -55,9 +55,8 @@ es_client = _init_es_client() topic_cache_filename: str = "topic_hierarchy_cache.json" -S = TypeVar("S", bound=schema.Base) -M = TypeVar("M", bound=model.BaseModel) - +S = TypeVar('S', bound=schema.Base) +M = TypeVar('M', bound=model.BaseModel) def filter_none(): return True @@ -346,17 +345,17 @@ def generate_person_bio(author_id: str, include: Optional[set[str]] = None) -> s return bio +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: 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, detail=f"Person with id {author_id} not found" @@ -384,13 +383,20 @@ def search_persons_by_name( "display_name": {"query": author_name, "boost": 3} } }, - {"match": {"display_name": {"query": author_name, "fuzziness": 1}}}, + # { + # "match": { + # "display_name": { + # "query": author_name, + # "fuzziness": 1 + # } + # } + # } ], "minimum_should_match": 1, } } try: - response = es_client.search( + response = elasticsearch.search( index="openalex-authors", query=search_query, source=include.split(",") if include else True, @@ -426,12 +432,11 @@ def search_persons_by_institution( }, } } - print(include, institution_name) try: - response = es_client.search( + response = elasticsearch.search( index="openalex-authors", - source=include.split(",") if include else True, query=search_query, + source=include.split(",") if include else True, size=limit, sort=[{"works_count": {"order": "desc"}}], )