如何实现elasticsearch不过滤?

mal*_*ers 6 elasticsearch

我使用的是弹性搜索版本1.7.5

我从这篇文章中得到了一些想法: Elastic search Not filter inside and filter

下面的查询似乎没有过滤掉与 17 相关的项目。item_category_id您可以看到语法错误吗?not数组外部, 内部定义的术语filters工作正常。

      {
      query: {
          filtered: {
              filter: {
                  and: {
                      filters: [
                          { not: {
                                  filter: {
                                      terms: {item_category_ids: [17] }
                                  }
                              }
                          },
                          { term: { user_inactive: false } },
                          { term: { kind: 1 } }
                      ]
                  }
              }
          }
      }
Run Code Online (Sandbox Code Playgroud)

Val*_*Val 8

ES 2.0 中已弃用filteredand/not查询,您应该bool/filter/must_not像这样使用:

{
  "query": {
    "bool": {
      "filter": [
        {
          "bool": {
            "must_not": {
              "terms": {
                "item_category_ids": [
                  17
                ]
              }
            }
          }
        },
        {
          "term": {
            "user_inactive": false
          }
        },
        {
          "term": {
            "kind": 1
          }
        }
      ]
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

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

{
  "query": {
    "constant_score": {
      "filter": {
        "bool": {
          "must_not": {
            "terms": {
              "item_category_ids": [
                17
              ]
            }
          },
          "must": [
            {
              "term": {
                "user_inactive": false
              }
            },
            {
              "term": {
                "kind": 1
              }
            }
          ]
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)


mal*_*ers 0

这就是我所追求的。

{
      query: {
          filtered: {
              filter: {
                  and: [
                      { terms: { published_posted_community_ids: @community_ids } },
                      { term: { user_inactive: false } },
                      { terms: { kind: 1 } },
                      { not: { terms: { item_category_ids: 17 } } }
                  ]
              }
          }
      }
  }
Run Code Online (Sandbox Code Playgroud)