How to do nested boolean filters for finding exact matcing values on same field in elasticsearch?

423 Views Asked by At

My Index mapping is shown below:

curl -X PUT localhost:9200/testing/listings/_mapping -d '{
 "listings" : {
  "properties" : {
    "address" : {
       "properties": {
          "location": { "type" : "string",
                        "index" : "not_analyzed"
           }
        }
    },
    "suggest" : { 
         "type" : "completion", 
         "index_analyzer" : "simple",
         "search_analyzer" : "simple",
         "payloads" : true
    }                              
  }
 }
}'

I need to perform the action of following query in elasticsearch

SELECT * FROM LISTINGS WHERE (published = true AND inActive = false) AND (residentialKind = "Villa" OR residentialKind = "Apartment");

To execute above query action i used the following nested bool query in elasticsearch.

{"query":{
  "filtered": {
    "filter": {
        "bool":{
            "must":[
               {"match":{"published":true}},
               {"match":{"inActive":false}},
               {"bool":
                    {"should": [
                            {"term":{"residentialKind":"Villa"}},
                            {"term":{"residentialKind":"Apartment"}} 
                        ]
                    }
                }
             ]
         }
      }
   }
 }
}

Its giving following error

{
"error": "SearchPhaseExecutionException[Failed to execute phase [query], all shards failed; shardFailures {[B5SbtdlkQTGL0WU-dvg2yg][propgod][0]: SearchParseException[[propgod][0]: from[-1],size[-1]: Parse Failure [Failed to parse source [{\"query\":{\n\t\"filtered\": {\n    \t\"filter\": {\n     \t\t\"bool\":{\n      \t\t\t\"must\":[\n                   {\"match\":{\"published\":true}},\n                   {\"match\":{\"inActive\":false}},\n                   {\"bool\":\n                   \t\t{\"should\": [\n                        \t\t{\"term\":{\"residentialKind\":\"Villa\"}},\n                                {\"term\":{\"residentialKind\":\"Apartment\"}} \n                        \t]\n                       }\n                   }\n     \t\t\t]\n   \t\t\t}\n    \t}\n    }\n }\n}]]]; nested: QueryParsingException[[propgod] No filter registered for [match]]; }{[B5SbtdlkQTGL0WU-dvg2yg][propgod][1]: SearchParseException[[propgod][1]: from[-1],size[-1]: Parse Failure [Failed to parse source [{\"query\":{\n\t\"filtered\": {\n    \t\"filter\": {\n     \t\t\"bool\":{\n      \t\t\t\"must\":[\n                   {\"match\":{\"published\":true}},\n                   {\"match\":{\"inActive\":false}},\n                   {\"bool\":\n                   \t\t{\"should\": [\n                        \t\t{\"term\":{\"residentialKind\":\"Villa\"}},\n                                {\"term\":{\"residentialKind\":\"Apartment\"}} \n                        \t]\n                       }\n                   }\n     \t\t\t]\n   \t\t\t}\n    \t}\n    }\n }\n}]]]; nested: QueryParsingException[[propgod] No filter registered for [match]]; }{[B5SbtdlkQTGL0WU-dvg2yg][propgod][2]: SearchParseException[[propgod][2]: from[-1],size[-1]: Parse Failure [Failed to parse source [{\"query\":{\n\t\"filtered\": {\n    \t\"filter\": {\n     \t\t\"bool\":{\n      \t\t\t\"must\":[\n                   {\"match\":{\"published\":true}},\n                   {\"match\":{\"inActive\":false}},\n                   {\"bool\":\n                   \t\t{\"should\": [\n                        \t\t{\"term\":{\"residentialKind\":\"Villa\"}},\n                                {\"term\":{\"residentialKind\":\"Apartment\"}} \n                        \t]\n                       }\n                   }\n     \t\t\t]\n   \t\t\t}\n    \t}\n    }\n }\n}]]]; nested: QueryParsingException[[propgod] No filter registered for [match]]; }{[B5SbtdlkQTGL0WU-dvg2yg][propgod][3]: SearchParseException[[propgod][3]: from[-1],size[-1]: Parse Failure [Failed to parse source [{\"query\":{\n\t\"filtered\": {\n    \t\"filter\": {\n     \t\t\"bool\":{\n      \t\t\t\"must\":[\n                   {\"match\":{\"published\":true}},\n                   {\"match\":{\"inActive\":false}},\n                   {\"bool\":\n                   \t\t{\"should\": [\n                        \t\t{\"term\":{\"residentialKind\":\"Villa\"}},\n                                {\"term\":{\"residentialKind\":\"Apartment\"}} \n                        \t]\n                       }\n                   }\n     \t\t\t]\n   \t\t\t}\n    \t}\n    }\n }\n}]]]; nested: QueryParsingException[[propgod] No filter registered for [match]]; }{[B5SbtdlkQTGL0WU-dvg2yg][propgod][4]: SearchParseException[[propgod][4]: from[-1],size[-1]: Parse Failure [Failed to parse source [{\"query\":{\n\t\"filtered\": {\n    \t\"filter\": {\n     \t\t\"bool\":{\n      \t\t\t\"must\":[\n                   {\"match\":{\"published\":true}},\n                   {\"match\":{\"inActive\":false}},\n                   {\"bool\":\n                   \t\t{\"should\": [\n                        \t\t{\"term\":{\"residentialKind\":\"Villa\"}},\n                                {\"term\":{\"residentialKind\":\"Apartment\"}} \n                        \t]\n                       }\n                   }\n     \t\t\t]\n   \t\t\t}\n    \t}\n    }\n }\n}]]]; nested: QueryParsingException[[propgod] No filter registered for [match]]; }]",
"status": 400
}

So if i wrong with my query please help me to fix this query. Thanks in Advance.

1

There are 1 best solutions below

1
Sloan Ahrens On

"match" is a query, not a filter, so you're getting an error because you tried to use it as a filter. This should do what you want (assuming "residentialKind" isn't analyzed):

POST /test_index/_search
{
    "query": {
        "bool":{
            "must":[
               {"match":{"published":true}},
               {"match":{"inActive":false}},
               {"bool":
                    {"should": [
                            {"term":{"residentialKind":"Villa"}},
                            {"term":{"residentialKind":"Apartment"}} 
                        ]
                    }
                }
             ]
         }
    }
}

If "residentialKind" IS analyzed with something like the standard analyzer, this should work:

POST /test_index/_search
{
    "query": {
        "bool":{
            "must":[
               {"match":{"published":true}},
               {"match":{"inActive":false}},
               {"bool":
                    {"should": [
                            {"match":{"residentialKind":"Villa"}},
                            {"match":{"residentialKind":"Apartment"}} 
                        ]
                    }
                }
             ]
         }
    }
}

or even

POST /test_index/_search
{
    "query":{
      "filtered": {
        "filter": {
            "bool":{
                "must":[
                   {"term":{"published":true}},
                   {"term":{"inActive":false}},
                   {"bool":
                        {"should": [
                                {"term":{"residentialKind":"villa"}},
                                {"term":{"residentialKind":"apartment"}} 
                            ]
                        }
                    }
                 ]
             }
          }
       }
   }
}

Here is the code I used for testing:

http://sense.qbox.io/gist/bbed61ef297b058c92d9c7f1523479dfeb3c35b2