diff --git a/service/build.gradle b/service/build.gradle index f05e4027..b506a19c 100644 --- a/service/build.gradle +++ b/service/build.gradle @@ -31,7 +31,7 @@ task exportOpenApiSpec(type: Exec) { '--entrypoint', 'python', "${imagePrefix}-service:${imageVersion}", '-c', - 'import json; from inse.service import app; open("/mnt/openapi/inse-service.openapi.json", "w").write(json.dumps(app.openapi(), indent=2))' + "import json; from inse.service import app; open('/mnt/openapi/inse-service.openapi.json', 'w').write(json.dumps(app.openapi(), indent=2))" } task runServeContainer(type: Exec) { diff --git a/service/src/main/inse/logic.py b/service/src/main/inse/logic.py index 691dfb0e..b7aaad0f 100644 --- a/service/src/main/inse/logic.py +++ b/service/src/main/inse/logic.py @@ -198,7 +198,8 @@ def count_topic_hierarchy(topic_names: List[str]) -> dict: ### persons -def search_persons(author_id: Optional[str] = None, query: Optional[str] = None) -> model.PersonSearchResult: +def search_persons(author_id: Optional[str] = None, query: Optional[str] = None, + include: Optional[set[str]] = None) -> model.PersonSearchResult: headers = { "Authorization": f"ApiKey {config.settings.elasticsearch_api_key}", "Content-Type": "application/json" @@ -227,6 +228,7 @@ def search_persons(author_id: Optional[str] = None, query: Optional[str] = None) response.raise_for_status() data = response.json() + include_set = {tag.strip().lower() for tag in include} if include else set() persons = [] for hit in data.get("hits", {}).get("hits", []): source = hit.get("_source", {}) @@ -241,14 +243,41 @@ def search_persons(author_id: Optional[str] = None, query: Optional[str] = None) 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 - )) + person_data = { + "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 + } + + # Include optional fields based on the include_set + if "topics_full" in include_set: + person_data["topics_full"] = source.get("topics", []) + if "topic_share" in include_set: + person_data["topic_share"] = source.get("topic_share", []) + if "summary_stats" in include_set: + person_data["summary_stats"] = source.get("summary_stats") + if "ids" in include_set: + person_data["ids"] = source.get("ids") + if "last_known_institutions" in include_set: + person_data["last_known_institutions"] = source.get("last_known_institutions", []) + if "sources" in include_set: + person_data["sources"] = source.get("sources", []) + if "counts_by_year" in include_set: + person_data["counts_by_year"] = source.get("counts_by_year", []) + if "works_api_url" in include_set: + person_data["works_api_url"] = source.get("works_api_url") + if "dates" in include_set: + person_data["dates"] = { + "created_date": source.get("created_date"), + "updated_date": source.get("updated_date") + } + if "cited_by_count_boost" in include_set: + person_data["cited_by_count_boost"] = source.get("cited_by_count_boost") + + persons.append(model.Person(**person_data)) return model.PersonSearchResult(persons=persons) diff --git a/service/src/main/inse/model.py b/service/src/main/inse/model.py index 0d0b41ea..b9ddf38b 100644 --- a/service/src/main/inse/model.py +++ b/service/src/main/inse/model.py @@ -57,6 +57,17 @@ class Person(BaseModel): cited_by_count: int = Field(description="Number of citations.") affiliations: List[Affiliation] = Field(default=[], description="Affiliations.") topics: List[str] = Field(default=[], description="Research topics.") + # Optional detailed information + topics_full: Optional[List[dict]] = Field(default=None, description="Detailed topics with hierarchy.") + topic_share: Optional[List[dict]] = Field(default=None, description="Topic share distribution.") + summary_stats: Optional[dict] = Field(default=None, description="Summary statistics.") + ids: Optional[dict] = Field(default=None, description="External identifiers.") + last_known_institutions: Optional[List[dict]] = Field(default=None, description="Last known institutions.") + sources: Optional[List[dict]] = Field(default=None, description="Sources list.") + counts_by_year: Optional[List[dict]] = Field(default=None, description="Counts by year.") + works_api_url: Optional[str] = Field(default=None, description="Works API URL.") + dates: Optional[dict] = Field(default=None, description="Created and updated dates.") + cited_by_count_boost: Optional[int] = Field(default=None, description="Cited by count boost.") class PersonSearchResult(BaseModel): persons: List[Person] = Field(default=[], description="List of found persons.") diff --git a/service/src/main/inse/service.py b/service/src/main/inse/service.py index ef4c7af8..22a16a19 100644 --- a/service/src/main/inse/service.py +++ b/service/src/main/inse/service.py @@ -5,7 +5,7 @@ service. """ import logging -from typing import Annotated, List +from typing import Annotated, List, Optional from fastapi import FastAPI from fastapi import Depends from fastapi import Path @@ -156,10 +156,20 @@ def count_topic_hierarchy( operation_id='searchPersons', response_model=model.PersonSearchResult, summary='Searches for persons by ID or name.', + response_model_exclude_none=True, tags=[ 'Persons' ]) def search_persons(author_id: Annotated[str, Query(description="OpenAlex author ID (e.g., A5093075883)")] = None, - query: Annotated[str, Query(description="Search query (e.g., Arman AND Arzani)")] = None): - return logic.search_persons(author_id, query) + query: Annotated[str, Query(description="Search query (e.g., Arman AND Arzani)")] = None, + include: Annotated[Optional[str], Query( + description=( + "Comma-separated include tags: topics_full, topic_share, summary_stats, ids, " + "last_known_institutions, sources, counts_by_year, works_api_url, dates, " + "cited_by_count_boost" + ), + example="topics_full,topic_share" + )] = None): + include_set = {tag.strip().lower() for tag in include.split(",") if tag.strip()} if include else None + return logic.search_persons(author_id, query, include_set) @app.get('/persons/bio', operation_id='generatePersonBio',