Untitled

mail@pastecode.io avatar
unknown
python
9 days ago
1.1 kB
0
Indexable
Never
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")
Leave a Comment