在elasticsearch中使用单个查询进行多个搜索

txu3uszq  于 2021-06-14  发布在  ElasticSearch
关注(0)|答案(0)|浏览(190)

我有一个数据集,其中的文档由三个字段标识,比如“name”、“timestamp”和“country”。现在,我使用elasticsearch dsl py,但是我可以阅读本地elasticsearch查询,所以我也可以接受这些作为答案。
下面是我通过三个字段获取单个文档的代码:

def get(name, timestamp, country):
    search = Item.search()
    search = search.filter("term", name=name)
    search = search.filter("term", timestamp=timestamp)
    search = search.filter("term", country=country)
    search = search[:1]
    return search.execute()[0]

这一切都很好,但有时我需要得到200多个项目,调用这个函数意味着200个查询到es。
我要找的是一个查询,它将获取三个字段标识符的列表,并返回与之匹配的所有文档,无论顺序如何。
我尝试过使用ors+ands,但不幸的是性能仍然很差,尽管至少我没有往返服务器200次。

def get_batch(list_of_identifiers):
    search = Item.search()
    batch_query = None
    for ref in list_of_identifiers:
        sub_query = Q("match", name=ref["name"])
        sub_query &= Q("match", timestamp=ref["timestamp"])
        sub_query &= Q("match", country=ref["country"])
        if not batch_query:
            batch_query = sub_query
        else:
            batch_query |= sub_query
    search = search.filter(batch_query)
    return search.scan()

有没有更快/更好的方法来解决这个问题?
在单个查询中使用多搜索是否比使用should/must(或/ands)更快?
编辑:我尝试了多重搜索,几乎没有时间上的差别。我们在谈论秒。对于6个项目,需要60毫秒才能得到结果,对于200个项目,我们讨论的是4-5秒。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题