【问题标题】:How to combine must and must_not in elasticsearch with same field如何在elasticsearch中将must和must_not与相同的字段结合起来
【发布时间】:2020-09-04 18:53:24
【问题描述】:

我有 elasticsearch 6.8.8,只是我的问题的一个例子。我想创建一个查询,让我获得值为“1”的“Test”字段的文档,并且我不想获得值为“3”的“Test”字段,我知道我可以只写第一个表达式没有 3,它会给我一份价值为“1”的文件。但我想知道,有什么方法可以让我同时在同一个字段上使用 must 和 must_not 并获得“1”的值?

我写了这个基本的例子来了解我的意思:

{
"from": 0,
"query": {
    "nested": {
        "path": "attributes",
        "query": {
            "bool": {
                "should": [
                    {
                        "bool": {
                            "must": [
                                {
                                    "match": {
                                        "attributes.key": {
                                            "query": "Test"
                                        }
                                    }
                                },
                                {
                                    "match": {
                                        "attributes.value": {
                                            "query": "1"
                                        }
                                    }
                                }
                            ],
                            "must_not": [
                                {
                                    "match": {
                                        "attributes.key": {
                                            "query": "Test"
                                        }
                                    }
                                },
                                {
                                    "match": {
                                        "attributes.value": {
                                            "query": "3"
                                        }
                                    }
                                }
                            ]
                        }
                    }
                ]
            }
        }
    }
}

}

我使用属性作为嵌套字段,键值字段使用映射作为字符串类型。

【问题讨论】:

    标签: elasticsearch search kibana


    【解决方案1】:

    您需要在must_not 中省略attributes.key:Test,因为它会过滤掉所有Tests:

    GET combine_flat/_search
    {
      "from": 0,
      "query": {
        "nested": {
          "inner_hits": {}, 
          "path": "attributes",
          "query": {
            "bool": {
              "should": [
                {
                  "bool": {
                    "must": [
                      {
                        "match": {
                          "attributes.key": {
                            "query": "Test"
                          }
                        }
                      },
                      {
                        "match": {
                          "attributes.value": {
                            "query": "1"
                          }
                        }
                      }
                    ],
                    "must_not": [
                      {
                        "match": {
                          "attributes.value": {
                            "query": "3"
                          }
                        }
                      }
                    ]
                  }
                }
              ]
            }
          }
        }
      }
    }
    

    提示:使用inner_hits 只返回匹配的嵌套键值对,而不是整个字段。

    【讨论】:

    • 谢谢。但是我有一个问题,不能只过滤掉“Test”键的“3”值吗?或者它也会过滤“Test1”“Test2”键?
    • 我的意思是如果我将它们添加到查询中。 “Test1”和“Test2”
    • 你已经在must 中指定了Test1,所以它会过滤掉所有其他不匹配的keys。如果你想强制键值对,你必须在你的主要bool-must 中做很多bool-musts
    猜你喜欢
    • 2014-12-23
    • 1970-01-01
    • 1970-01-01
    • 2018-04-06
    • 2020-03-28
    • 2016-02-11
    • 2013-05-11
    • 2023-01-04
    • 1970-01-01
    相关资源
    最近更新 更多