# How i can iterate over nested propery in painless elasticsearch script

**URL:** <https://discuss.elastic.co/t/how-i-can-iterate-over-nested-propery-in-painless-elasticsearch-script/358186>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [April 25, 2024, 9:05am UTC](https://discuss.elastic.co/t/how-i-can-iterate-over-nested-propery-in-painless-elasticsearch-script/358186 "2024-04-25T09:05:31Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![David\_Gegiya](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/david_gegiya/32/125222_2.png) [@David\_Gegiya](https://discuss.elastic.co/u/David_Gegiya)\
**Post date:** [April 25, 2024, 9:05am UTC](https://discuss.elastic.co/t/how-i-can-iterate-over-nested-propery-in-painless-elasticsearch-script/358186/1 "2024-04-25T09:05:31Z")

</div>

My document mapping looks like this:

```auto
                "_source": {
                    "id": 60,
                    "price": 60,
                    "locations": [
                      {
                         "id": 1
                         "price": 70
                      },
                      {
                         "id": 5,
                         "price": 80
                      }
                    ]
                }

```

So, price field can be both in root and in nested locations array. I want to be able to filter them by both values - if `locationId` is set in query parameters, then filter by price in locations, else - filter by root price.  
I wrote painless script:

```auto
			if(params['locationId'] != null){
				for (def location : doc['locations']) {
					if (location.id == params['locationId']) {
						if ((params['lte'] != null || location.price <= params['lte']) && (params['gte'] != null || location.price >= params['gte'])) {
							return true;
						} else {
							return false;
						}
					}
				}
			}
			return (params['lte'] != null || params._source.price <= params['lte']) && (params['gte'] != null || params._source.price >= params['gte']);

```

And now I'm trying to use it:

```auto
            "query": {
                "bool": {
                    "filter": [
                        {
                            "script": {
                                "script": {
                                    "source": "my_script",
                                    "params": {
                                        "locationId": "2"
                                    }
                                }
                            }
                        }
                    ]
                }
            }

```

But I'm getting an error on second line of my script.  
`No field found for [locations] in mapping with types []`  
How can I solve this? I also tried to access `locations` by `params._source.locations`, as I do similar in sort scripts, but `params` are not accessible from `filters` query
