ElasticSearch - 如何聚合(嵌套)产品属性
ElasticSearch - How to aggregate (nested) product attributes
我有一个非常简单的索引,具有以下映射:
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"
}
}
}
}
}
}
}
但是return下面的代码都是属性值。我想要的是按名称聚合的 return 属性。例如,性别 > [男、女]、颜色 > [Black/White、蓝、绿]、尺码 > [ 37, 38, 39 ] 等等...
我该怎么做?
我不确定如何将文本字段 w/o fielddata
设置为 true。这可能是一个 cluster-wide 设置或其他东西......我已经调整了映射以将 name/value 对也包含为 keywords
这样我们就可以正确地聚合它们:
...
"attributes": {
"type": "nested",
"properties": {
"name": {
"type": "text",
"fields": {
"kwd": { <--
"type": "keyword"
}
}
},
"value": {
"type": "text",
"fields": {
"kwd": { <--
"type": "keyword"
}
}
}
}
}
在re-syncing文档之后我们可以进行如下操作:
{
"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": {}
}
}
}
}
}
}
}
}
}
这将为我们提供每个 name
下的唯一 values
。
我有一个非常简单的索引,具有以下映射:
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"
}
}
}
}
}
}
}
但是return下面的代码都是属性值。我想要的是按名称聚合的 return 属性。例如,性别 > [男、女]、颜色 > [Black/White、蓝、绿]、尺码 > [ 37, 38, 39 ] 等等...
我该怎么做?
我不确定如何将文本字段 w/o fielddata
设置为 true。这可能是一个 cluster-wide 设置或其他东西......我已经调整了映射以将 name/value 对也包含为 keywords
这样我们就可以正确地聚合它们:
...
"attributes": {
"type": "nested",
"properties": {
"name": {
"type": "text",
"fields": {
"kwd": { <--
"type": "keyword"
}
}
},
"value": {
"type": "text",
"fields": {
"kwd": { <--
"type": "keyword"
}
}
}
}
}
在re-syncing文档之后我们可以进行如下操作:
{
"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": {}
}
}
}
}
}
}
}
}
}
这将为我们提供每个 name
下的唯一 values
。