How to loop through nested field in painless script

I have following nested type mapping:

"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 :

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

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

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

reference :

1 Like

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