【问题标题】:Elasticsearch 6.3.2 terms match empty array "plus" othersElasticsearch 6.3.2 术语匹配空数组“加”其他
【发布时间】:2019-09-26 20:17:52
【问题描述】:

在我的数据库中,一篇文章可以有零 (0) 个或多个以数组形式表示的类别。

当我进行查询时,查看这些类别,传递一些值:

{
  "query": {
    "bool": {
      "should": {
        "terms": {
          "categories": ["First", "Second", "And so on"]
        }
      }
    }
  }
}

而且效果很好,我有我期待的记录。但是当我想包含这些帖子时,问题就来了,其中类别是一个空数组 ([])。

我现在从旧版 ES (1.4.5) 升级到 6.3.2 版,这段代码是使用“missing”制作的,已被弃用。

我尝试更改添加着名的"null_value": "NULL" 的映射,然后查询,但没有奏效。 还尝试了 should 与 must_not 的组合,如升级“missing”的建议,但没有奏效。

我怎样才能做到这一点?这意味着如果我已编入索引:

Post.new(id: 1, title: '1st', categories: [])
Post.new(id: 2, title: '2nd', categories: ['news', 'tv'])
Post.new(id: 3, title: '3rd', categories: ['tv', 'trending'])
Post.new(id: 4, title: '4th', categories: ['movies'])
Post.new(id: 5, title: '5th', categories: ['technology', 'music'])

结果应返回帖子编号 1、2 y 3 - 类别为“新闻”、“电视”或空数组的帖子。

【问题讨论】:

  • 寻找missing 的新方法不是使用should/must_not,而是使用bool/must_not/exists。你试过吗?
  • 这个答案可能会有所帮助:stackoverflow.com/a/40397795/4604579
  • 也许我没有很好地应用你的解决方案@Val,但是谢谢(以及关于 ES 的所有其他答案)。

标签: ruby-on-rails elasticsearch elasticsearch-rails elasticsearch-ruby


【解决方案1】:

可以在must_not 中使用exists 复制缺失的内容。您必须修改查询如下:

{
  "query": {
    "bool": {
      "should": [
        {
          "terms": {
            "categories": [
              "First",
              "Second",
              "And so on"
            ]
          }
        },
        {
          "bool": {
            "must_not": [
              {
                "exists": {
                  "field": "categories"
                }
              }
            ]
          }
        }
      ]
    }
  }
}

你可以阅读它here

【讨论】:

    猜你喜欢
    • 2020-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-22
    • 2018-04-21
    • 1970-01-01
    • 1970-01-01
    • 2021-11-11
    相关资源
    最近更新 更多