elastic client updated
LocationBasedServices/smartguide/pipeline/head This commit looks good

This commit is contained in:
2026-06-02 11:08:01 +02:00
parent 2eb97bc4f7
commit b27e960e0a
2 changed files with 59 additions and 29 deletions
@@ -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()
+30 -26
View File
@@ -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: