简体   繁体   English

Elasticsearch:使用python从索引中检索所有文档

[英]Elasticsearch : retrieve all documents from index with python

I need to retrieve documents from Elasticsearch in Python. 我需要从Python中的Elasticsearch检索文档。

So I wrote this small code : 所以我写了这个小代码:

es = Elasticsearch(
            myHost,
            port=myPort,
            scheme="http")

request = '''{"query": {"match_all": {}}}'''

results = es.search(index=myIndex, body=request)['hits']['hits']

print(len(results))
>> 10

The problem is that it only returns 10 documents from my index when I expect to have few hundreds. 问题在于,当我希望有几百个文档时,它仅从索引中返回10个文档。 How is it possible to retrieve all documents from the index ? 如何从索引中检索所有文档?

You have several ways to solve this. 您有几种方法可以解决此问题。

If you know the maximum amount of documents you will have in the index, you can set the size parameter of the search as that number or more. 如果知道索引中将包含的最大文档数量,则可以将搜索的size参数设置为该数量或更大。 For example, if you know you will have less than 100, you can retrieve them this way results = es.search(index=myIndex, body=request, size=100)['hits']['hits'] 例如,如果您知道少于100,则可以通过以下方式检索它们: results = es.search(index=myIndex, body=request, size=100)['hits']['hits']

If you don't know that number, and you still want all of them, you will have to use the scan function, instead of the search function. 如果您不知道该数字,但仍然想要所有这些数字,则必须使用scan功能,而不是search功能。 The documentation for that is here 该文档在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM