Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get all documents under an elasticsearch index with python client ?

I'm trying to get all index document using python client but the result show me only the first document This is my python code :

res = es.search(index="92c603b3-8173-4d7a-9aca-f8c115ff5a18", doc_type="doc", body = {
'size' : 10000,
'query': {
    'match_all' : {}
}
})
print("%d documents found" % res['hits']['total'])
data = [doc for doc in res['hits']['hits']]
for doc in data:
    print(doc)
    return "%s %s %s" % (doc['_id'], doc['_source']['0'], doc['_source']['5'])
like image 864
J.Ghassen Avatar asked Oct 18 '25 04:10

J.Ghassen


2 Answers

try "_doc" instead of "doc"

res = es.search(index="92c603b3-8173-4d7a-9aca-f8c115ff5a18", doc_type="_doc", body = {
'size' : 100,
'query': {
    'match_all' : {}
}
})
like image 159
Habib Mezghani Avatar answered Oct 19 '25 20:10

Habib Mezghani


Elasticsearch by default retrieve only 10 documents. You could change this behaviour - doc here . The best practice for pagination are search after query and scroll query. It depends from your needs. Please read this answer Elastic search not giving data with big number for page size

To show all the results:

for doc in res['hits']['hits']:
    print doc['_id'], doc['_source']
like image 34
Lupanoide Avatar answered Oct 19 '25 19:10

Lupanoide



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!