Search code examples
elasticsearchelasticafoselasticabundle

Elasticsearch Filtered Bool Query


I am running into some serious Problems with a custom Search. All i want is a Wildcard Search in three Fields and the Result should to filtered by another field. In Elastica it results in this Query:

{
   "bool":{
      "should":[
         {
            "wildcard":{
               "ean":"*180g*"
            }
         },
         {
            "wildcard":{
               "titel":"*180g*"
            }
         },
         {
            "wildcard":{
               "interpret":"*180g*"
            }
         }
      ],
      "filter":[
         {
            "term":{
               "genre":{
                  "value":"Rock",
                  "boost":1
               }
            }
         }
      ]
   }
}

Actually i can't find an error, but Elasticsearch does not give me Filtered Results. What happens? Elasticsearch returns ALL Items with the Filtered Term, either if the Boolean Shoulds are True or False. When i add the Filter as "Must" i am getting the same results? What is wrong here !?


Solution

  • You need to add "minimum_should_match": 1 in your bool query.

    {
       "bool":{
          "minimum_should_match": 1,
          "should":[
             {
                "wildcard":{
                   "ean":"*180g*"
                }
             },
             {
                "wildcard":{
                   "titel":"*180g*"
                }
             },
             {
                "wildcard":{
                   "interpret":"*180g*"
                }
             }
          ],
          "filter":[
             {
                "term":{
                   "genre":{
                      "value":"Rock",
                      "boost":1
                   }
                }
             }
          ]
       }
    }