# Script query with nested array - is it possible?

**URL:** <https://discuss.elastic.co/t/script-query-with-nested-array-is-it-possible/263772>\
**Category:** Elasticsearch\
**Created:** [February 9, 2021, 4:34pm UTC](https://discuss.elastic.co/t/script-query-with-nested-array-is-it-possible/263772 "2021-02-09T16:34:22Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ofir\_Edi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ofir_edi/32/78181_2.png) [@Ofir\_Edi](https://discuss.elastic.co/u/Ofir_Edi)\
**Post date:** [February 9, 2021, 4:34pm UTC](https://discuss.elastic.co/t/script-query-with-nested-array-is-it-possible/263772/1 "2021-02-09T16:34:22Z")

</div>

Hi,  
i'm having issue filtering by script on a nested array.  
this is my sample data and mapping:

```auto
        {
              "id" : "123",
              "messages" : [
                {
                  "level" : "error",
                  "timestamp" : "01/02/2021",
                  "message" : "event1"
                },
                {
                  "level" : "error",
                  "timestamp" : "01/02/2021",
                  "message" : "event2"
                },
                {
                  "level" : "trace",
                  "timestamp" : "02/02/2021",
                  "message" : "event3"
                }
              ]
            }

```

mapping:

```auto
        {
        "mappings" : {
          "properties" : {
            "messages" : {
              "type" : "nested",
              "properties" : {
                "level" : {
                  "type" : "keyword"
                },
                "message" : {
                  "type" : "text"
                },
                "timestamp" : {
                  "type" : "date",
                  "format" : "dd/MM/yyyy"
                }
              }
            },
            "id" : {
              "type" : "keyword"
            }
          }
        }
      }

```

here is a sample query to try and find documents with at leaser 2 error messages:

```auto
    {
      "query": {
        "bool": {
          "filter": {
            "script": {
              "script": {
                "source": """
                  int counter = 0;
                  for (def event : doc['messages'].value) {
                  	if (event.level.equals("error")) { counter++; }
                  }
                  return counter > 1;
                """,
                "lang": "painless"
              }
            }
          }
        }
      }
    }

```

but i' getting the following error: No field found for [messages] in mapping with types .  
if i'm trying to do any filtering on id field (which is not nested) everything works fine.  
I could not find in the documentation if this is supported or not and if it does - then how to apply such logic.  
I'm using version 7.10

---

<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:** [March 9, 2021, 4:35pm UTC](https://discuss.elastic.co/t/script-query-with-nested-array-is-it-possible/263772/2 "2021-03-09T16:35:07Z")

</div>

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