Add Elasticsearch integration for person search functionality

This commit is contained in:
Steven Tischer
2026-05-26 20:38:50 +02:00
parent e1b464d418
commit f625cb02d6
4 changed files with 69 additions and 0 deletions
+2
View File
@@ -22,6 +22,8 @@ class Settings(BaseSettings):
llm_api_url: str = 'https://project.nes.uni-due.de/llm2/api/generate'
llm_api_token: str = 'c2d5e7ccf7a4760dd3d19bed2caed9d85ff7437400e0ea82a6d6f912ebc2f212'
llm_model: str = 'gemma4:e4b'
elasticsearch_url: str = 'https://project.nes.uni-due.de/elastic/openalex-authors/_search'
elasticsearch_api_key: str = 'am1wWXBaMEJEVWJoZkE5MnBaUlk6TWlvbW1LRlMzNjdDUHgzYmVkUmI5UQ=='
settings = Settings()
logger.info(f"Database URL: {settings.database_url}.")
+44
View File
@@ -58,6 +58,50 @@ def insert_element(db: Session, value: S) -> S:
db.refresh(value)
return value
def search_persons() -> model.PersonSearchResult:
headers = {
"Authorization": f"ApiKey {config.settings.elasticsearch_api_key}",
"Content-Type": "application/json"
}
payload = {
"query": {
"query_string": {
"default_field": "display_name",
"query": "Arman AND Arzani"
}
}
}
with httpx.Client() as client:
response = client.post(config.settings.elasticsearch_url, json=payload, headers=headers)
response.raise_for_status()
data = response.json()
persons = []
for hit in data.get("hits", {}).get("hits", []):
source = hit.get("_source", {})
affiliations = []
for aff in source.get("affiliations", []):
institution = aff.get("institution", {})
affiliations.append(model.Affiliation(
institution=institution.get("display_name", ""),
country_code=institution.get("country_code", "")
))
topics = [t.get("display_name", "") for t in source.get("topics", [])]
persons.append(model.Person(
id=hit.get("_id", ""),
name=source.get("display_name", ""),
works_count=source.get("works_count", 0),
cited_by_count=source.get("cited_by_count", 0),
affiliations=affiliations,
topics=topics
))
return model.PersonSearchResult(persons=persons)
def generate_person_bio() -> str:
headers = {
"Authorization": f"Bearer {config.settings.llm_api_token}",
+15
View File
@@ -37,3 +37,18 @@ class ItemPage(Page[Item]):
class PersonBio(BaseModel):
bio: str = Field(description="The generated biography.")
class Affiliation(BaseModel):
institution: str = Field(description="Institution name.")
country_code: str = Field(description="Country code.")
class Person(BaseModel):
id: str = Field(description="The person id.")
name: str = Field(description="The person name.")
works_count: int = Field(description="Number of works.")
cited_by_count: int = Field(description="Number of citations.")
affiliations: List[Affiliation] = Field(default=[], description="Affiliations.")
topics: List[str] = Field(default=[], description="Research topics.")
class PersonSearchResult(BaseModel):
persons: List[Person] = Field(default=[], description="List of found persons.")
+8
View File
@@ -113,6 +113,14 @@ def delete_item(id: Annotated[int, Path(description='The item id.', ge=0)],
finally:
db.commit()
@app.get('/persons/search',
operation_id='searchPersons',
response_model=model.PersonSearchResult,
summary='Searches for persons.',
tags=[ 'Persons' ])
def search_persons():
return logic.search_persons()
@app.get('/persons/bio',
operation_id='generatePersonBio',
response_model=model.PersonBio,