# Querying a list to check the size

**URL:** <https://discuss.elastic.co/t/querying-a-list-to-check-the-size/284969>\
**Category:** Kibana\
**Tags:** painless, kql-kibana-query-language\
**Created:** [September 23, 2021, 8:01am UTC](https://discuss.elastic.co/t/querying-a-list-to-check-the-size/284969 "2021-09-23T08:01:18Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![eztask.io](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/eztask.io/32/94976_2.png) [@eztask.io](https://discuss.elastic.co/u/eztask.io)\
**Post date:** [September 23, 2021, 8:01am UTC](https://discuss.elastic.co/t/querying-a-list-to-check-the-size/284969/1 "2021-09-23T08:01:18Z")

</div>

Hello, I am just new to Kibana query. I am trying to query a field, which is a list of objects

```auto
"leadStages" : [
            {
              "leadStage" : 1,
              "inTime" : 1597832744832,
              "outTime" : 1597946057541
            },
            {
              "outTime" : 1598083851243,
              "inTime" : 1597946057541,
              "leadStage" : 2
            },
            {
              "inTime" : 1598083851243,
              "outTime" : 0,
              "leadStage" : 3
            }
          ],

```

In the above document field i.e. leadStages, I would like to fetch last element and check "inTime" value. The query I wrote

```auto
{
  "query": {
    "bool": {
      "filter": {
        "script": {
          "script": {
            "source": """
              def stages = doc['leadStages'];
              def arrLen = stages.length-1;
              
              return (stages[arrLen].inTime == 0);
            """,
            "lang": "painless",
            "params": {
              "param1": 5
            }
          }
        }
      }
    }
  }
}

```

But the error I got is

```auto
"caused_by" : {
            "type" : "illegal_argument_exception",
            "reason" : "No field found for [leadStages] in mapping"
          }

```

I then changed the query to

```auto

            "source": """
              def stages = doc['leadStages.inTime'];
              def arrLen = stages.length-1;
              
              return (stages[arrLen].value == 0);
            """,

```

This time the error was

```auto
"type" : "illegal_state_exception",
            "reason" : "A document doesn't have a value for a field! Use doc[<field>].size()==0 to check if a document is missing a field!"

```

I have already spent couple of hours on this. Your help would help a lot.

---

<div class="post-metadata">

**Author:** ![tsullivan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tsullivan/32/31077_2.png) [@tsullivan](https://discuss.elastic.co/u/tsullivan)\
**Post date:** [September 23, 2021, 9:37pm UTC](https://discuss.elastic.co/t/querying-a-list-to-check-the-size/284969/2 "2021-09-23T21:37:41Z")

</div>

> [@eztask.io](#):
>
> `"reason" : "No field found for [leadStages] in mapping"`

Did you try to see if your index needs to have this field mapped?

See [Explicit mapping | Elasticsearch Guide [8.11] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/explicit-mapping.html)

---

<div class="post-metadata">

**Author:** ![eztask.io](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/eztask.io/32/94976_2.png) [@eztask.io](https://discuss.elastic.co/u/eztask.io)\
**Post date:** [September 25, 2021, 1:55pm UTC](https://discuss.elastic.co/t/querying-a-list-to-check-the-size/284969/3 "2021-09-25T13:55:47Z")

</div>

Yes, mapping there.

```auto
"leadStages" : {
          "type" : "nested",
          "properties" : {
            "inTime" : {
              "type" : "long"
            },
            "leadStage" : {
              "type" : "integer"
            },
            "outTime" : {
              "type" : "long"
            }
          }

```

---

<div class="post-metadata">

**Author:** ![tsullivan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tsullivan/32/31077_2.png) [@tsullivan](https://discuss.elastic.co/u/tsullivan)\
**Post date:** [September 27, 2021, 5:32pm UTC](https://discuss.elastic.co/t/querying-a-list-to-check-the-size/284969/4 "2021-09-27T17:32:35Z")

</div>

Initially, I wondered: are there multiple indices matched in the search, which might include older indices that do not have the correct mapping?

After doing more research, I noticed a few things that apply here:

1. The documentation lists the ways to interact with `nested` documents, and it doesn't mention they can be interacted with in a script: [https://www.elastic.co/guide/en/elasticsearch/reference/current/nested.html#nested-accessing-documents](https://www.elastic.co/guide/en/elasticsearch/reference/current/nested.html#nested-accessing-documents)
2. The documentation states that nested objects are stored as separate documents, but the script query works on single documents at a time
3. I found [another post with a similar question](https://discuss.elastic.co/t/help-for-painless-iterate-nested-fields/162394), and the responder stated:

> You can not access the values of all nested objects in a script at query time. Your script query only works on one nested object at a time.

---

<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:** [October 25, 2021, 5:33pm UTC](https://discuss.elastic.co/t/querying-a-list-to-check-the-size/284969/5 "2021-10-25T17:33:06Z")

</div>

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