# Nested Field can't access the nested field array using script query

**URL:** <https://discuss.elastic.co/t/nested-field-cant-access-the-nested-field-array-using-script-query/257256>\
**Category:** Elasticsearch\
**Created:** [December 1, 2020, 6:12pm UTC](https://discuss.elastic.co/t/nested-field-cant-access-the-nested-field-array-using-script-query/257256 "2020-12-01T18:12:27Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![khalilw1](https://avatars.discourse-cdn.com/v4/letter/k/bbce88/32.png) [@khalilw1](https://discuss.elastic.co/u/khalilw1)\
**Post date:** [December 1, 2020, 6:12pm UTC](https://discuss.elastic.co/t/nested-field-cant-access-the-nested-field-array-using-script-query/257256/1 "2020-12-01T18:12:27Z")

</div>

Hello Guys,

I have the following mapping

```auto
PUT products/_mapping
{
  "properties": {
    "discounts": {
      "properties": {
        "KW": {
          "type": "nested"
        },
        "SA": {
          "type": "nested"
        }
      }
    }
  }
}

```

each field (i.e. discounts.KW and discounts.SA) are arrays of discounts. each discount have a start and end date fields. I am trying to filter my data using script inside a nested bool query.

Here is sample data I am using

```auto
    POST /products/_update/5?doc_as_upsert=true
{
  "doc": {
    "discounts": {
      "KW": [{
        "start": "2015-01-01T12:10:30Z",
        "end": "2022-01-01T12:10:30Z"
      }],
      "SA": [{
        "start": "2015-01-01T12:10:30Z",
        "end": "2018-01-01T12:10:30Z"
      }]
    }
  }
}

```

and here is the query:

```auto
    GET /products/_search
{
  "query": { 
    "bool": { 
      "must": {
        "match_all": {}
      },
      "filter": {
        "nested": {
          "path": "discounts.KW",
          "query": {
            "script": {
              "script": { 
                "source": "for (int i = 0; i < doc['discounts.KW'].length; i++) { if ( doc['discounts.KW'].value[i].start.value <= params.now && doc['discounts.KW'].value[i].end.value < params.now) return true; } return false;",
                "params": {
                  "now": "2019-01-01T12:10:30Z"
                }
              }
            }
          }
        }
      }
    }
  }
}

```

The error I get is that there is no field discounts.KW in doc I tried variations with params.\_source.discounts.KW and ctx.\_source.discounts.KW but both give me nullpointer

Please let me know if you can help and if there is any missing information I will gladly add it

---

<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 29, 2020, 6:12pm UTC](https://discuss.elastic.co/t/nested-field-cant-access-the-nested-field-array-using-script-query/257256/2 "2020-12-29T18:12:27Z")

</div>

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