1

我在 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
    }

有可能这个字段在少数文档中根本不存在(我不能在我的用例中使用过滤器,我只需要在脚本中使用它)

我做错了什么或如何让它工作?

4

1 回答 1

1

我无法重现该问题(我也在使用 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;}"
        }
      }
    }
  }
}
于 2016-11-02T16:27:23.590 回答