# How to get a value of key value pair in an object in elasticsearch painless script

**URL:** <https://discuss.elastic.co/t/how-to-get-a-value-of-key-value-pair-in-an-object-in-elasticsearch-painless-script/140972>\
**Category:** Elasticsearch\
**Created:** [July 20, 2018, 11:33pm UTC](https://discuss.elastic.co/t/how-to-get-a-value-of-key-value-pair-in-an-object-in-elasticsearch-painless-script/140972 "2018-07-20T23:33:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![pungent](https://avatars.discourse-cdn.com/v4/letter/p/a698b9/32.png) [@pungent](https://discuss.elastic.co/u/pungent)\
**Post date:** [July 20, 2018, 11:33pm UTC](https://discuss.elastic.co/t/how-to-get-a-value-of-key-value-pair-in-an-object-in-elasticsearch-painless-script/140972/1 "2018-07-20T23:33:47Z")

</div>

A partial mapping of one of my indices (in ES6.2) is as follows

```
"sdata": {
  "type": "object",
  "dynamic": true,
  "properties": {
    "xyz": {
      "dynamic": true,
      "properties": {}
    },
    "students": {
      "dynamic": true,
      "properties": {
        "name_age": {
          "dynamic": "true",
          "type": "object",
          "properties": {
          }
        },
        "school_id": {
          "type": "integer"
        }
      }
    }
  }
}

```

and one sample doc of that index is as follows:

```
{
  "sdata": {
      "xyz": {
          "abc": 0,
          "def": 0,
          "klm": null
      },
      "students": {
          "roll_age": {
              1021: 27,
              99271: 12,
              5119: 21,
          },
          "school_id": 12
      }
  }
}

```

I have a painless script in which my goal is to do a custom scoring calculation based on supplied student's roll number (that is a random number). For example, if  
student roll is 99271, then it should return score 12

I tried to write a script in the following two ways - option1 and option2 below. None of them worked.

```
"script": {
  "params": {
    "student_roll": 99271
  },
  "source": """
    
    // option - 1
    if (doc['sdata.students.roll_age'].containsKey(params.student_roll)) {
      // how to return the value?
    }

    // option - 2
    int ilen = doc['sdata.students.roll_age'].length;
    for (int i = 0; i < ilen; i++) {
      if(doc['sdata.students.roll_age'][i] == params.student_roll) {
        return doc['sdata.students.roll_age'][i];
      }
    }

  """
}

```

Any idea how to get it through painless script?

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [July 23, 2018, 9:22am UTC](https://discuss.elastic.co/t/how-to-get-a-value-of-key-value-pair-in-an-object-in-elasticsearch-painless-script/140972/2 "2018-07-23T09:22:44Z")

</div>

Your sample document is not valid JSON. Did you mean for the keys inside `roll_age` to be strings?

```auto
{
  "sdata": {
    "xyz": {
      "abc": 0,
      "def": 0,
      "klm": null
    },
    "students": {
      "roll_age": {
        "1021": 27,
        "5119": 21,
        "99271": 12
      },
      "school_id": 12
    }
  }
}

```

If so, you can use the following Painless script to retrieve the values:

```auto
"script": {
  "params": {
    "student_roll": 99271
  },
  "source": """
return doc['sdata.students.roll_age.' + params.student_roll].value
"""
}

```

---

<div class="post-metadata">

**Author:** ![pungent](https://avatars.discourse-cdn.com/v4/letter/p/a698b9/32.png) [@pungent](https://discuss.elastic.co/u/pungent)\
**Post date:** [July 23, 2018, 6:12pm UTC](https://discuss.elastic.co/t/how-to-get-a-value-of-key-value-pair-in-an-object-in-elasticsearch-painless-script/140972/3 "2018-07-23T18:12:10Z")

</div>

Thank you @abdon ..yes my sample snippet was wrong, `roll_age` is string. It works now. Thank you 🙂 but is there any way to iterate `key=>value` pair in `for` loop?

---

<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:** [August 20, 2018, 6:12pm UTC](https://discuss.elastic.co/t/how-to-get-a-value-of-key-value-pair-in-an-object-in-elasticsearch-painless-script/140972/4 "2018-08-20T18:12:15Z")

</div>

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