Теперь понятно.
Надо убрать nested
из sizeData
и elasticSizeScale
(они у вас в единственном экземпляре, там nested
не нужен. А вот с size
все наоборот - там, как раз, nested
необходим. Если это сделать то получится так:
DELETE test
PUT test
{
"mappings": {
"properties": {
"sizeData": {
"properties": {
"elasticSizeScale": {
"properties": {
"id": {
"type": "long"
},
"name": {
"properties": {
"en": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"ru": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
},
"sizes": {
"type": "nested",
"properties": {
"id": {
"type": "long"
},
"name": {
"properties": {
"en": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"ru": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
},
"sizeOrder": {
"type": "long"
}
}
}
}
},
"id": {
"type": "long"
},
"identifier": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"name": {
"properties": {
"en": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"ru": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}
}
}
}
}
PUT test/_doc/1
{
"sizeData": {
"id": 1,
"identifier": "group_size_shoes_mn",
"name": {
"ru": "Размер мужской обуви",
"en": "Men's shoe sizes"
},
"elasticSizeScale": {
"id": 3,
"name": {
"ru": "uk",
"en": "uk"
},
"sizes": [
{
"id": 321,
"name": {
"ru": "6",
"en": "6"
},
"sizeOrder": 2
},
{
"id": 323,
"name": {
"ru": "7",
"en": "7"
},
"sizeOrder": 4
},
{
"id": 322,
"name": {
"ru": "6.5",
"en": "6.5"
},
"sizeOrder": 3
},
{
"id": 317,
"name": {
"ru": "13.5",
"en": "13.5"
},
"sizeOrder": 17
},
{
"id": 318,
"name": {
"ru": "14",
"en": "14"
},
"sizeOrder": 18
},
{
"id": 315,
"name": {
"ru": "12.5",
"en": "12.5"
},
"sizeOrder": 15
},
{
"id": 316,
"name": {
"ru": "13",
"en": "13"
},
"sizeOrder": 16
},
{
"id": 320,
"name": {
"ru": "5.5",
"en": "5.5"
},
"sizeOrder": 1
},
{
"id": 310,
"name": {
"ru": "10",
"en": "10"
},
"sizeOrder": 10
},
{
"id": 328,
"name": {
"ru": "9.5",
"en": "9.5"
},
"sizeOrder": 9
},
{
"id": 324,
"name": {
"ru": "7.5",
"en": "7.5"
},
"sizeOrder": 5
},
{
"id": 325,
"name": {
"ru": "8",
"en": "8"
},
"sizeOrder": 6
},
{
"id": 314,
"name": {
"ru": "12",
"en": "12"
},
"sizeOrder": 14
},
{
"id": 327,
"name": {
"ru": "9",
"en": "9"
},
"sizeOrder": 8
},
{
"id": 312,
"name": {
"ru": "11",
"en": "11"
},
"sizeOrder": 12
},
{
"id": 313,
"name": {
"ru": "11.5",
"en": "11.5"
},
"sizeOrder": 13
},
{
"id": 326,
"name": {
"ru": "8.5",
"en": "8.5"
},
"sizeOrder": 7
}
]
}
}
}
POST test/_search
{
"size": 0,
"aggs": {
"by_size": {
"nested": {
"path": "sizeData.elasticSizeScale.sizes"
},
"aggs": {
"by_id": {
"terms": {
"script": {
"source": "doc['sizeData.elasticSizeScale.sizes.id'][0] + ' - ' + doc['sizeData.elasticSizeScale.sizes.name.ru.keyword'][0] + ' - ' + doc['sizeData.elasticSizeScale.sizes.sizeOrder'][0]"
},
"size": 10,
"order": {
"by_size": "asc"
}
},
"aggs": {
"by_size": {
"max": {
"field": "sizeData.elasticSizeScale.sizes.sizeOrder"
}
}
}
}
}
}
}
}
В 7.12 мы добавили новую агрегацию, для таких случаев:
POST test/_search
{
"size": 0,
"aggs": {
"by_size": {
"nested": {
"path": "sizeData.elasticSizeScale.sizes"
},
"aggs": {
"by_id": {
"multi_terms": {
"terms": [{
"field" : "sizeData.elasticSizeScale.sizes.id"
}, {
"field" : "sizeData.elasticSizeScale.sizes.name.ru.keyword"
}, {
"field" : "sizeData.elasticSizeScale.sizes.sizeOrder"
}],
"size": 10,
"order": {
"by_size": "asc"
}
},
"aggs": {
"by_size": {
"max": {
"field": "sizeData.elasticSizeScale.sizes.sizeOrder"
}
}
}
}
}
}
}
}