如何聚合(嵌套)产品属性

ylamdve6  于 2021-06-14  发布在  ElasticSearch
关注(0)|答案(1)|浏览(282)

我有一个非常简单的索引,Map如下:

PUT /localindex-products
{
  "mappings": {
    "properties": {
      "name": { "type": "text" },
      "sku": { "type": "keyword" },
      "description": { "type": "text" },
      "contextual": {
        "properties": {
          "name": { "type": "text" },
          "value": { "type": "text" }
        }
      },
      "attributes": {
        "type": "nested",
        "properties": {
          "name": { "type": "text" },
          "value": { "type": "text" }
        }
      }
    }
  }
}

所有文档如下所示:

{
    "name" : "Shoes Adidas Falcon Male",
    "sku" : "TUD-0485-001-37",
    "description" : "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras ornare posuere dolor, in efficitur leo porttitor consequat. Donec consectetur urna leo, eget condimentum nisi aliquam ut. Phasellus gravida congue purus, non aliquam odio consequat et. Aenean in sollicitudin mi. Vivamus convallis erat sem, at egestas dui convallis non.",
    "contextual" : [
        {
            "name" : "Composition",
            "value" : "Upper: Textile and synthetic. Midsole: EVA. Sole: rubber, reinforced heel."
        },
        {
            "name" : "Warranty",
            "value" : "Against Manufacturing Defect."
        }
    ],
    "attributes" : [
        {
            "name" : "Gender",
            "value" : "Male"
        },
        {
            "name" : "Recommendation",
            "value" : "Walking"
        },
        {
            "name" : "Material",
            "value" : "Synthetic and Textile"
        },
        {
            "name" : "Color",
            "value" : "Black/White"
        },
        {
            "name" : "Size",
            "value" : "37"
        }
    ]
}

现在,我可以通过 attributes.values :

"aggs": {
    "attributes": {
      "nested": {
        "path": "attributes"
      }, 
      "aggs": {
        "values": {
          "terms": {
            "field": "attributes.value",
            "size": 10
          },
          "aggs": {
            "back_to_root": {
              "reverse_nested": {
                "path": "_source"
              }
            }
          }
        }
      }
    }
  }

但是下面的代码返回所有属性值。我想要的是返回按名称聚合的属性。例如,性别>[男,女],颜色>[黑/白,蓝,绿],大小>[37,38,39]等等。。。
我该怎么做?

2o7dmzc5

2o7dmzc51#

我不知道你怎么能在没有文本字段的情况下进行聚合 fielddata 设置为true。可能是一个集群范围内的设置或什么的。。。我已经调整了Map,将名称/值对作为 keywords 因此我们可以适当地对它们进行聚合:

...
"attributes": {
  "type": "nested",
  "properties": {
    "name": {
      "type": "text",
      "fields": {
        "kwd": {                <--
          "type": "keyword"
        }
      }
    },
    "value": {
      "type": "text",
      "fields": {
        "kwd": {                <--
          "type": "keyword"
        }
      }
    }
  }
}

重新同步文档后,我们可以按以下步骤进行:

{
  "size": 0,
  "aggs": {
    "attributes": {
      "nested": {
        "path": "attributes"
      },
      "aggs": {
        "names": {
          "terms": {
            "field": "attributes.name.kwd",
            "size": 10
          },
          "aggs": {
            "values": {
              "terms": {
                "field": "attributes.value.kwd",
                "size": 10
              },
              "aggs": {
                "back_to_root": {
                  "reverse_nested": {}
                }
              }
            }
          }
        }
      }
    }
  }
}

这将给我们带来独一无二的 values 在每个 name .

相关问题