我正在编写一个python程序来从弹性搜索索引中获取数据。我想根据我指出的匹配查询获取数据,最多25个。我想要前25个数据。我索引中的数据是10842。但是它从弹性搜索的索引中检索所有数据。我从这里matchall query for es检查了解决方案,但没有帮助。为我提供一些解决方案
代码如下:
from elasticsearch import Elasticsearch
import elasticsearch.helpers
count = 0
host = 'localhost'
ind = 'apps'
doc_typ = "change_apps"
limit_count = 25
def elasticsearch_import(host,ind,doc_typ,count,limit_count,port=9200,query={},single_line=False,single_line_label="message"):
data_count=count+limit_count
print("Data to be get from Elastic Search: ",data_count)
es = Elasticsearch()
results = elasticsearch.helpers.scan(es,
index=ind,
doc_type=doc_typ,
preserve_order=True,
query={"from":count,"size":data_count,"query": {"bool": {"must": [{"match_all": {}}],"must_not": [],"should": [] }},})
res=[]
for i in results:
res.append(i)
#print("res",res)
print("Data got from Elastic Search",len(res))
elasticsearch_import(host,ind,doc_typ,count,limit_count)
我得到的输出:
Data to be get from Elastic Search: 25
Data got from Elastic Search 10842
必需的输出:
Data to be get from Elastic Search: 25
Data got from Elastic Search 25
答案 0 :(得分:1)
这是scan
方法的作用...它在后台使用了scroll
方法,如果您查看api documentation,则size
实际上是{ {1}}。
batch size
如果您只是想得到一个具有大小的结果,那么search
就足够了,在这种情况下,size – size (per shard) of the batch send at each iteration.
是结果大小,默认值为size
。