# Querying and sorting using painless script and nested fields

**URL:** <https://discuss.elastic.co/t/querying-and-sorting-using-painless-script-and-nested-fields/249408>\
**Category:** Elasticsearch\
**Created:** [September 21, 2020, 6:30pm UTC](https://discuss.elastic.co/t/querying-and-sorting-using-painless-script-and-nested-fields/249408 "2020-09-21T18:30:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Emanuel\_Velzi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/emanuel_velzi/32/66975_2.png) [@Emanuel\_Velzi](https://discuss.elastic.co/u/Emanuel_Velzi)\
**Post date:** [September 21, 2020, 6:31pm UTC](https://discuss.elastic.co/t/querying-and-sorting-using-painless-script-and-nested-fields/249408/1 "2020-09-21T18:31:00Z")

</div>

Hi,

I am querying an index in my ES cluster 7.9.1:

```auto
{
   "query": {
      "multi_match": {
         "query": "Ciudad Boli",
         "fields": [
            "name.*.single"
         ],
         "type": "phrase_prefix"
      }
   },
   "sort": [
      {
         "_script": {
            "script": {
               "source": "_score * (doc.containsKey('sales.countries.CO.products.HOTELS.total') ? (1.0 + doc['sales.countries.CO.products.HOTELS.total'].value) : 0.0)",
               "lang": "painless"
            },
            "type": "number",
            "order": "desc"
         }
      }
   ],
   "track_scores": true
}

```

Some documents have an object "sales", with a property "countries", with a property "CO" and so on so on.. but other documents not. The idea is sort the results according this data.  
But I'm getting this error:

```auto
{
    "error": {
        "root_cause": [
            {
                "type": "script_exception",
                "reason": "runtime error",
                "script_stack": [
                    "org.elasticsearch.index.fielddata.ScriptDocValues$Longs.get(ScriptDocValues.java:121)",
                    "org.elasticsearch.index.fielddata.ScriptDocValues$Longs.getValue(ScriptDocValues.java:115)",
                    "_score * (doc.containsKey('sales.countries.CO.products.HOTELS.total') ? (1.0 + doc['sales.countries.CO.products.HOTELS.total'].value) : 0.0)",
                    " ^---- HERE"
                ],
                "script": "_score * (doc.containsKey('sales.countries.CO.products.HOTELS.total') ? (1.0 + doc['sales.countries.CO.products.HOTELS.total'].value) : 0.0)",
                "lang": "painless",
                "position": {
                    "offset": 126,
                    "start": 0,
                    "end": 140
                }
            }
        ],
        "type": "search_phase_execution_exception",
        "reason": "all shards failed",
        "phase": "query",
        "grouped": true,
        "failed_shards": [
            {
                "shard": 0,
                "index": "cities",
                "node": "Jxy454W0S-mxTongRFYWOw",
                "reason": {
                    "type": "script_exception",
                    "reason": "runtime error",
                    "script_stack": [
                        "org.elasticsearch.index.fielddata.ScriptDocValues$Longs.get(ScriptDocValues.java:121)",
                        "org.elasticsearch.index.fielddata.ScriptDocValues$Longs.getValue(ScriptDocValues.java:115)",
                        "_score * (doc.containsKey('sales.countries.CO.products.HOTELS.total') ? (1.0 + doc['sales.countries.CO.products.HOTELS.total'].value) : 0.0)",
                        " ^---- HERE"
                    ],
                    "script": "_score * (doc.containsKey('sales.countries.CO.products.HOTELS.total') ? (1.0 + doc['sales.countries.CO.products.HOTELS.total'].value) : 0.0)",
                    "lang": "painless",
                    "position": {
                        "offset": 126,
                        "start": 0,
                        "end": 140
                    },
                    "caused_by": {
                        "type": "illegal_state_exception",
                        "reason": "A document doesn't have a value for a field! Use doc[<field>].size()==0 to check if a document is missing a field!"
                    }
                }
            }
        ]
    },
    "status": 400
}

```

Can anybody help me please?

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [September 22, 2020, 7:38am UTC](https://discuss.elastic.co/t/querying-and-sorting-using-painless-script-and-nested-fields/249408/2 "2020-09-22T07:38:56Z")

</div>

Hey,

the exception already states a workaround, that you should include in your script, because you seem to have documents, where the field that you are referring to does not exist.

> [@Emanuel\_Velzi](#):
>
> `A document doesn't have a value for a field! Use doc[<field>].size()==0 to check if a document is missing a field!"`

--Alex

---

<div class="post-metadata">

**Author:** ![Emanuel\_Velzi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/emanuel_velzi/32/66975_2.png) [@Emanuel\_Velzi](https://discuss.elastic.co/u/Emanuel_Velzi)\
**Post date:** [September 22, 2020, 12:26pm UTC](https://discuss.elastic.co/t/querying-and-sorting-using-painless-script-and-nested-fields/249408/3 "2020-09-22T12:26:10Z")

</div>

Thanks for your reply...

Yes, that's the key. I thought that checking `doc.containsKey(...)` was enough, but I was wrong.  
Later I found in the documentation:

> The `doc['field']` will throw an error if `field` is missing from the mappings. In `painless` , a check can first be done with `doc.containsKey('field')` to guard accessing the `doc` map.

Finally, using both checks works for me:  
`if(doc.containsKey(key) && doc[key].size()>=1.0) {`

Thanks!

---

<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:** [October 20, 2020, 12:26pm UTC](https://discuss.elastic.co/t/querying-and-sorting-using-painless-script-and-nested-fields/249408/4 "2020-10-20T12:26:15Z")

</div>

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