[sum] 类型的聚合器不能接受子聚合

Aggregator of type [sum] cannot accept sub-aggregations

我正在使用 rest 高级客户端并得到上述异常,但邮递员工作正常这里是 java 源代码,这是使用 rest 高级客户端构建的。

怀疑最后三个总和聚合导致了问题,如果 java 代码查询构造中的顺序不正确,请纠正我。

AggregationBuilder recommendationDataAgg = AggregationBuilders.nested("recommendations", "events.recommendationData");

AggregationBuilder exceptionIdAgg = AggregationBuilders
        .filter("exceptionId",
                QueryBuilders.termsQuery("events.recommendationData.exceptionId", exactMatchThese));

AggregationBuilder itemAgg = AggregationBuilders.terms("by_item").field("events.recommendationData.item.keyword");
AggregationBuilder destinationAgg = AggregationBuilders.terms("by_destination").field("events.recommendationData.location.keyword");
AggregationBuilder recommendationsAgg = AggregationBuilders.nested("recommendations", "events.recommendationData.recommendations");
AggregationBuilder transportAgg = AggregationBuilders.terms("by_trans").field("events.recommendationData.recommendations.stockTransfer.transportMode.keyword");
AggregationBuilder sourceAgg = AggregationBuilders.terms("by_sourcelocation").field("events.recommendationData.recommendations.stockTransfer.sourceLocation.keyword");
AggregationBuilder shipDateAgg = AggregationBuilders.terms("by_shipdate").field("events.recommendationData.recommendations.stockTransfer.shipDate.keyword");
AggregationBuilder arrivalDateAgg = AggregationBuilders.terms("by_arrival").field("events.recommendationData.recommendations.stockTransfer.arrivalDate.keyword");
AggregationBuilder quantityAgg = AggregationBuilders.sum("quantity").field("events.recommendationData.recommendations.stockTransfer.transferQuantity");
AggregationBuilder transfercostAgg = AggregationBuilders.sum("transfercost").field("events.recommendationData.recommendations.stockTransfer.transferCost");
AggregationBuilder revenueAgg = AggregationBuilders.sum("revenueRecovered").field("events.recommendationData.recommendations.stockTransfer.revenueRecovered");
arrivalDateAgg =  recommendationDataAgg.subAggregation(
        exceptionIdAgg.subAggregation(itemAgg.subAggregation(destinationAgg.subAggregation(recommendationsAgg
                .subAggregation(transportAgg.subAggregation(
                        sourceAgg.subAggregation(shipDateAgg.subAggregation(arrivalDateAgg))))))));

AggregationBuilder aggregation = arrivalDateAgg
        .subAggregation(quantityAgg)
        .subAggregation(transfercostAgg)
        .subAggregation(revenueAgg);

searchSourceBuilder.aggregation(aggregation);

searchRequest.source(searchSourceBuilder);

这是来自邮递员的有效查询

