# Trouble with nested object search in painless

**URL:** <https://discuss.elastic.co/t/trouble-with-nested-object-search-in-painless/370419>\
**Category:** Elastic Search\
**Tags:** painless\
**Created:** [November 12, 2024, 4:45pm UTC](https://discuss.elastic.co/t/trouble-with-nested-object-search-in-painless/370419 "2024-11-12T16:45:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sgtlaugh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sgtlaugh/32/139153_2.png) [@sgtlaugh](https://discuss.elastic.co/u/sgtlaugh)\
**Post date:** [November 12, 2024, 4:45pm UTC](https://discuss.elastic.co/t/trouble-with-nested-object-search-in-painless/370419/1 "2024-11-12T16:45:30Z")

</div>

Hello, I am trying to do the following. Suppose there is an index called my-index with the following mapping:

```auto
{
  "my-index": {
    "mappings": {
      "properties": {
        "fields": {
          "type": "nested",
          "properties": {
            "id": {
              "type": "keyword"
            },
            "names": {
              "type": "keyword"
            }
          }
        }
      }
    }
  }
}

```

What I am trying to do is sort the documents based on fields[fieldId].names, where fieldId will be given in the params dynamically. This is what I've tried from Python shell.

```auto
import requests

headers = {
    'Content-Type': 'application/json',
}

json_data = {
    'script': {
        'source': """
            // Simplified version of an attempt
            if (doc['fields'] == null || doc['fields'].length == 0) {
              return false;
            }
            
            return true;

        """,
        'params': {
            'fieldId': 'id1'
        },
        'lang': 'painless',
    },
    'context': 'filter',
    'context_setup': {
        'index': 'my-index',
        'document': {
            'fields': [
                {
                    'id': 'id1',
                    'names': 'CF1,CF3',
                },
                {
                    'id': 'id2',
                    'names': [],
                },
                {
                    'id': 'id3',
                    'names': 'hello,world,lol,zzz',
                },
            ],
        },
    },
}

requests.post('http://localhost:9200/_scripts/painless/_execute?pretty', headers=headers, json=json_data).json()

```

I did not write the full query in the source. The main problem I am having is that whenever I try to access `doc['fields']`, it's throwing an error no matter what I try. And I can't figure out why it's happening and how to solve it. I can access `doc['field.id']` and `doc['field.names']` but not `doc['field']`. Any idea why I could be getting the following error and how to solve it? Any help would be greatly appreciated, thanks.

```auto
{'error': {'root_cause': [{'type': 'script_exception', 'reason': 'runtime error', 'script_stack': ['org.opensearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:87)', 'org.opensearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:55)', "if (doc['fields'] == null || doc['fields'].length == 0) {\n ", ' ^---- HERE'], 'script': ' ...', 'lang': 'painless', 'position': {'offset': 69, 'start': 61, 'end': 133}}], 'type': 'script_exception', 'reason': 'runtime error', 'script_stack': ['org.opensearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:87)', 'org.opensearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:55)', "if (doc['fields'] == null || doc['fields'].length == 0) {\n ", ' ^---- HERE'], 'script': ' ...', 'lang': 'painless', 'position': {'offset': 69, 'start': 61, 'end': 133}, 'caused_by': {'type': 'illegal_argument_exception', 'reason': 'No field found for [fields] in mapping'}}, 'status': 400}

```

---

<div class="post-metadata">

**Author:** ![sgtlaugh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sgtlaugh/32/139153_2.png) [@sgtlaugh](https://discuss.elastic.co/u/sgtlaugh)\
**Post date:** [November 12, 2024, 4:45pm UTC](https://discuss.elastic.co/t/trouble-with-nested-object-search-in-painless/370419/2 "2024-11-12T16:45:55Z")

</div>

Removed #opensearch

---

<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 10, 2024, 4:46pm UTC](https://discuss.elastic.co/t/trouble-with-nested-object-search-in-painless/370419/3 "2024-12-10T16:46:53Z")

</div>

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