elasticsearch - ElasticSearch 2.X查找数组中存在数值失败

标签 elasticsearch elasticsearch-2.0 elasticsearch-dsl

我有一个映射的字段如下ES2.3

"move_in_ts": {
  "type": "integer"
}

"move_out_ts": {
  "type": "integer"
}

Sample document stores data as follows:

"move_in_ts": [
        1475280000,
        1475539200,
        1475712000,
        1475884800,
        1477008000,
        1477785600
      ]

我的DSL查询中有一个脚本(试图在该数组中找到一个整数)
"script": "if(doc['move_in_ts'] && doc['move_in_ts'].values.contains('1475280000')){return 200;}"

并尝试了这个:
 "script": "if(doc['move_in_ts'] && doc['move_in_ts'].contains('1475280000')){return 200;}"

并尝试了这个:
 "script": "if(doc['move_in_ts'] && doc['move_in_ts'].contains(1475280000)){return 200;}"

并尝试了这个:
 "script": "if(doc['move_in_ts'] && doc['move_in_ts'].values.contains(1475280000)){return 200;}"

但在上述所有情况下,都会出现以下错误:
"reason": {
      "type": "null_pointer_exception",
      "reason": null
    }

几个文档中可能根本不存在该字段(我无法在用例中使用过滤器,只需要在脚本中使用它)

我在做什么错或如何使它正常工作?

最佳答案

我无法重现该问题(我也在使用ES 2.3)。您还需要toLong()以获得正确的结果,否则它将得到零结果。我创建了这样的样本索引。

PUT books
{
  "mappings": {
    "book":{
      "properties": {
        "move_in_ts":{
          "type": "integer"
        }
      }
    }
  }
}

我索引了一些文档
POST books/book
{
  "move_in_ts" : null
}

POST books/book
{
  "move_in_ts" : [4,null]
}

POST books/book
{
  "move_in_ts" : []
}

POST books/book
{
  "some_other_field" : "some value"
}

POST books/book
{
  "move_in_ts" : [
        1475280000,
        1475539200,
        1475712000,
        1475884800,
        1477008000,
        1477785600
      ]
}

然后下面的查询给出正确的结果
GET books/book/_search
{
  "query": {
    "bool": {
      "filter": {
        "script": {
          "script": "if(doc['move_in_ts'] && doc['move_in_ts'].values.contains(1475280000.toLong())){return 200;}"
        }
      }
    }
  }
}

关于elasticsearch - ElasticSearch 2.X查找数组中存在数值失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40382354/

相关文章:

elasticsearch - 如何在 Elasticsearch 中按天聚合查询?

python - Haystack-Django 模板不存在于/search/Error

elasticsearch - 如何跨多个文本字段组合完成、建议和匹配短语?

Elasticsearch : Curator does not work

elasticsearch - 从inner_hits过滤后的对象抛出错误中检索字段值

elasticsearch - 如何与多个输入 Elasticsearch 匹配

elasticsearch - 如何在Elasticsearch中实现此sql查询结果

json - window : curl with json data on the command line

elasticsearch - 在Elasticsearch中使用术语查询

python - Elasticsearch子字符串查询的一种特殊情况