-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Paginated search queries always returns a next token even if it's the last page of the pagination. #242
Comments
It's true. If there are no more items, there shouldn't be a next link. |
Lines 524 to 604 in f797ed4
Issue is here, but I'm not quite sure how to fix this, cause I'm not so familiar with elasticsearch. |
I can't seem to figure out a good way to obtain the "page" number in elasticsearch. We could pass a page number inside the token, but this would cause issues in other places sadly: my example: async def execute_search(
...
) -> Tuple[Iterable[Dict[str, Any]], Optional[int], Optional[str]]:
"""
...
"""
search_after = None
page = 1
if token:
decoded_token = urlsafe_b64decode(token.encode()).decode().split(",")
search_after = decoded_token[:-1]
page = int(decoded_token[-1]) + 1
query = search.query.to_dict() if search.query else None
index_param = indices(collection_ids)
search_task = asyncio.create_task(
self.client.search(
index=index_param,
ignore_unavailable=ignore_unavailable,
query=query,
sort=sort or DEFAULT_SORT,
search_after=search_after,
size=limit,
)
)
try:
es_response = await search_task
except exceptions.NotFoundError:
raise NotFoundError(f"Collections '{collection_ids}' do not exist")
matched = es_response["hits"]["total"]["value"]
hits = es_response["hits"]["hits"]
items = (hit["_source"] for hit in hits)
next_token = None
if matched > page * limit:
if hits and (sort_array := hits[-1].get("sort")):
next_token = urlsafe_b64encode(
",".join([str(x) for x in sort_array] + [str(page)]).encode()
).decode()
return items, matched, next_token Note that this version of this function also makes use of Lines 573 to 579 in f797ed4
Lines 597 to 604 in f797ed4
|
I added another test related to this here: #244 |
) **Related Issue(s):** - #242 **Merge dependencie(s):** - #241 **Description:** - Paginated search queries now don't return a token on the last page. - Made some fixes to the respective tests. In particular `test_pagination_token_idempotent` had and indentation issue - Improved `execute_search` to make use of `es_response["hits"]["total"]["value"]` **PR Checklist:** - [x] Code is formatted and linted (run `pre-commit run --all-files`) - [x] Tests pass (run `make test`) - [x] Documentation has been updated to reflect changes, if applicable - [x] Changes are added to the changelog --------- Co-authored-by: Jonathan Healy <jonathan.d.healy@gmail.com>
Describe the bug
Paginated search queries always returns a next token even if it's the last page of the pagination.
To Reproduce
Steps to reproduce the behavior:
docker-compose up
"limit":1
-> Response will contain an "next link"Expected behavior
I assume the last page of the pagination should not include a "next link"
The text was updated successfully, but these errors were encountered: