Elastic Search 聚合如何在电子商务网站中更新

How does Elastic Search aggregations get updated in ecommerce sites

电子商务网站中有多个聚合,如果用户 select 一个过滤器,其他聚合如何更新?是为每个聚合发送单个请求还是单个请求将处理所有聚合更新。

示例:

品牌:
abc(100)
xyz(50)

颜色:
红色(110)
白色(40)

尺寸:
中(60)
小(40)
大(50)

如果用户 selects 'red' 和 'medium' 是否会发送 'Color' 聚合和 'Size' 聚合的个人请求? 解释这在实时电子商务网站中是如何发生的

假设选择了品牌 "brand1" 和 "brand2",选择了颜色 "color1" 和 "color2",选择了尺码 "size1" 和 "size2" .

结果只会显示满足以下所有条件的产品:

  • 其品牌为"brand1"或"brand2"
  • 它的颜色是 "color1" 或 "color2"
  • 它的大小是 "size1" 或 "size2"

类别品牌的聚合结果将在所有产品上进行过滤,以使每个产品都满足以下所有条件:

  • 它的颜色是 "color1" 或 "color2"
  • 它的大小是 "size1" 或 "size2"

颜色和尺码类别的情况类似。

考虑到所有这些因素,我们可以创建单个 Elasticsearch 查询请求来提供所有结果以及所有聚合(见下文)。

POST _search
{
   "filter": {
      "bool": {
         "must": [
            {
               "terms": {
                  "brand": [
                     "brand1",
                     "brand2"
                  ]
               }
            },
            {
               "terms": {
                  "color": [
                     "color1",
                     "color2"
                  ]
               }
            },
            {
               "terms": {
                  "size": [
                     "size1",
                     "size2"
                  ]
               }
            }
         ]
      }
   },
   "aggs": {
      "filtered_brand_aggs": {
         "filter": {
            "bool": {
               "must": [
                  {
                     "terms": {
                        "color": [
                           "color1",
                           "color2"
                        ]
                     }
                  },
                  {
                     "terms": {
                        "size": [
                           "size1",
                           "size2"
                        ]
                     }
                  }
               ]
            }
         },
         "aggs": {
            "brand_aggs": {
               "terms": {
                  "field": "brand"
               }
            }
         }
      },
      "filtered_color_aggs": {
         "filter": {
            "bool": {
               "must": [
                  {
                     "terms": {
                        "brand": [
                           "brand1",
                           "brand2"
                        ]
                     }
                  },
                  {
                     "terms": {
                        "size": [
                           "size1",
                           "size2"
                        ]
                     }
                  }
               ]
            }
         },
         "aggs": {
            "color_aggs": {
               "terms": {
                  "field": "color"
               }
            }
         }
      },
      "filtered_size_aggs": {
         "filter": {
            "bool": {
               "must": [
                  {
                     "terms": {
                        "color": [
                           "color1",
                           "color2"
                        ]
                     }
                  },
                  {
                     "terms": {
                        "brand": [
                           "brand1",
                           "brand2"
                        ]
                     }
                  }
               ]
            }
         },
         "aggs": {
            "size_aggs": {
               "terms": {
                  "field": "size"
               }
            }
         }
      }
   }
}

这是最普遍的查询。当然,必须根据过滤器选择修改此查询。如果说没有选择品牌过滤器,品牌字段的所有 terms 过滤器都将消失。其他领域也是如此。如果没有选择过滤器,您基本上会得到以下查询:

POST _search
{
   "aggs": {
      "brand_aggs": {
         "terms": {
            "field": "brand"
         }
      },
      "color_aggs": {
         "terms": {
            "field": "color"
         }
      },
      "size_aggs": {
         "terms": {
            "field": "size"
         }
      }
   }
}

因此,每次过滤器选择更改时,都会触发新查询并更新结果和聚合。