簡體   English   中英

在多個字段上進行ElasticSearch過濾(具有聚集)

[英]ElasticSearch filtering on multiple fields (with aggregration)

我正在為網上商店構建多面過濾功能,如下所示:

Filter on Brand:
[ ] LG (10)
[ ] Apple (5)
[ ] HTC (3)

Filter on OS:
[ ] Android 4 (11)
[ ] Android 5 (2)
[ ] IOS (5)

我在Elasticsearch中使用聚合和過濾,經過幾天學習ES(喜歡它!),這對我來說效果很好。 但是可悲的是,我現在停留在實際過濾上。

如果我單擊“ LG”,則將禁用IOS篩選器,並且(5)將更改為(0),並且右側的結果將更改為13個Android手機。 很好,到目前為止很好。

現在,如果我單擊“ Android 4”,則右側只會顯示11個電話。 真棒! 到現在為止還挺好 :)

但是現在,如果我單擊“ Android 5”,所有結果都會消失。 我不確定自己在做什么錯。 我希望所有配備Android 4和5的LG手機都能出現。

下面是最后一種情況的示例查詢。 請注意,查詢中還包含其他一些字段,這些字段用於構建分面過濾。

{
   "size":100,
   "query":{
      "filtered":{
         "query":{
            "match_all":[

            ]
         },
         "filter":{
            "bool":{
               "must":[
                  {
                     "term":{
                        "brand.untouched":"LG"
                     }
                  },
                  {
                     "term":{
                        "operating_system.untouched":"Android 4"
                     }
                  },
                  {
                     "term":{
                        "operating_system.untouched":"Android 5"
                     }
                  }
               ],
               "should":[

               ],
               "must_not":{
                  "missing":{
                     "field":"model"
                  }
               }
            }
         },
         "strategy":"query_first"
      }
   },
   "aggs":{
      "brand.untouched":{
         "terms":{
            "field":"brand.untouched"
         }
      },
      "operating_system.untouched":{
         "terms":{
            "field":"operating_system.untouched"
         }
      },
      "camera1":{
         "histogram":{
            "field":"camera1",
            "interval":5,
            "min_doc_count":0
         }
      },
      "price_seperate":{
         "histogram":{
            "field":"price_seperate",
            "interval":125,
            "min_doc_count":0
         }
      }
   }
}

有人知道解決方案嗎? 非常感謝。

您的查詢正在搜索的文檔中, operating_system.untouched既是“ Android 4”又是“ Android 5”,但事實並非如此,因此結果為零。 您可以簡單地使用Terms Filter以便operating_system.untouched的值與“ Android 4”或“ Android 5”匹配的文檔。 以下是您應該使用的更新查詢:

{
   "size":100,
   "query":{
      "filtered":{
         "filter":{
            "bool":{
               "must":[
                  {
                     "terms":{
                        "brand.untouched": [
                            "LG"
                        ]
                     }
                  },
                  {
                     "terms":{
                        "operating_system.untouched": [
                            "Android 4",
                            "Android 5"
                        ]
                     }
                  }
               ],
               "must_not":{
                  "missing":{
                     "field":"model"
                  }
               }
            }
         },
         "strategy":"query_first"
      }
   },
   "aggs":{
      "brand.untouched":{
         "terms":{
            "field":"brand.untouched"
         }
      },
      "operating_system.untouched":{
         "terms":{
            "field":"operating_system.untouched"
         }
      },
      "camera1":{
         "histogram":{
            "field":"camera1",
            "interval":5,
            "min_doc_count":0
         }
      },
      "price_seperate":{
         "histogram":{
            "field":"price_seperate",
            "interval":125,
            "min_doc_count":0
         }
      }
   }
}

如果要添加價格范圍之類的另一組類別,只需在bool must子句中添加bool should子句。 當您想按兩個范圍(0, 100](100, 200]的現場price進行過濾時,請參見下面的示例。這基本上意味着您可以嵌套mustshould過濾器以實現所需的任何布爾邏輯在Elasticsearch中實現過濾。

... 
"must":[
    {
        "terms":{
            "brand.untouched": [
                "LG"
            ]
        }
    },
    {
        "terms":{
            "operating_system.untouched": [
               "Android 4",
               "Android 5"
            ]
        }
    },
    "bool": {
        "should": [
            {
                "range": {
                    "price": {
                        "gt": 0,
                        "lte": 100
                    }
                }
            },
            {
                "range": {
                    "price": {
                        "gt": 100,
                        "lte": 200
                    }
                }
            }
        ]
    }
],
...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM