Untitled

 avatar
unknown
python
a year ago
1.1 kB
2
Indexable
from elasticsearch import Elasticsearch

es = Elasticsearch()

all_docs = []
search_after = None

# First query
response = es.search(
    index='my_index',
    size=1000,
    body={
        "sort": [
            {"timestamp": "asc"},
            {"_id": "asc"}
        ],
        "query": {
            "bool": {
                "filter": [
                    {"term": {"status": "active"}}
                ]
            }
        }
    }
)

hits = response['hits']['hits']
all_docs.extend(hits)

while hits:
    search_after = hits[-1]['sort']
    response = es.search(
        index='my_index',
        size=1000,
        body={
            "search_after": search_after,
            "sort": [
                {"timestamp": "asc"},
                {"_id": "asc"}
            ],
            "query": {
                "bool": {
                    "filter": [
                        {"term": {"status": "active"}}
                    ]
                }
            }
        }
    )
    hits = response['hits']['hits']
    all_docs.extend(hits)

print(f"Retrieved {len(all_docs)} documents")
Editor is loading...
Leave a Comment