GOAL: Get the
values
associated with thecharacteristic
named"ram"
Example Object/Schema:
Showcase Path: product{}.characteristics[].values[]
Product {
"characteristics": [ //1st level
{ //1st level object
"name": "ram",
"values": [ //2nd level
{"value": 2}, //to be returned in aggregation
{"value": 4} //to be returned in aggregation
]
}
]
}
Document:
{
"_index": "product",
"_type": "_doc",
"_id": "18",
"_score": 1.0,
"_source": {
"doc": {
"id": 18,
"name": "iphone_11",
"localizedName": [
{
"locale": "en-US",
"value": "iPhone 11"
}
],
"productType": null,
"shops": [
],
"characteristics": [
{
"name": "ram",
"values": [
{
"value": "2",
"localizedValues": [
]
},
{
"value": "4",
"localizedValues": [
]
}
],
"localizedName": [
{
"id": 15,
"locale": "en-US",
"value": "Ram"
}
]
}
]
}
}
}
Mappings:
{
"product": {
"mappings": {
"properties": {
"doc": {
"properties": {
"characteristics": {
"type": "nested",
"properties": {
"localizedName": {
"properties": {
"id": {
"type": "long"
},
"locale": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"value": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
},
"name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"values": {
"type": "nested",
"properties": {
"value": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}
},
"id": {
"type": "long"
},
"name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}
}
}
}
Current aggregation traversal:
{
"size": 0,
"aggs": {
"characteristics": {
"nested": {
"path": "characteristics"
},
"aggs": {
"ram": {
"filter": {
"bool": {
"must": [
{
"term": {
"characteristics.name": "ram"
}
}
]
}
},
"aggs": {
"values": {
"nested": {
"path": "characteristics.values"
},
"aggs": {
"all_values": {
"terms": {
"field": "characteristics.values.value"
}
}
}
}
}
}
}
}
}
}
Response:
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 0,
"relation": "eq"
},
"max_score": null,
"hits": []
},
"aggregations": {
"characteristics": {
"meta": {},
"doc_count": 0,
"ram": {
"meta": {},
"doc_count": 0,
"values": {
"doc_count": 0,
"all_values": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": []
}
}
}
}
}
}
CodePudding user response:
This seems to work in Elasticsearch 7.15 (based on your second sample):
{
"size": 0,
"aggs": {
"characteristics": {
"nested": {
"path": "characteristics"
},
"aggs": {
"ram": {
"filter": {
"bool": {
"must": [
{
"term": {
"characteristics.name": "ram"
}
}
]
}
},
"aggs": {
"values": {
"nested": {
"path": "characteristics.values"
},
"aggs": {
"all_values": {
"terms": {
"field": "characteristics.values.value"
}
}
}
}
}
}
}
}
}
}
It's quite convoluted though so maybe it would make sense to create an additional "attribute values" index which would have a doc for each attribute value, containing also attribute name and product id/name? Then aggregation would get way simpler.