【问题标题】:search for a field in elasticsearch that its value is NULL or any specific value在 elasticsearch 中搜索其值为 NULL 或任何特定值的字段
【发布时间】:2018-02-01 12:54:24
【问题描述】:

我想在 elasticsearch 中编写一个查询,在字段中搜索空值和任何特定值, 我读了这个链接:null_values

并发现对于类型为 boolean 或 long 的字段,其默认值为 null,但我如何告诉 elastic 在 long 字段中搜索以具有值或 null:

field == null OR field == [1,2,3,4,]

现在当我这样查询时

'should' => [
  [
    'term' => ['field' => null]
  ],
  [
    'terms' => ['field' => [1, 2, 3, 4, 5 ,6]
  ]
]

它让我出错 Bad Request..

你能帮我写下我的查询吗? 谢谢

【问题讨论】:

    标签: elasticsearch search null


    【解决方案1】:

    您需要使用exists 查询来检查该字段是否存在。

    在查询字符串语法中,它是这样的:

    NOT(_exists_:field) OR field == [1,2,3,4,]
    

    在查询 DSL 中,它是这样的:

    'should' => [
      [
        'bool' => [ 'must_not' => [ 'exists' => [ 'field' => 'field' ] ] ]
      ],
      [
        'terms' => ['field' => [1, 2, 3, 4, 5 ,6]
      ]
    ]
    

    【讨论】:

    • 你看到我的链接了吗??因为我使用了一个和你一样的查询......它不关心我的任何应该参数......我认为这是因为我的错误查询......我被困在这里@Val
    • stackoverflow.com/questions/48523312/… 这是我描述我的问题的最后一个问题的链接
    • 确保在您的布尔查询中也包含minimum_should_match
    • 只有一次,不是两次,即最里面的布尔值而不是最外面的布尔值
    • 感谢您的快速响应......它现在可以正常工作......问题是我的 sql 中的数据已更改......但在我的弹性索引中它没有......
    猜你喜欢
    • 2016-06-08
    • 1970-01-01
    • 1970-01-01
    • 2016-09-07
    • 2013-03-18
    • 1970-01-01
    • 2015-12-26
    • 2016-03-12
    • 2017-12-24
    相关资源
    最近更新 更多