【问题标题】:elasticsearch-dsl library in python giving double results when using search.from_dict() method to construct query from dictionary syntaxpython 中的 elasticsearch-dsl 库在使用 search.from_dict() 方法从字典语法构造查询时给出双重结果
【发布时间】:2019-09-23 13:49:03
【问题描述】:

请在下面找到我的 iPython shell 语句:

In [90]: s = Search(using=client, index='institutes')

In [91]: s = s.filter('match_phrase',country__uri='canada').filter("nested", path="institutecourse_set", query=Q("match_phrase", **{'institutecourse_set.stream.uri': 'sciences'}))

In [92]: di = s.to_dict()

In [93]: s1 =  Search(using=client, index='institutes')

In [94]: s1 = s1.from_dict(di)

In [95]: s1.count()

Out[95]: 84

In [96]: s.count()

Out[96]: 42

我的基本要求是在 django 项目中使用高级库 elasticsearch-dsl-py 基于多个输入和过滤条件构建查询。

为了易于解释和易于构建代码,我将查询组成为字典(json)样式,就像基本的 elasticsearch 查询一样,然后我利用 elasticsearch_dsl 库中的“from_dict(dictionary_object)”方法并调用 count()并为我的目的在结果上执行()。

上面显示的是一个较小的示例,但问题很明显。当我使用高级语法时,使用 from_dict 得到双倍的结果(上面的答案 42 是正确的),谁能解释为什么会这样?

【问题讨论】:

    标签: python elasticsearch elasticsearch-dsl-py


    【解决方案1】:

    我在 Honza 的帮助下找到了答案。 “from_dict”方法是一个类方法,它重置了搜索对象,因此它没有任何索引可供搜索,并且可能会给出比预期更多的结果,因为它会在所有可能的索引中执行搜索。

    相反,我们只需要再次提及索引,例如: s.from_dict(dict_qry),使用(client).index() 客户在哪里, 从弹性搜索导入弹性搜索 客户端 = Elasticsearch()

    阅读它here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-07
      • 1970-01-01
      • 1970-01-01
      • 2019-05-17
      • 2021-05-21
      • 1970-01-01
      • 2021-01-22
      • 2023-03-04
      相关资源
      最近更新 更多