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

Hello Guys,

I have the following mapping

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

    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:

    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

1 Like

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