在elasticsearch中搜索值为NULL或任何特定值的字段

mar*_*ral 5 null search elasticsearch

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

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

field == null OR field == [1,2,3,4,]
Run Code Online (Sandbox Code Playgroud)

现在当我这样查询时

'should' => [
  [
    'term' => ['field' => null]
  ],
  [
    'terms' => ['field' => [1, 2, 3, 4, 5 ,6]
  ]
]
Run Code Online (Sandbox Code Playgroud)

它给我错误 Bad Request..

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

Val*_*Val 3

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

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

NOT(_exists_:field) OR field == [1,2,3,4,]
Run Code Online (Sandbox Code Playgroud)

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

'should' => [
  [
    'bool' => [ 'must_not' => [ 'exists' => [ 'field' => 'field' ] ] ]
  ],
  [
    'terms' => ['field' => [1, 2, 3, 4, 5 ,6]
  ]
]
Run Code Online (Sandbox Code Playgroud)