# How to loop through nested field in painless script

**URL:** <https://discuss.elastic.co/t/how-to-loop-through-nested-field-in-painless-script/288695>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [November 9, 2021, 3:22am UTC](https://discuss.elastic.co/t/how-to-loop-through-nested-field-in-painless-script/288695 "2021-11-09T03:22:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Agnivesh\_Sharma](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/agnivesh_sharma/32/77134_2.png) [@Agnivesh\_Sharma](https://discuss.elastic.co/u/Agnivesh_Sharma)\
**Post date:** [November 9, 2021, 3:22am UTC](https://discuss.elastic.co/t/how-to-loop-through-nested-field-in-painless-script/288695/1 "2021-11-09T03:22:24Z")

</div>

I have following nested type mapping:

```auto
"visitor_data": {
            "type": "nested",
            "dynamic": "false",
            "properties": {
              "checkin_time": {
                "type": "date",
                "format": "epoch_second"
              },
              "completed_on": {
                "type": "date",
                "format": "epoch_second"
              },
              "edition_id": {
                "type": "keyword"
              },
              "end_date": {
                "type": "date",
                "format": "date"
              },
              "event_id": {
                "type": "keyword"
              },
              "evisitor": {
                "type": "boolean"
              },
              "flag": {
                "type": "byte"
              },
              "interest_date": {
                "type": "date",
                "format": "epoch_second"
              },
              "is_verified": {
                "type": "boolean"
              },
              "post_status": {
                "type": "byte"
              },
              "pre_status": {
                "type": "byte"
              },
              "start_date": {
                "type": "date",
                "format": "date"
              },
              "status": {
                "type": "byte"
              },
              "visitor_published": {
                "type": "byte"
              },
              "visitor_score": {
                "type": "byte"
              }
            }
          }

```

I am trying to loop through this field in script but it seems i cant access it. please suggest a way of doing it. here is my script code :

```auto
"script_score": {
            "script": {
              "lang": "painless", 
              "source": """
                int total = 0;
                for (int i = 0; i < doc['visitor_data'].length; ++i)
                {
                  total +=1;
                }
                return total;
              """
            }
          }

```

---

<div class="post-metadata">

**Author:** ![Agnivesh\_Sharma](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/agnivesh_sharma/32/77134_2.png) [@Agnivesh\_Sharma](https://discuss.elastic.co/u/Agnivesh_Sharma)\
**Post date:** [November 9, 2021, 8:44pm UTC](https://discuss.elastic.co/t/how-to-loop-through-nested-field-in-painless-script/288695/2 "2021-11-09T20:44:01Z")

</div>

I found the solution. it was to use with params field

```auto
for(int i=0; i<params['_source']['visitor_data'].length;++i)
              {
                if(params['_source']['visitor_data'][i]['edition_id'] == "476097")
                total += 1;
              }

```

reference :

> **[Retrieve selected fields from a search | Elasticsearch Guide \[master\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/master/search-fields.html#script-fields)**

---

<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 7, 2021, 8:44pm UTC](https://discuss.elastic.co/t/how-to-loop-through-nested-field-in-painless-script/288695/3 "2021-12-07T20:44:03Z")

</div>

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