2015-05-01 33 views
0

我正在为电子商务产品过滤制作过滤器。每个请求toES将至少有一个必须筛选器的类别匹配,然后可选的属性过滤。比方说颜色和尺寸。但是,颜色和大小必须都匹配所选值之一。如果选择的颜色是蓝色和红色,则这些是“或”,但如果选择尺寸M和L,则结果应返回(蓝色或红色)和“大小”(M或L)的产品。我的过滤器,因为它是:在ElasticSearch中堆叠过滤器

"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" 
              } 
             } 
             ] 
            } 
           } 
          ] 
          } 
         } 
        ] 
       } 
       } 
      ] 
     } 
    } 
    } 
} 

此过滤器返回shoulds为他们所有。我得到的所有的产品,该颜色是白色,黑色或蓝色,或尺寸为M或L.

总结起来就应该是这样:

在一个单独的属性

这一切都OR和它扩展搜索,但是当选择另一个属性时,它将成为这些属性之间的AND,但仍然是OR。

回答

1

除了@ MAVE的回答,您可以通过使用Terms过滤器,而不是Term过滤器简化您的搜索请求。它使请求更具可读性。

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

是的,欢呼,工作就像一个魅力! – Mave

+0

@Mave谢谢!将它标记为答案,如果它解决了你的问题:) – bittusarkar

+0

我自己的答案解决了我的问题,但你的整洁。我在泡菜! ;) – Mave

0

当然的属性必须是在他们的必备条款,导致:

"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" 
          } 
         } 
        ] 
       } 
       } 
      ] 
     } 
    } 
    } 
}