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

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?

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

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)

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

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