# Wrong values returned by script\_field for array.values

**URL:** <https://discuss.elastic.co/t/wrong-values-returned-by-script-field-for-array-values/20609>\
**Category:** Elasticsearch\
**Created:** [November 6, 2014, 3:10pm UTC](https://discuss.elastic.co/t/wrong-values-returned-by-script-field-for-array-values/20609 "2014-11-06T15:10:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zoltan\_Balogh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/zoltan_balogh/32/1136_2.png) [@Zoltan\_Balogh](https://discuss.elastic.co/u/Zoltan_Balogh)\
**Post date:** [November 6, 2014, 3:10pm UTC](https://discuss.elastic.co/t/wrong-values-returned-by-script-field-for-array-values/20609/1 "2014-11-06T15:10:17Z")

</div>

I've tried to retrieve array values from my data by using script\_fields,  
but I received inconsistent results.

My mapping:

{

"user": {  
"properties": {  
"id": {  
"type": "string",  
"index": "not\_analyzed"  
},  
"other\_ids": {  
"type": "string",  
"index": "not\_analyzed"  
}  
}  
}  
}

Some test data:

{  
"id": "id1",  
"other\_ids": [  
"id2",  
"id3"  
]  
}

{  
"id": "id2",  
"other\_ids": [  
"id1"  
]  
}

{  
"id": "id3",  
"other\_ids": [  
"id1",  
"id2"  
]  
}

The "simplified" query:

{  
"fields": [  
"\_source"  
],  
"query": {  
"match\_all": {}  
},  
"script\_fields": {  
"other\_id\_script": {  
"lang": "groovy",  
"script": "doc["other\_ids"].values"  
}  
}  
}

The relevant parts from 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 of other\_id\_script are differ from the current  
doc's other\_ids values in the first two cases.

Is it an error, or am I missed something? Any help would be appreciated!

(The whole script can be found here:  
[https://gist.github.com/baloghz/c27e39ad419a6f4684ab](https://gist.github.com/baloghz/c27e39ad419a6f4684ab))

Regards!  
Zoltan

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/7eeec9a3-9675-4744-ac5a-1081273abccf%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/7eeec9a3-9675-4744-ac5a-1081273abccf%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Zoltan\_Balogh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/zoltan_balogh/32/1136_2.png) [@Zoltan\_Balogh](https://discuss.elastic.co/u/Zoltan_Balogh)\
**Post date:** [November 6, 2014, 3:12pm UTC](https://discuss.elastic.co/t/wrong-values-returned-by-script-field-for-array-values/20609/2 "2014-11-06T15:12:48Z")

</div>

(My ES version is 1.3.0)

On Thursday, November 6, 2014 4:10:17 PM UTC+1, Zoltan Balogh wrote:

> I've tried to retrieve array values from my data by using script\_fields,  
> but I received inconsistent results.
> 
> My mapping:
> 
> {
> 
> "user": {  
> "properties": {  
> "id": {  
> "type": "string",  
> "index": "not\_analyzed"  
> },  
> "other\_ids": {  
> "type": "string",  
> "index": "not\_analyzed"  
> }  
> }  
> }  
> }
> 
> Some test data:
> 
> {  
> "id": "id1",  
> "other\_ids": [  
> "id2",  
> "id3"  
> ]  
> }
> 
> {  
> "id": "id2",  
> "other\_ids": [  
> "id1"  
> ]  
> }
> 
> {  
> "id": "id3",  
> "other\_ids": [  
> "id1",  
> "id2"  
> ]  
> }
> 
> The "simplified" query:
> 
> {  
> "fields": [  
> "\_source"  
> ],  
> "query": {  
> "match\_all": {}  
> },  
> "script\_fields": {  
> "other\_id\_script": {  
> "lang": "groovy",  
> "script": "doc["other\_ids"].values"  
> }  
> }  
> }
> 
> The relevant parts from 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 of other\_id\_script are differ from the current  
> doc's other\_ids values in the first two cases.
> 
> Is it an error, or am I missed something? Any help would be appreciated!
> 
> (The whole script can be found here:  
> [Test script to demonstrate strange behavior of script\_fields of array · GitHub](https://gist.github.com/baloghz/c27e39ad419a6f4684ab))
> 
> Regards!  
> Zoltan

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/98508b1b-a3e6-49b6-b49c-0dd10c8bd16c%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/98508b1b-a3e6-49b6-b49c-0dd10c8bd16c%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 6, 2017, 12:51am UTC](https://discuss.elastic.co/t/wrong-values-returned-by-script-field-for-array-values/20609/3 "2017-07-06T00:51:47Z")

</div>


