我有一个映射的字段如下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/