首页 > 解决方案 > 时尚商务的 Elasticsearch

问题描述

我在弹性搜索中索引了以下数据集

[
{
    "id": "1",
    "name": "Red T-shirt",
    "size": "S",
    "styleId": "R-1"
},
{
    "id": "2",
    "name": "Red T-shirt",
    "size": "M",
    "styleId": "R-1"
},
{
    "id": "3",
    "name": "Red T-shirt",
    "size": "L",
    "styleId": "R-1"
},
{
    "id": "4",
    "name": "White T-shirt",
    "size": "S",
    "styleId": "W-1"
},
{
    "id": "5",
    "name": "White T-shirt",
    "size": "XL",
    "styleId": "W-1"
}
]

在搜索结果中,我想要按 styleId 分组的产品。是否有可能像下面的示例结果?在亚马逊、Myntra、Flipkart 等大多数商业网站中,我看到他们正在做我正在尝试的事情。我尝试了许多弹性搜索概念,例如“折叠”,但没有成功。使用 kibana 和 elasticsearch 7.4 版。任何建议将不胜感激。

[
{
    "id": "1",
    "name": "Red T-shirt",
    "size": "S",
    "styleId": "R-1",
    "sizes": [
        {
            "id": "2",
            "name": "Red T-shirt",
            "size": "M",
            "styleId": "R-1"
        },
        {
            "id": "3",
            "name": "Red T-shirt",
            "size": "L",
            "styleId": "R-1"
        }
    ]
},
{
    "id": "4",
    "name": "White T-shirt",
    "size": "S",
    "styleId": "W-1",
    "sizes": [
        {
            "id": "5",
            "name": "White T-shirt",
            "size": "XL",
            "styleId": "W-1"
        }
    ]
},
]

标签: node.jselasticsearche-commerce

解决方案


您可以将折叠与基数聚合一起使用。 基数将给出组级别计数

{
  "_source": "false",
  "collapse": {
    "field": "styleId.keyword",
    "inner_hits": {
      "name": "group-details",
      "size": 5
      }
  },
  "from": 0,
  "size": 5,
  "aggs": {
    "type_count": {
      "cardinality": {
        "field": "styleId.keyword"
      }
    }
  }
}

推荐阅读