2015-05-01 34 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" 
          } 
         } 
        ] 
       } 
       } 
      ] 
     } 
    } 
    } 
}