Add exception/exclusion to must_not clause in elasticsearch


(Ekaitz Hernández Troyas) #1

I have this problem in elasticsearch, I would like to filter all products related to a specific brand "Brand1".
However I would like to have an exception and keep a specific type of product for that brand.
How can I do that?, for now I have this query which removes all products related to brand1.

POST brandindex/product/_search
{  
            "filter":{
                "bool": {
                    "should": [   
                      {"bool": {
                            "must_not":[ 
                             	{"term" : {"brand.raw" : "Brand1"}},  
                            ]
                         }
                      }
                    ]
                }
                
            }
        
    ,
    "size":3000
}  

Is it possible to do it without adding a new bool in the should array like the next query?

 POST brandindex/product/_search
{  
            "filter":{
                "bool": {
                    "should": [   
                      {"bool": {
                            "must_not":[ 
                             	{"term" : {"brand.raw" : "Brand1"}},  
                            ]
                         }
                      },
					  {"bool": {
                            "must":[ 
                             	{"term" : {"brand.raw" : "Brand1"}},  
							 	{"term" : {"type.raw" : "slippers"}}
                            ]
                         }
                      }
                    ]
                }
                
            }
        
    ,
    "size":3000
}               

Thanks


(system) #2