Elasticsearch的script_field为array.values返回了错误的值

时间:2014-11-06 13:07:19

标签: arrays elasticsearch

我尝试使用script_fields从我的数据中检索数组值,但收到的结果不一致。

我的映射:

curl -XPUT localhost:9200/test/user/_mapping -d '
{
  "user": {
    "properties": {
      "id": {
        "type": "string",
        "index": "not_analyzed"
      },
      "other_ids": {
        "type": "string",
        "index": "not_analyzed"
      }
    }
  }
}
'

一些测试数据:

curl -XPUT localhost:9200/test/user/id1 -d '
{
  "id": "id1",
  "other_ids": [
    "id2",
    "id3"
  ]
}
'

curl -XPUT localhost:9200/test/user/id2 -d '
{
  "id": "id2",
  "other_ids": [
    "id1"
  ]
}
'

curl -XPUT localhost:9200/test/user/id3 -d '
{
  "id": "id3",
  "other_ids": [
    "id1",
    "id2"
  ]
}
'

"简化"查询:

curl -XGET 'localhost:9200/test/user/_search?pretty=1'  -d '
{
  "fields": [
    "_source"
  ],
  "query": {
    "match_all": {}
  },
  "script_fields": {
    "other_id_script": {
      "lang": "groovy",
      "script": "doc[\"other_ids\"].values"
    }
  }
}
'

答案的相关部分:

  "hits" : {
    "total" : 3,
    "max_score" : 1.0,
    "hits" : [ {
      "_source":{"id":"id1","other_ids":["id2","id3"]},
      "fields" : {
        "other_id_script" : [ [ "id1", "id2" ] ]
      }
    }, {
      "_source":{"id":"id2","other_ids":["id1"]},
      "fields" : {
        "other_id_script" : [ [ "id1", "id2" ] ]
      }
    }, {
      "_source":{"id":"id3","other_ids":["id1","id2"]},
      "fields" : {
        "other_id_script" : [ [ "id1", "id2" ] ]
      }
    } ]
  }

我们可以看到other_id_script的值与前两种情况下当前文档的other_ids值不同。

这是一个错误,还是我错过了什么?

(整个脚本可以在这里找到:https://gist.github.com/baloghz/c27e39ad419a6f4684ab

1 个答案:

答案 0 :(得分:2)

看起来这个问题已在Elasticsearch issue #8576中解决,由commit d22645c修复,并在1.3.6,1.4.1和1.5.0版本中有效。

该问题的作者也提出了一个解决方法:

"script": "doc[\"other_ids\"].values.take(100)"