【问题标题】:Elasticsearch: filter documents on missing field unless other field existsElasticsearch:过滤缺少字段的文档,除非存在其他字段
【发布时间】:2016-07-18 10:10:11
【问题描述】:

我正在做一个 Elasticsearch 查询,它过滤掉所有文档,其中缺少特定字段

query: {
  filtered: {
    filter: {
      bool: {
        should: {
          bool: {
            must: {
              term: 'someCondition'
            },
            must_not: {
              missing: {field: 'somefield'}
            }
          }
        }
      }
    }
  }
}

这按预期工作。现在我需要将此missingfilter 设置为有条件的,以便仅在另一个字段不存在时才匹配。

我尝试将我的 must_not 转换为 should,如下所示:

should: {
  missing: {field: 'somefield'},
  exists: {'field': 'somotherfield'}
}

但这似乎不起作用。

澄清一下:“SELECT * from docs WHERE (somefield IS NOT NULL OR someotherfield IS NOT NULL)”

【问题讨论】:

  • 如果你把它写出来,你会突然明白它应该是怎样的......should: [[{exists:{field:somefield}}, {exists:{field:'someotherfield'}}]]

标签: elasticsearch


【解决方案1】:

试试这样的查询:

"filters" : [ {
"missing" : {
        "field" : "somefield"
  }
}, {
  "not" : {
    "filter" : {
      "missing" : {
        "field" : "somotherfield"
      }
    }
  }
} ]

【讨论】:

    猜你喜欢
    • 2014-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-26
    • 1970-01-01
    • 2016-02-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多