【问题标题】:How to use 'not' to filter terms in Elastic Search?如何使用“不”来过滤 Elastic Search 中的术语?
【发布时间】:2020-06-28 02:49:20
【问题描述】:

我是 Elastic Search 的新手,找不到有关如何将 notmust_not 用作弹性搜索的术语过滤器的信息。

我的查询设置如下:

    {
        "size":4,
        "from":0,
        "query":{
            "bool":{
                "filter":[
                    {
                        "term":{
                            "published":1
                        }
                    },
                    {
                        "term":{
                            "brand.keyword":"Honda"
                        }
                    },
                    {
                        "not": {
                            "term": {
                                "tags": "Red"
                            }
                        }
                    },
                    {
                        "wildcard":{
                            "image":"*"
                        }
                    }
                ]
            }
        }
    }

但是当我在 Postman 中测试时,我得到了错误:

 "type": "parsing_exception",
 "reason": "no [query] registered for [not]"

有人知道我该如何解决这个问题吗?

【问题讨论】:

标签: elasticsearch


【解决方案1】:

请注意,filter 子句用作must 子句。两者之间的区别只是过滤子句中的任何查询都不会影响文档的分数,或者换句话说,对于 filter 子句,不会计算分数,而对于 mustmust_not 和 @ 987654327@将计算分数。这被称为filter context and query context

现在,如果您不希望 must_not 成为分数计算的一部分,那么查询将是:

{
  "size": 4,
  "from": 0,
  "query": {
    "bool": {
      "filter": [
        {
          "term": {
            "published": 1
          }
        },
        {
          "term": {
            "brand.keyword": "Honda"
          }
        },
        {
          "bool": {
            "must_not": [
              {
                "term": {
                  "tags": "Red"
                }
              }
            ]
          }
        },
        {
          "wildcard": {
            "image": "*"
          }
        }
      ]
    }
  }
}

【讨论】:

  • 谢谢,这消除了错误但不影响结果
  • @MeltingDog 请添加索引映射、示例文档和预期结果以更清晰。您可以将其添加到同一个问题中,也可以为同一个问题写一个单独的问题。
  • 我想我明白了。由于标签是一个数组,我需要使用"tags.keyword": "Red"
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-26
  • 1970-01-01
相关资源
最近更新 更多