【问题标题】:fetch limited data from Elastic search indices using match all query python使用匹配所有查询 python 从弹性搜索索引中获取有限数据
【发布时间】:2019-10-21 14:49:19
【问题描述】:

我正在编写一个 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

【问题讨论】:

    标签: python-3.x elasticsearch elastic-stack elasticsearch-5 elasticsearch-plugin


    【解决方案1】:

    这就是scan 方法的作用......它在底层使用scroll 方法,如果你查看api documentationsize 实际上是指batch size

    size – size (per shard) of the batch send at each iteration.
    

    如果你只是想得到一个有大小的结果,search 就足够了,在这种情况下,size 是结果大小,默认值为10

    【讨论】:

    • 我得到的搜索数据限制是9990。如何增加限制。限制只到 9990 的最大值。一次拍摄最多只能获取 9990 个数据。
    • 您是否收到from + size 不能大于10000 的错误?这是为search api 设计的,如果您需要超过 10000 个,那么您应该使用scrollscan。对于您的 9990 的问题,可能是您有 from = 10,因此使用 from=0 和 size=10000 您将获得全部 10000 个结果。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-10
    • 2015-10-03
    • 1970-01-01
    • 1970-01-01
    • 2015-03-20
    相关资源
    最近更新 更多