你需要改变你的映射,使
productAttributeFields一个
nested字段,以便您可以保留之间的关联
productAttributeFields.name和
productAttributeFields.value。
映射应如下所示:
{ "mappings": { "product": { "properties": { "name": { "type": "string" }, "categoryName": { "type": "string", "index": "not_analyzed" }, "priceBrutto": { "type": "float" }, "categoryCode": { "type": "integer" }, "productAttributeFields": { "type": "nested", "include_in_parent": true,"properties": { "name": { "index": "not_analyzed", "type": "string" }, "value": { "index": "not_analyzed", "type": "string" } } } } } }}然后查询更改为
{ "query": { "match": { "categoryCode": 123 } }, "aggs": { "attrs_root": { "nested": { "path": "productAttributeFields" }, "aggs": { "attrs": { "terms": { "field": "productAttributeFields.name" }, "aggs": { "attrsValues": { "terms": { "field": "productAttributeFields.value", "size": 100 } } } } } } }}


