# Accessing the value of a dynamic key in an object

**URL:** https://discuss.elastic.co/t/accessing-the-value-of-a-dynamic-key-in-an-object/136012
**Category:** Elasticsearch
**Created:** [June 15, 2018, 12:23am UTC](https://discuss.elastic.co/t/accessing-the-value-of-a-dynamic-key-in-an-object/136012 "2018-06-15T00:23:23Z")
**Posts on this page:** 7
**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: [June 15, 2018, 12:23am UTC](https://discuss.elastic.co/t/accessing-the-value-of-a-dynamic-key-in-an-object/136012/1 "2018-06-15T00:23:23Z")

</div>

Partial mapping of one of my index is as follows:

```
"schools": {
  "type": "object",
  "dynamic": true,
  "properties": {}
}

```

A sample `schools` object of an existing document is:

```
"schools": {
  "8291": {
    "max": 11,
    "min": 11
  },
  "3546": {
    "max": 12,
    "min": 10
  },
  "3896": {
    "max": 18,
    "min": 12
  }
}

```

I have painless script in ES6.2 where dynamically `key` (e.g. 3896) is computed on runtime and passed to script as a parameter

```
"script": {
  "params": {
    "key": key
  },
  "source": {
    if (doc.containsKey('schools') && doc.schools.containsKey(key)){
      String dynamic_key = "schools."+key+".min";
      return doc[dynamic_key].value;
    } else {
      return 0;
  }
}

```

So if passed `key` value is 3896, then this should return 12. But I get an error message

`No field found for [schools.3896.min] in mapping with types [myindexname]`

What am I doing wrong? What is the correct way to access a dynamic object where a key name is computed on runtime to fetch its value?

---

<div class="post-metadata">

### Author: ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)
#### Post date: [June 18, 2018, 2:26am UTC](https://discuss.elastic.co/t/accessing-the-value-of-a-dynamic-key-in-an-object/136012/2 "2018-06-18T02:26:13Z")

</div>

How many different values under "schools" are you going to have in your index?

---

<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: [June 18, 2018, 5:43pm UTC](https://discuss.elastic.co/t/accessing-the-value-of-a-dynamic-key-in-an-object/136012/3 "2018-06-18T17:43:53Z")

</div>

@Igor_Motov at a particular time it would be 72, but these kay names changes every week.

---

<div class="post-metadata">

### Author: ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)
#### Post date: [June 18, 2018, 5:50pm UTC](https://discuss.elastic.co/t/accessing-the-value-of-a-dynamic-key-in-an-object/136012/4 "2018-06-18T17:50:02Z")

</div>

Do you have a daily or weekly indices? If not, increasing the number of dynamic fields is not going to be as sustainable solution.

---

<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: [June 18, 2018, 5:54pm UTC](https://discuss.elastic.co/t/accessing-the-value-of-a-dynamic-key-in-an-object/136012/5 "2018-06-18T17:54:46Z")

</div>

No, I don't have weekly/daily indices.

---

<div class="post-metadata">

### Author: ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)
#### Post date: [June 18, 2018, 5:56pm UTC](https://discuss.elastic.co/t/accessing-the-value-of-a-dynamic-key-in-an-object/136012/6 "2018-06-18T17:56:13Z")

</div>

In this case I would try to avoid having dynamic fields here. What kind of searches are you planning to do with data and what are you trying to achieve by using the script?

---

<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: [July 16, 2018, 5:56pm UTC](https://discuss.elastic.co/t/accessing-the-value-of-a-dynamic-key-in-an-object/136012/7 "2018-07-16T17:56:17Z")

</div>

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