-
Notifications
You must be signed in to change notification settings - Fork 801
how can I usge search_after in elasticsearch-dsl 7.1.0 #1329
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
Comments
s = Index('index_name').search().query().sort('_id').extra( or you can use from_dict s = Search.from_dict({"query": {"match": {"title": "python"}}}) |
The function I wrote below will work. from __future__ import annotations
from typing import Iterator
from elasticsearch_dsl import Search
def search_after(s: Search, *, sort_values: list[str] | None = None, unique_field: str = "_id", size: int = 1000) -> Iterator:
def execute(s: Search) -> Iterator:
es_response = s.execute()
if hits := es_response["hits"]["hits"]:
last_document = hits[-1]
yield from es_response
yield from execute(s.update_from_dict({"search_after": last_document["sort"]}))
if sort_values is None:
assert s.to_dict().get("sort") is not None, "sort_values not found in search, please sort_values parameter or sort your search." # noqa: E501
sort_values = [unique_field] if sort_values is None else sort_values + [unique_field]
sort_values.extend(s.to_dict().get("sort", [])
s = s.sort(*sort_values).update_from_dict({"size": size})
yield from execute(s) |
I didn't do the fancy iterator thing, but I did use Point in Time so the query would be unaffected by refreshes.
|
I would like to open a PR to implement both of these commented suggestions.
|
I opened a PR |
Any of these methods would help to paginate over the 10000 limit? |
I regularly use the code on PR #1623 to page millions of objects. |
Then I am doing something wrong with your code, because an
all |
Can you post your code? It looks like your size might be set too high for the shards to return results. |
You are completelly right, before doing the paging I had the lines
where I was trying to fetch all without paging :) |
how can I usge search_after in elasticsearch-dsl 7.1.0,Can you give me an example!
The text was updated successfully, but these errors were encountered: