# Accessing array of object in painless script , search query \[ES 6.4\]

**URL:** <https://discuss.elastic.co/t/accessing-array-of-object-in-painless-script-search-query-es-6-4/209559>\
**Category:** Elasticsearch\
**Created:** [November 26, 2019, 6:22pm UTC](https://discuss.elastic.co/t/accessing-array-of-object-in-painless-script-search-query-es-6-4/209559 "2019-11-26T18:22:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![kushal](https://avatars.discourse-cdn.com/v4/letter/k/35a633/32.png) [@kushal](https://discuss.elastic.co/u/kushal)\
**Post date:** [November 26, 2019, 6:22pm UTC](https://discuss.elastic.co/t/accessing-array-of-object-in-painless-script-search-query-es-6-4/209559/1 "2019-11-26T18:22:08Z")

</div>

Documents have an array of objects, which i need to iterate over in order to compute the final score.  
Basic mapping is following  
\<` { "settings": { "number_of_shards": 1, "number_of_replicas": 3, "max_result_window": 20000 } }, "mappings": { "taxonomy_catalog": { "properties": { "taxonomy":{ "type": "nested" }, "taxonomy_object":{ "type":"object", "enabled":false }, "type": { "type": "text" }, "id": { "type": "integer" }, "catalog_id": { "type": "integer", "fields": { "keyword": { "type": "keyword" } } } "deal": { "type": "integer" } } } } }`\>

the query is following  
\<`GET taxonomy_catalogs/_search { "query": { "function_score": { "query": { "bool": { "must_not": [{ "term": { "catalog_id": "295062" } }], "must": [{ "bool": { "filter": { "term": { "taxonomy_ss_cat": 10000 } } } }, { "nested": { "path": "taxonomy", "query": { "bool": { "must": [ { "bool": { "should": [ { "term": { "taxonomy.color.keyword": { "value": "Blue" } } }] } } ] } } } } ] } }, "functions": [{ "script_score": { "script": { "lang": "painless", "inline": " double score = 0.0; for(int i=0;i<params._source.taxonomy_object.length;i++){ double product_score = 0.0; if(params._source.taxonomy_object[i].color == params.attribute_values[0]){ product_score += params.attribute_weightage[0] } if(product_score > 0.0 ) score += Math.pow(params.A_VALUE, product_score); } return score*10.0 + doc['freshness_ptile'].value; ", "params": { "A_VALUE": 1, "attribute_values": ["Blue"], "attribute_weightage": [1.245] } } } } ] } }, "from": 0, "size": 20 }`\>

I had ensured that each of my documents have the taxonomy\_object key and its populated also, nonetheless , i keep on getting null\_pointer\_exception on the above query , complete error log is as follows  
\</`"reason": { "type": "script_exception", "reason": "runtime error", "script_stack": ["i=0;i<params._source.taxonomy_object.length;i++){ double ", " ^---- HERE"], "script": " double score = 0.0; for(int i=0;i<params._source.taxonomy_object.length;i++){ double product_score = 0.0; if(params._source.taxonomy_object[i].color == params.attribute_values[0]){ product_score += params.attribute_weightage[0] } if(product_score > 0.0 ) score += Math.pow(params.A_VALUE, product_score); } return score*10.0 + doc['freshness_ptile'].value; ", "lang": "painless", "caused_by": { "type": "null_pointer_exception", "reason": null } }`\>

Any help on this would be appreciated?

---

<div class="post-metadata">

**Author:** ![rjernst](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rjernst/32/6363_2.png) [@rjernst](https://discuss.elastic.co/u/rjernst)\
**Post date:** [November 27, 2019, 12:17am UTC](https://discuss.elastic.co/t/accessing-array-of-object-in-painless-script-search-query-es-6-4/209559/2 "2019-11-27T00:17:44Z")

</div>

Nested fields are separate documents in Lucene, so they are not accessible within scripts (see [https://github.com/elastic/elasticsearch/issues/23719](https://github.com/elastic/elasticsearch/issues/23719)). You would need to index the values into a field in the parent document.

---

<div class="post-metadata">

**Author:** ![kushal](https://avatars.discourse-cdn.com/v4/letter/k/35a633/32.png) [@kushal](https://discuss.elastic.co/u/kushal)\
**Post date:** [November 27, 2019, 5:14am UTC](https://discuss.elastic.co/t/accessing-array-of-object-in-painless-script-search-query-es-6-4/209559/3 "2019-11-27T05:14:02Z")

</div>

Hi @rjernst ,  
Thanks for reply.  
AS you would see from the code , i'm trying to access \</`taxonomy_object`\> in the painless script , which is purely an object and not a nested type. My understanding is that i should be able to access objects ( arrays of objects from the script which aren't of type nested)

---

<div class="post-metadata">

**Author:** ![kushal](https://avatars.discourse-cdn.com/v4/letter/k/35a633/32.png) [@kushal](https://discuss.elastic.co/u/kushal)\
**Post date:** [December 2, 2019, 6:35am UTC](https://discuss.elastic.co/t/accessing-array-of-object-in-painless-script-search-query-es-6-4/209559/4 "2019-12-02T06:35:56Z")

</div>

@rjernst, could you please assist on this any further, or tag someone who might be able to assist.

---

<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:** [December 30, 2019, 6:35am UTC](https://discuss.elastic.co/t/accessing-array-of-object-in-painless-script-search-query-es-6-4/209559/5 "2019-12-30T06:35:59Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
