0

I'm looking to make a filter for ecommerce product filtering. Each request toES will have at least a must filter for the category match, and then optional filtering for attributes. Let's say Colour and Size. But, colour and size must both match one of the selected values. If the selected colours are blue and red, those are an OR, but if sizes M and L is selected, the results should return products that are either (blue OR red), AND size (M OR L). My filter as it is:

"query":{
  "filtered":{
     "filter":{
        "bool":{
           "must":[
              {
                 "term":{
                    "categories":4838
                 }
              },
              {
                 "bool":{
                    "should":[
                       {
                          "bool":{
                             "must":[
                                {
                                   "bool":{
                                      "should":[
                                         {
                                            "term":{
                                               "Colour":"White"
                                            }
                                         },
                                         {
                                            "term":{
                                               "Colour":"Black"
                                            }
                                         },
                                         {
                                            "term":{
                                               "Colour":"Blue"
                                            }
                                         }
                                      ]
                                   }
                                }
                             ]
                          }
                       },
                       {
                          "bool":{
                             "must":[
                                {
                                   "bool":{
                                      "should":[
                                         {
                                            "term":{
                                               "Size":"M"
                                            }
                                         },
                                         {
                                            "term":{
                                               "Size":"L"
                                            }
                                         }
                                      ]
                                   }
                                }
                             ]
                          }
                       }
                    ]
                 }
              }
           ]
        }
     }
  }
}

This filter returns shoulds for all of them. I get all products where the colour is white, black or blue, or where the size is M or L.

Summing it up how it should be:

in a single attribute it's all ORs and it expands the search, but when another attribute is selected, it becomes an AND between those attributes, but still ORs within them.

2 Answers 2

1

In addition to @Mave's answer, you can simplify your search request by using Terms filter instead of Term filter. It makes the request more readable too.

{
   "query": {
      "filtered": {
         "filter": {
            "bool": {
               "must": [
                  {
                     "term": {
                        "categories": 4838
                     }
                  },
                  {
                     "terms": {
                        "Colour": [
                           "White",
                           "Black",
                           "Pink"
                        ]
                     }
                  },
                  {
                     "terms": {
                        "Size": [
                           "M",
                           "L"
                        ]
                     }
                  }
               ]
            }
         }
      }
   }
}
Sign up to request clarification or add additional context in comments.

1 Comment

My own answer fixed my problem, but yours is neater.. I'm in a pickle! ;)
0

Of course the attributes had to be in their must clauses, resulting in:

"query":{
  "filtered":{
     "filter":{
        "bool":{
           "must":[
              {
                 "term":{
                    "categories":4838
                 }
              },
              {
                 "bool":{
                    "should":[
                       {
                          "term":{
                             "Colour":"White"
                          }
                       },
                       {
                          "term":{
                             "Colour":"Black"
                          }
                       },
                       {
                          "term":{
                             "Colour":"Pink"
                          }
                       }
                    ]
                 }
              },
              {
                 "bool":{
                    "should":[
                       {
                          "term":{
                             "Size":"M"
                          }
                       },
                       {
                          "term":{
                             "Size":"L"
                          }
                       }
                    ]
                 }
              }
           ]
        }
     }
  }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.