【问题标题】:Elastic search query using bool instead of not使用 bool 而不是 not 的弹性搜索查询
【发布时间】:2018-01-13 00:18:34
【问题描述】:

我正在尝试使用 boolnot 构建弹性搜索查询,因为不推荐使用 not。例如。匹配所有不属于foo@bar.com的文档。

我使用 elasticsearch-dsl 尝试了这些(在这篇文章之后https://www.elastic.co/blog/lost-in-translation-boolean-operations-and-filters-in-the-bool-query):

{'query': {
    'bool': {
        'must_not': [{
            'match': {
                'author': 'foo@bar.com'
            }
        }]
    }
}}

尽管存在 'author': 'foo@bar.com' 以外的文档,但不会返回任何结果。

我也试过

{'query': {
    'bool': {
        'must_not': [{
            'term': {
                'author': 'foo@bar.com'
            }
        }]
    }
}}

这会返回混合文档,其中一些具有'author': 'foo@bar.com',而另一些则没有。

【问题讨论】:

  • 您应该尝试在 must 的同一级别添加 must_not。比如:bool > must,must_not。另一方面,应表示的基本级别是“或”。有了这个或者,可能会出现一些不需要的文档。
  • 您可以尝试为您的搜索创建一个真正的句子。例如,我想获取不是用户自己的文档并且它们是私有的文档。那么,您应该查询什么?完成我们的句子。

标签: elasticsearch elasticsearch-dsl elasticsearch-py


【解决方案1】:

事实证明这是 5.0 中映射更改的问题 (https://www.elastic.co/guide/en/elasticsearch/reference/current/breaking_50_mapping_changes.html)

以下查询按预期工作:

{'query': {
    'bool': {
        'must_not': [{
            'term': {
                'author.keyword': 'foo@bar.com'
            }
        }]
    }
}}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-09
    • 1970-01-01
    相关资源
    最近更新 更多