Search code examples
elasticsearchelasticsearch-dsl

Elasticsearch DSL query returning result for condition which isn't true


I want to have three conditions in my elasticsearch query and accordingly I have written as below. But I don't know why it is returning a DOCUMENT where AMOUNT is 250 and it EXISTS whereas my condition is ATLEAST one of the two i.e. AMOUNT less than or equal to zero or AMOUNT should not exist.

Below is the DSL Query enter image description here

{
   "from":0,
   "size":10,
   "track_total_hits":true,
   "_source": ["amount", "npa_stageid_loanaccounts"],
   "query":{
      "bool":{
         "must":[
            {
               "query_string":{
                  "default_field":"npa_stageid_loanaccounts.keyword",
                  "query":"Y"
               }
            },
            {
               "bool":{
                  "minimum_should_match":1,
                  "should":[
                     {
                        "range":{
                           "Amount":{
                              "lte":0
                           }
                        }
                     },
                     {
                        "bool":{
                           "must_not":[
                              {
                                 "exists":{
                                    "field":"Amount"
                                 }
                              }
                           ]
                        }
                     }
                  ]
               }
            }
         ]
      }
   }
}

Solution

  • In your documents, you have amount but in your query you have Amount, the casing is not the same.