Merge pull request 'ts-82-author-overview-and-description' (#105) from ts-82-author-overview-and-description into dev
LocationBasedServices/smartguide/pipeline/head This commit looks good

Reviewed-on: #105
This commit was merged in pull request #105.
This commit is contained in:
2026-06-04 10:04:40 +02:00
2 changed files with 68 additions and 23 deletions
@@ -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()
+20 -15
View File
@@ -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"}}],
)