在elasticsearch中,如何将must和must_not结合在同一个字段中?

问题描述 投票:0回答:1

我有elasticsearch 6.8.8,只是为了举例说明我的问题。我想创建一个查询,得到带有 "Test "字段的 "1 "的文档,而我不想得到带有 "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个回答
1
投票

你需要删除 attributes.key:Testmust_not 因为它能过滤掉所有 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 只返回匹配的嵌套键值对,而不是整个字段。

© www.soinside.com 2019 - 2024. All rights reserved.