如何实现ElasticSearch不过滤?

5lhxktic  于 2022-12-03  发布在  ElasticSearch
关注(0)|答案(2)|浏览(140)

我使用的是ElasticSearch1.7.5版
我从这篇文章中得到了一些想法:Elastic search Not filter inside and filter
下面的查询似乎没有筛选出与item_category_id 17关联的项。是否存在语法错误?在not之外、filters数组内部定义的术语工作正常。

{
      query: {
          filtered: {
              filter: {
                  and: {
                      filters: [
                          { not: {
                                  filter: {
                                      terms: {item_category_ids: [17] }
                                  }
                              }
                          },
                          { term: { user_inactive: false } },
                          { term: { kind: 1 } }
                      ]
                  }
              }
          }
      }
insrf1ej

insrf1ej1#

filteredand/not查询在ES 2.0中已被弃用,您应该使用bool/filter/must_not,如下所示:

{
  "query": {
    "bool": {
      "filter": [
        {
          "bool": {
            "must_not": {
              "terms": {
                "item_category_ids": [
                  17
                ]
              }
            }
          }
        },
        {
          "term": {
            "user_inactive": false
          }
        },
        {
          "term": {
            "kind": 1
          }
        }
      ]
    }
  }
}

对于ES 1.7,您需要将查询更改为:

{
  "query": {
    "constant_score": {
      "filter": {
        "bool": {
          "must_not": {
            "terms": {
              "item_category_ids": [
                17
              ]
            }
          },
          "must": [
            {
              "term": {
                "user_inactive": false
              }
            },
            {
              "term": {
                "kind": 1
              }
            }
          ]
        }
      }
    }
  }
}
hl0ma9xz

hl0ma9xz2#

这就是我想要的

{
      query: {
          filtered: {
              filter: {
                  and: [
                      { terms: { published_posted_community_ids: @community_ids } },
                      { term: { user_inactive: false } },
                      { terms: { kind: 1 } },
                      { not: { terms: { item_category_ids: 17 } } }
                  ]
              }
          }
      }
  }

相关问题