Skip to content

Instantly share code, notes, and snippets.

@gizemabali
Last active March 22, 2019 14:15
Show Gist options
  • Save gizemabali/81b9dd4483bcc6c90dcaa844583a4aaa to your computer and use it in GitHub Desktop.
Save gizemabali/81b9dd4483bcc6c90dcaa844583a4aaa to your computer and use it in GitHub Desktop.
extract elasticsearch data in ascending order in Python
from elasticsearch import Elasticsearch
es = Elasticsearch([{'host': "localhost", 'port': 9200}])
query = {
"sort": [
{"timestamp": {"order": "asc"}},
{"_score": {"order": "asc"}}
],
"query": {"bool": {"must": [{"match": {"id": id}},
{"range": {"timestamp": {"gte": start_time, "lte": end_time}}}]}
}
}
samplesCount = 0
res = es.search(index="index_name", doc_type="index_type", body=query, size=1000, scroll='1m')
scrollId = res['_scroll_id']
scrollSize = res['hits']['total']
while scrollSize > 0:
for x in range(0, len(res['hits']['hits'])):
value = res['hits']['hits'][x]["_source"]["value"]
samplesCount += 1
scrollSize -= 1
# codes
res = self.es.scroll(scroll_id=scrollId, scroll='1m')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment