ruby-on-rails - 如何实现 Elasticsearch 不过滤?

标签 ruby-on-rails elasticsearch

我正在使用 flex 搜索版本1.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 } }
                      ]
                  }
              }
          }
      }

最佳答案

在ES 2.0中, filtered and and/not queries have been deprecated应该改为使用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
              }
            }
          ]
        }
      }
    }
  }
}

关于ruby-on-rails - 如何实现 Elasticsearch 不过滤?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41133067/

相关文章:

ruby-on-rails - 在 Cucumber 场景中使用 Rspec/Mocha stub

ruby-on-rails - Rails CSS 和 JavaScript 修改后的追加和 CSSEdit?

elasticsearch - 使用 Logstash 解析 XML 文件

elasticsearch - Elasticsearch术语聚合和小写值

indexing - Elasticsearch 索引未分析

docker - Elasticsearch保护连接

ruby-on-rails - 无法安装 Debugger gem - Rails - Mac OSX Mavericks

ruby-on-rails - 访问子模型 rails 中的父属性

ruby-on-rails - Has_many 上的 Rails Enum 问题,通过...无法从我的 Controller 调用方法

elasticsearch - 3 个字母词的模糊性