【问题标题】:ElasticSearch 2.X finding a numeric value exists in the array failsElasticSearch 2.X 查找数组中存在的数值失败
【发布时间】:2016-11-02 14:24:12
【问题描述】:

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

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

我做错了什么或如何让它发挥作用?

【问题讨论】:

  • 是 'move_in_ts' 嵌套字段吗?
  • 我无法重现该问题。你能发布你的整个查询吗?你的索引很大吗?
  • 不,它不是嵌套字段
  • @ChintanShah25 不,我的索引很小。哪个特定脚本适合您?
  • @ChintanShah25 这是我正在使用的真实脚本。 "functions": [{ "script_score": { "script": "if (doc['move_in_ts'] && doc['move_out_ts']) {return per_day_boost * ( [ doc['move_in_ts'], doc['move_out_ts'] ].transpose().find { av_start, av_out -> av_start <= desired_in_ts && av_out >= desired_out_ts }[0] - desired_in_ts).abs()/86400}; return 0;", "params": { "per_day_boost": -175, "desired_in_ts": 1478131200, "desired_out_ts": 1494547200 } } }]

标签: elasticsearch elasticsearch-2.0 elasticsearch-dsl


【解决方案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;}"
        }
      }
    }
  }
}

【讨论】:

    猜你喜欢
    • 2013-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-12
    • 1970-01-01
    • 2022-06-28
    • 2019-10-12
    • 1970-01-01
    相关资源
    最近更新 更多