added page offset to person search
This commit is contained in:
@@ -10,6 +10,7 @@ def search(
|
||||
query: dict = None,
|
||||
source = None,
|
||||
size: int = None,
|
||||
offset:int = None,
|
||||
sort: list = None,
|
||||
body: dict = None,
|
||||
) -> dict[str, Any]:
|
||||
@@ -27,6 +28,8 @@ def search(
|
||||
payload["_source"] = source
|
||||
if size is not None:
|
||||
payload["size"] = size
|
||||
if offset is not None:
|
||||
payload["from"] = offset
|
||||
if sort is not None:
|
||||
payload["sort"] = sort
|
||||
else:
|
||||
|
||||
@@ -369,11 +369,10 @@ def get_person_by_id(author_id: str, include: Optional[str] = None) -> AuthorsLi
|
||||
|
||||
|
||||
def search_persons_by_name(
|
||||
author_name: str, limit: Optional[int] = 10, include: Optional[str] = None
|
||||
author_name: str, limit: Optional[int] = 10, page_offset: Optional[int] = 0, include: Optional[str] = None
|
||||
) -> AuthorsListResponse:
|
||||
if not author_name:
|
||||
raise HTTPException(status_code=400, detail="name must be provided")
|
||||
|
||||
search_query = {
|
||||
"bool": {
|
||||
"should": [
|
||||
@@ -399,6 +398,7 @@ def search_persons_by_name(
|
||||
response = elasticsearch.search(
|
||||
index="openalex-authors",
|
||||
query=search_query,
|
||||
offset=page_offset,
|
||||
source=include.split(",") if include else True,
|
||||
size=limit,
|
||||
)
|
||||
@@ -407,12 +407,12 @@ def search_persons_by_name(
|
||||
raise HTTPException(
|
||||
status_code=500, detail=f"Elasticsearch search failed: {str(e)}"
|
||||
)
|
||||
|
||||
total_found = response.get("hits",{}).get("total",{}).get("value",0)
|
||||
hits = response.get("hits", {}).get("hits", [])
|
||||
|
||||
persons = [util.get_persons_data(hit) for hit in hits]
|
||||
|
||||
return AuthorsListResponse(results=persons)
|
||||
return AuthorsListResponse(results=persons,meta=Meta(count=total_found,page=page_offset))
|
||||
|
||||
|
||||
def search_persons_autocomplete(author_name: str, limit: Optional[int] = 5, include: Optional[str] = None) -> AuthorsListResponse:
|
||||
@@ -501,7 +501,7 @@ def search_persons_autocomplete(author_name: str, limit: Optional[int] = 5, incl
|
||||
return AuthorsListResponse(results=persons)
|
||||
|
||||
def search_persons_by_institution(
|
||||
institution_name: str, limit: Optional[int] = 10, include: Optional[str] = None
|
||||
institution_name: str, limit: Optional[int] = 10, page_offset: Optional[int] = 0, include: Optional[str] = None
|
||||
) -> AuthorsListResponse:
|
||||
|
||||
if not institution_name:
|
||||
@@ -523,6 +523,7 @@ def search_persons_by_institution(
|
||||
query=search_query,
|
||||
source=include.split(",") if include else True,
|
||||
size=limit,
|
||||
offset=page_offset,
|
||||
sort=[{"works_count": {"order": "desc"}}],
|
||||
)
|
||||
|
||||
|
||||
@@ -323,8 +323,10 @@ def search_persons_by_name(
|
||||
example="topics_full,topic_share",
|
||||
),
|
||||
],
|
||||
limit:Annotated[Optional[int], Query(description=("Limit of search page results"),example ="2")]= None,
|
||||
page_offset : Annotated[Optional[int], Query(description=("Offset of the result page"),example="2")] = None,
|
||||
):
|
||||
return logic.search_persons_by_name(author_name=author_name, include=include)
|
||||
return logic.search_persons_by_name(author_name=author_name, include=include,page_offset=page_offset,limit=limit)
|
||||
|
||||
@router.get(
|
||||
'/persons/autocomplete',
|
||||
@@ -365,5 +367,7 @@ def search_persons_by_institution(
|
||||
example="topics_full,topic_share",
|
||||
),
|
||||
],
|
||||
limit:Annotated[Optional[int], Query(description=("Limit of search page results"),example ="2")]= None,
|
||||
page_offset : Annotated[Optional[int], Query(description=("Offset of the result page"),example="2")] = None,
|
||||
):
|
||||
return logic.search_persons_by_institution(institution_name, include=include)
|
||||
return logic.search_persons_by_institution(institution_name, include=include,page_offset=page_offset,limit=limit)
|
||||
|
||||
Reference in New Issue
Block a user