# Painless query - Iterate over map fields

**URL:** <https://discuss.elastic.co/t/painless-query-iterate-over-map-fields/158259>\
**Category:** Elasticsearch\
**Created:** [November 27, 2018, 1:00am UTC](https://discuss.elastic.co/t/painless-query-iterate-over-map-fields/158259 "2018-11-27T01:00:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ig8230](https://avatars.discourse-cdn.com/v4/letter/i/3ab097/32.png) [@ig8230](https://discuss.elastic.co/u/ig8230)\
**Post date:** [November 27, 2018, 1:00am UTC](https://discuss.elastic.co/t/painless-query-iterate-over-map-fields/158259/1 "2018-11-27T01:00:38Z")

</div>

Data

```
"discovery_workflow": {
        "WORKFLOW": {
          "START": {
            "process_name": "WORKFLOW",
            "state": "START",
            "event": {
              "time": 1542060483545
            }
          },
          "END": {
            "process_name": "WORKFLOW",
            "state": "END",
            "event": {
              "time": 1542060492975
            }
          }
        }
      }

```

Script:

```
GET test-index/_search
{
  "query": {
    "script": {
      "script": {
        "source": """
            String[] workflow_states = new String[] {'WORKFLOW', 'determine_package_metadata_format', 'validate_adi'};
              for (entry in workflow_states){
                if (doc['discovery_workflow'][entry].END == null) {
                  return true;
                }
              }
            return false;
""",
        "lang": "painless"
      }
    }
  }
}

```

The above is a sample of the data I'm working with, and the script I've written so far. I'm trying to write a Painless script to iterate through the keys to check if they exist, and if so, what their values are. I'm just returning right away because I haven't gotten it to work. I keep running into weird errors like "No field found for [discovery\_workflow] in mapping with types ".

Can anyone point me in the right direction please? Thanks!

---

<div class="post-metadata">

**Author:** ![rjernst](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rjernst/32/6363_2.png) [@rjernst](https://discuss.elastic.co/u/rjernst)\
**Post date:** [November 28, 2018, 7:59pm UTC](https://discuss.elastic.co/t/painless-query-iterate-over-map-fields/158259/2 "2018-11-28T19:59:19Z")

</div>

That error means your query is running against an index that does not contain the field `discovery_workflow`. Are you sure the field exists?

---

<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 26, 2018, 8:13pm UTC](https://discuss.elastic.co/t/painless-query-iterate-over-map-fields/158259/3 "2018-12-26T20:13:01Z")

</div>

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