{
    "aggs": {
        "recommendations": {
            "nested": {
                "path": "events.recommendationData"
            },
            "aggs": {
                "exception": {
                    "filter": {
                        "terms": {
                            "events.recommendationData.exceptionId": [
                                "1"
                            ]
                        }
                    },
                    "aggs": {
                        "by_item": {
                            "terms": {
                                "field": "events.recommendationData.item.keyword"
                            },
                            "aggs": {
                                "by_destination": {
                                    "terms": {
                                        "field": "events.recommendationData.location.keyword"
                                    },
                                    "aggs": {
                                        "recommendations": {
                                            "nested": {
                                                "path": "events.recommendationData.recommendations"
                                            },
                                            "aggs": {
                                                "by_trans": {
                                                    "terms": {
                                                        "field": "events.recommendationData.recommendations.stockTransfer.transportMode.keyword"
                                                    },
                                                    "aggs": {
                                                        "by_sourcelocation": {
                                                            "terms": {
                                                                "field": "events.recommendationData.recommendations.stockTransfer.sourceLocation.keyword"
                                                            },
                                                            "aggs": {
                                                                "by_shipdate": {
                                                                    "terms": {
                                                                        "field": "events.recommendationData.recommendations.stockTransfer.shipDate.keyword"
                                                                    },
                                                                    "aggs": {
                                                                        "by_arrival": {
                                                                            "terms": {
                                                                                "field": "events.recommendationData.recommendations.stockTransfer.arrivalDate.keyword"
                                                                            },
                                                                            "aggs": {
                                                                                "quantity": {
                                                                                    "sum": {
                                                                                        "field": "events.recommendationData.recommendations.stockTransfer.transferQuantity"
                                                                                    }
                                                                                },
                                                                                "transferCost": {
                                                                                    "sum": {
                                                                                        "field": "events.recommendationData.recommendations.stockTransfer.transferCost"
                                                                                    }
                                                                                },
                                                                                "revenueRecovered": {
                                                                                    "sum": {
                                                                                        "field": "events.recommendationData.recommendations.stockTransfer.revenueRecovered"
                                                                                    }
                                                                                }
                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

这是映射

{
    "mappings": {
        "recommendations": {
            "properties": {
                "events": {
                    "type": "nested",
                    "properties": {
                        "recommendationData": {
                            "type": "nested",
                            "properties": {
                                "recommendations": {
                                    "type": "nested",
                                    "properties": {
                                        "recommendationType": {
                                            "type": "keyword"
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

这是文档

{
    "clusterId": "1",
    "rank": 1,
    "events": [
        {
            "eventId": "2",
            "eventType": "Delayed",
            "metaInfo": {
                "batch_id": "batch_1"
            },
            "recommendationData": [
                {
                    "exceptionId": "1",
                    "item": "KitKat",
                    "location": "DC1",
                    "dueDate": "2019-01-10T05:30:00.000+0530",
                    "quantity": 100,
                    "metaInfo": {
                        "batch_id": "batch_1",
                        "dummy_id": "dummy_1"
                    },
                    "rank": 1,
                    "recommendations": [
                        {
                            "rank": 1,
                            "recommendationType": "Out Of Stock",
                            "customerName": "Walmart",
                            "stockTransfer": {
                                "primaryRecommendation": true,
                                "priority": 1,
                                "sourceLocation": "DC1",
                                "transferQuantity": 100,
                                "metaInfo": 40,
                                "shipDate": "01/01/2020",
                                "arrivalDate": "10/01/2020",
                                "transportMode": "Air",
                                "transferCost": 1000,
                                "maxQtyAvailableForTransfer": 40,
                                "totalQtyAtSource": 1,
                                "revenueRecovered": 12000
                            },
                            "expedite": null
                        },
                        {
                            "rank": 1,
                            "recommendationType": "Out Of Stock",
                            "customerName": "Walmart",
                            "stockTransfer": {
                                "primaryRecommendation": true,
                                "priority": 1,
                                "sourceLocation": "DC1",
                                "transferQuantity": 100,
                                "metaInfo": 40,
                                "shipDate": "01/01/2020",
                                "arrivalDate": "10/01/2020",
                                "transportMode": "Air",
                                "transferCost": 1000,
                                "maxQtyAvailableForTransfer": 40,
                                "totalQtyAtSource": 1,
                                "revenueRecovered": 12000
                            },
                            "expedite": null
                        },
                        {
                            "rank": 1,
                            "recommendationType": "Out Of Stock",
                            "customerName": "Walmart",
                            "stockTransfer": {
                                "primaryRecommendation": true,
                                "priority": 1,
                                "sourceLocation": "DC1",
                                "transferQuantity": 100,
                                "metaInfo": 40,
                                "shipDate": "01/01/2020",
                                "arrivalDate": "10/01/2020",
                                "transportMode": "Air",
                                "transferCost": 1000,
                                "maxQtyAvailableForTransfer": 40,
                                "totalQtyAtSource": 1,
                                "revenueRecovered": 12000
                            },

                            "expedite": null
                        },  {
                            "rank": 1,
                            "recommendationType": "Out Of Stock",
                            "customerName": "Walmart",
                            "stockTransfer": {
                                "primaryRecommendation": true,
                                "priority": 1,
                                "sourceLocation": "DC1",
                                "transferQuantity": 100,
                                "metaInfo": 40,
                                "shipDate": "01/01/2020",
                                "arrivalDate": "10/01/2020",
                                "transportMode": "Road",
                                "transferCost": 1000,
                                "maxQtyAvailableForTransfer": 40,
                                "totalQtyAtSource": 1,
                                "revenueRecovered": 12000
                            },
                            "expedite": null
                        },
                        {
                            "rank": 1,
                            "recommendationType": "Out Of Stock",
                            "customerName": "Walmart",
                            "stockTransfer": {
                                "primaryRecommendation": true,
                                "priority": 1,
                                "sourceLocation": "DC1",
                                "transferQuantity": 100,
                                "metaInfo": 40,
                                "shipDate": "01/01/2020",
                                "arrivalDate": "10/01/2020",
                                "transportMode": "Rail",
                                "transferCost": 1000,
                                "maxQtyAvailableForTransfer": 40,
                                "totalQtyAtSource": 1,
                                "revenueRecovered": 12000
                            },
                            "expedite": null
                        }
                    ]
                }
            ]
        }
    ]
}

的确,最后三个总和聚合没有正确添加。您需要将它们全部添加到 arrivalDateAgg 聚合

...
arrivalDateAgg
   .subAggregation(quantityAgg)
   .subAggregation(transfercostAgg)
   .subAggregation(revenueAgg)