Invalid values returned by Elasticearch script_field for array.values
I tried to extract array values from my data using script_fields
, but got inconsistent results.
My display:
curl -XPUT localhost:9200/test/user/_mapping -d '
{
"user": {
"properties": {
"id": {
"type": "string",
"index": "not_analyzed"
},
"other_ids": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
'
Some test data:
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"
]
}
'
"Simplified" query:
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"
}
}
}
'
Relevant parts of the answer:
"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" ] ]
}
} ]
}
As we can see, the values other_id_script
differ from the current doc values other_ids
in the first two cases.
Is this a bug or am I missing something?
(The entire script can be found here: https://gist.github.com/baloghz/c27e39ad419a6f4684ab )
source to share
It seems that this problem has been solved in Elasticsearch issue # 8576 , it was amended to make d22645c and stored in real time in versions 1.3.6, 1.4.1 and 1.5.0.
The author of the question also gave a workaround:
"script": "doc[\"other_ids\"].values.take(100)"
source to share