Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bool query does not support filter

This is the query that throws the exception using ES 2.0:

bool query does not support filter

How to use Exists and Missing query?

Query:

{
   "bool":{
      "must":[
         {
            "bool":{
               "should":[
                  {
                     "bool":{
                        "must":[
                           {
                              "range":{
                                 "startDate":{
                                    "lte":"2016-10-27T11:24:49.6616538+05:30"
                                 }
                              }
                           }
                        ],
                        "filter":[
                           {
                              "bool":{
                                 "must_not":[
                                    {
                                       "exists":{
                                          "field":"endDate"
                                       }
                                    }
                                 ]
                              }
                           }
                        ]
                     }
                  }
               ]
            }
         }
      ]
   }
}
like image 359
user7078562 Avatar asked Jan 06 '23 04:01

user7078562


1 Answers

Well first off, that error is generally from use on a 1.x version of Elasticsearch. (in that case you need a FilteredQuery)

Next it appears you have many levels of unnecessary nesting. Not sure if maybe you stripped other things out to make a simpler example. I have rewritten your query like this (and added the outer braces):

{
    "query" : {
        "bool" : {
            "must" : [{
                    "range" : {
                        "startDate" : { "lte" : "2016-10-27T11:24:49.6616538+05:30" }
                    }
                }
            ],
            "filter" : [{
                    "bool" : {
                        "must_not" : [{
                                "exists" : { "field" : "endDate"    }
                            }
                        ]
                    }
                }
            ]} } 
}

Both your original query and my rewritten one work fine on my server (v2.3.1) so I'm guessing really you have ES 1.x ?

Also, if you are not leveraging the lucene scoring, and just want to return the documents (or apply your own sort) then you can drop the filter altogether and simplify it further:

{
    "query" : {
        "bool" : {
            "must" : [{
                    "range" : {
                        "startDate" : { "lte" : "2016-10-27T11:24:49.6616538+05:30"}
                    }
                }
            ],
            "must_not" : [{
                    "exists" : { "field" : "endDate"    }
                }
            ]} } 
}
like image 156
Thunder Avatar answered May 30 '23 08:05

Thunder