diff --git a/service/src/main/inse/logic.py b/service/src/main/inse/logic.py index 88c95c16..ab8ced5f 100644 --- a/service/src/main/inse/logic.py +++ b/service/src/main/inse/logic.py @@ -255,6 +255,62 @@ def count_topic_hierarchy(topic_names: List[str]) -> dict: ### persons +def list_persons( + page_offset: int = 0, + page_size: int = 20, + query: Optional[str] = None, + include: Optional[set[str]] = None, +) -> AuthorsListResponse: + payload = _author_list_query(page_offset, page_size, query) + data = elasticsearch.search(index="openalex-authors", payload=payload) + hits = data.get("hits", {}).get("hits", []) + persons = [util.get_persons_data(hit) for hit in hits] + total = _extract_total_hits(data) + return AuthorsListResponse( + results=persons, + meta=Meta( + count=total, + page=page_offset // page_size + 1 if page_size > 0 else 1, + per_page=page_size, + ), + ) + + +def _author_list_query( + page_offset: int, page_size: int, query: Optional[str] = None +) -> dict: + search_query = {"match_all": {}} + if query: + search_query = { + "query_string": { + "fields": [ + "display_name^3", + "display_name_alternatives", + "_id", + "ids.orcid", + ], + "query": query, + } + } + + return { + "from": page_offset, + "size": page_size, + "query": search_query, + "sort": [ + {"works_count": {"order": "desc", "missing": "_last"}}, + {"cited_by_count": {"order": "desc", "missing": "_last"}}, + ], + } + + +def _extract_total_hits(data: dict) -> int: + total = data.get("hits", {}).get("total", 0) + if isinstance(total, dict): + return total.get("value", 0) + return total + + def search_persons( author_id: Optional[str] = None, query: Optional[str] = None, diff --git a/service/src/main/inse/model.py b/service/src/main/inse/model.py index f8c145a3..5222e81a 100644 --- a/service/src/main/inse/model.py +++ b/service/src/main/inse/model.py @@ -38,7 +38,3 @@ class ItemPage(Page[Item]): class PersonBio(BaseModel): bio: str = Field(description="The generated biography.") - - -class PersonPage(Page[Person]): - pass diff --git a/service/src/main/inse/service.py b/service/src/main/inse/service.py index 76f59946..a9fab67e 100644 --- a/service/src/main/inse/service.py +++ b/service/src/main/inse/service.py @@ -215,21 +215,23 @@ def count_topic_hierarchy( ### persons - @router.get( - "/persons/search", - operation_id="searchPersons", + "/persons", + operation_id="getPersons", response_model=AuthorsListResponse, - summary="Searches for persons by ID or name.", + summary="Returns a page of OpenAlex authors.", response_model_exclude_none=True, tags=["Persons"], ) -def search_persons( - author_id: Annotated[ - str, Query(description="OpenAlex author ID (e.g., A5093075883)") - ] = None, +def get_persons( + page_offset: Annotated[ + int, Query(alias="pageOffset", description="The page offset.", ge=0) + ] = 0, + page_size: Annotated[ + int, Query(alias="pageSize", description="The page size.", ge=1, le=100) + ] = 20, query: Annotated[ - str, Query(description="Search query (e.g., Arman AND Arzani)") + Optional[str], Query(description="Optional author search query.") ] = None, include: Annotated[ Optional[str], @@ -248,7 +250,9 @@ def search_persons( if include else None ) - return logic.search_persons(author_id, query, include_set) + return logic.list_persons( + page_offset=page_offset, page_size=page_size, query=query, include=include_set + ) @router.get( diff --git a/service/src/main/inse/test_persons.py b/service/src/main/inse/test_persons.py index 56a26c6e..949b2432 100644 --- a/service/src/main/inse/test_persons.py +++ b/service/src/main/inse/test_persons.py @@ -1,8 +1,11 @@ from fastapi.testclient import TestClient - from inse import logic from inse import model from inse.main import app +from inse.models.authors_list_response import AuthorsListResponse +from inse.models.meta import Meta +from inse.models.author import Author +from inse.models.author_ids import AuthorIds client = TestClient(app) @@ -46,11 +49,11 @@ def test_list_persons_searches_openalex_authors(monkeypatch): assert captured["payload"]["from"] == 20 assert captured["payload"]["size"] == 10 assert captured["payload"]["query"]["query_string"]["query"] == "Ada" - assert page.total == 1 - assert page.elements[0].id == "A5023888391" - assert page.elements[0].name == "Ada Lovelace" - assert page.elements[0].topics == ["Computer Science"] - assert page.elements[0].affiliations[0].institution == "University of Example" + assert page.meta.count == 1 + assert page.results[0].id == "A5023888391" + assert page.results[0].display_name == "Ada Lovelace" + assert page.results[0].topics[0].display_name == "Computer Science" + assert page.results[0].affiliations[0].institution.display_name == "University of Example" def test_list_persons_uses_match_all_without_query(monkeypatch): @@ -65,8 +68,8 @@ def test_list_persons_uses_match_all_without_query(monkeypatch): page = logic.list_persons(page_offset=0, page_size=20) assert captured["payload"]["query"] == {"match_all": {}} - assert page.elements == [] - assert page.total == 0 + assert page.results == [] + assert page.meta.count == 0 def test_get_persons_endpoint_returns_page(monkeypatch): @@ -75,31 +78,31 @@ def test_get_persons_endpoint_returns_page(monkeypatch): assert page_size == 10 assert query == "Ada" assert include == {"ids"} - return model.PersonPage( - offset=5, - size=10, - total=1, - elements=[ - model.Person( + return AuthorsListResponse( + meta=Meta( + count=1, + page=1, + per_page=10 + ), + results=[ + Author( id="A5023888391", - name="Ada Lovelace", + display_name="Ada Lovelace", works_count=42, cited_by_count=1234, affiliations=[], topics=[], - ids={"openalex": "https://openalex.org/A5023888391"}, + ids=AuthorIds(openalex="https://openalex.org/A5023888391"), ) ], ) monkeypatch.setattr(logic, "list_persons", fake_list_persons) - response = client.get("/inse-service/persons?pageOffset=5&pageSize=10&query=Ada&include=ids") + response = client.get("/api/inse-service/persons?pageOffset=5&pageSize=10&query=Ada&include=ids") assert response.status_code == 200 data = response.json() - assert data["offset"] == 5 - assert data["size"] == 10 - assert data["total"] == 1 - assert data["elements"][0]["name"] == "Ada Lovelace" - assert data["elements"][0]["ids"]["openalex"] == "https://openalex.org/A5023888391" + assert data["meta"]["count"] == 1 + assert data["results"][0]["display_name"] == "Ada Lovelace" + assert data["results"][0]["ids"]["openalex"] == "https://openalex.org/A5023888391" \ No newline at end of file