Вам необходимо изменить поле "challan_offences" с типа объекта на вложенный тип данных и использовать вложенное агрегирование .
Типы объектов сглаживаются, что является полем "challan_offences "будет храниться как
{
challan_offences.offence_name="<sometext>",
challan_offences.amount=100,
challan_offences.offence_name="<sometext>",
challan_offences.amount=4000,
challan_offences.offence_name="<sometext>",
challan_offences.amount=1200
}
, поэтому сумма агрегации показывает неправильные значения. При переходе к вложенному типу отношений корабль в offence_name и количество будет поддерживаться
Mapping
PUT index70
{
"mappings": {
"properties": {
"challan_offences":{
"type": "nested",
"properties": {
"offence_name":{
"type":"text",
"fields":{
"keyword":{
"type":"keyword"
}
}
},
"amount":{
"type":"integer"
}
}
}
}
}
}
Запрос:
{
"aggs": {
"challan_offences": {
"nested": {
"path": "challan_offences"
},
"aggs": {
"offence_name": {
"terms": {
"field": "challan_offences.offence_name.keyword"
},
"aggs": {
"amount": {
"sum": {
"field": "challan_offences.amount"
}
}
}
}
}
}
},
"size": 0,
"_source": {
"excludes": []
},
"stored_fields": [
"*"
],
"script_fields": {},
"query": {
"bool": {
"must": [],
"should": [],
"must_not": []
}
}
}
Результат:
"aggregations" : {
"challan_offences" : {
"doc_count" : 3,
"offence_name" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "Fail to produce Driving Licence (No DL) ",
"doc_count" : 1,
"amount" : {
"value" : 1200.0
}
},
{
"key" : "Fail to produce certificate of Fitness",
"doc_count" : 1,
"amount" : {
"value" : 4000.0
}
},
{
"key" : "Valid Insurance",
"doc_count" : 1,
"amount" : {
"value" : 100.0
}
}
]
}
}
}