# ElasticSearch script query incorrect values

**URL:** https://discuss.elastic.co/t/elasticsearch-script-query-incorrect-values/283547
**Category:** Elasticsearch
**Tags:** painless
**Created:** [September 7, 2021, 2:32pm UTC](https://discuss.elastic.co/t/elasticsearch-script-query-incorrect-values/283547 "2021-09-07T14:32:01Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![me.mohammed](https://avatars.discourse-cdn.com/v4/letter/m/a3d4f5/32.png) [@me.mohammed](https://discuss.elastic.co/u/me.mohammed)
#### Post date: [September 7, 2021, 2:32pm UTC](https://discuss.elastic.co/t/elasticsearch-script-query-incorrect-values/283547/1 "2021-09-07T14:32:01Z")

</div>

Hello everyone, I'm trying to apply this query

```auto
GET rdbms_sync_idx/_search
{
   "query":{
      "nested":{
         "path":"bogoDeals",
         "query":{
            "bool":{
               "filter":[
                  {
                     "range":{
                        "bogoDeals.startAt":{
                           "lte":"now"
                        }
                     }
                  },
                  {
                     "bool":{
                        "should":[
                           {
                              "bool":{
                                 "must_not":{
                                    "exists":{
                                       "field":"bogoDeals.endAt"
                                    }
                                 }
                              }
                           },
                           {
                              "range":{
                                 "bogoDeals.endAt":{
                                    "gt":"now"
                                 }
                              }
                           }
                        ]
                     }
                  },
                  {
                     "nested":{
                        "path":"bogoDeals.bogoDealOptions",
                        "query":{
                           "bool":{
                              "filter":{
                                 "script":{
                                    "script":{
                                       "source":"(doc['bogoDeals.bogoDealOptions.bonusQuantity'].value + doc['bogoDeals.bogoDealOptions.minQuantity'].value) >= doc['quantity'].value",
                                       "lang":"expression"
                                    }
                                 }
                              }
                           }
                        }
                     }
                  }
               ]
            }
         }
      }
   }
}

```

I'm trying to retrieve the documents where `(bogoDeal.bogoDealOption.bonusQuantity + bogoDeal.bogoDealOption.minQuantity) >= quantity`, the only problem with the query above is that no matter what I do `doc['quantity']` always returns the value of 0.

Here is what my document looks like

```auto
{
   "_index":"rdbms_sync_idx",
   "_type":"_doc",
   "_id":"10",
   "_version":5,
   "_seq_no":63549,
   "_primary_term":3,
   "found":true,
   "_source":{
      "modifiedAt":"2021-09-04T19:14:13.000Z",
      "quantity":10,
      "id":10,
      "createdAt":"2021-03-20T20:32:03.033Z",
      "price":2.15,
      "bogoDeals":[
         {
            "sourceProductId":10,
            "targetProductId":10,
            "bogoDealOptions":[
               {
                  "createdAt":"2021-09-04T16:04:41.980Z",
                  "minQuantity":10,
                  "modifiedAt":"2021-09-04T16:04:41.980Z",
                  "id":27,
                  "bonusQuantity":2
               }
            ],
            "createdAt":"2021-08-30T17:40:14.179Z",
            "modifiedAt":"2021-08-30T17:40:14.179Z",
            "startAt":"2021-08-30T17:40:14.000Z",
            "description":"",
            "endAt":"2021-11-30T21:00:00.000Z",
            "id":18
         },
         {
            "sourceProductId":10,
            "targetProductId":10,
            "bogoDealOptions":[
               
            ],
            "createdAt":"2021-08-30T17:40:56.287Z",
            "modifiedAt":"2021-08-30T17:40:56.287Z",
            "startAt":"2021-11-30T21:00:00.000Z",
            "description":"",
            "endAt":"2022-11-30T21:00:00.000Z",
            "id":19
         }
      ]
   }
}

```

I tried the painless & expression languages, with both I have the issue of not being able to retrieve the value of `quantity`, I think it might be because I'm doing a script in a `nested of nested`, is there any solution to this issue? if so please attach code with your comment, really appreciated!

---

<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 5, 2021, 2:32pm UTC](https://discuss.elastic.co/t/elasticsearch-script-query-incorrect-values/283547/2 "2021-10-05T14:32:20Z")

</div>

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