elasticsearch 使用 resthighlevel 客户端生成查询

elasticsearch generate query using resthighlevel client

我必须使用 rest 高级客户端生成以下查询,我已经尝试了以下查询,但它无法形成查询,因为下面想知道它在我当前的聚合代码中哪里出了问题 top hits should出现在总和聚合之前,而且我正在使用其他高级客户端连接到弹性搜索。

必填查询:

{
    "aggregations": {
        "by_destination": {
            "terms": {
                "field": "destinationLocation.keyword",
                "size": 10,
                "min_doc_count": 1,
                "shard_min_doc_count": 0,
                "show_term_doc_count_error": false,
                "order": [
                    {
                        "_count": "desc"
                    },
                    {
                        "_key": "asc"
                    }
                ]
            },
            "aggregations": {
                "by_trans": {
                    "terms": {
                        "field": "transportMode.keyword",
                        "size": 10,
                        "min_doc_count": 1,
                        "shard_min_doc_count": 0,
                        "show_term_doc_count_error": false,
                        "order": [
                            {
                                "_count": "desc"
                            },
                            {
                                "_key": "asc"
                            }
                        ]
                    },
                    "aggregations": {
                        "by_sourcelocation": {
                            "terms": {
                                "field": "sourceLocation.keyword",
                                "size": 10,
                                "min_doc_count": 1,
                                "shard_min_doc_count": 0,
                                "show_term_doc_count_error": false,
                                "order": [
                                    {
                                        "_count": "desc"
                                    },
                                    {
                                        "_key": "asc"
                                    }
                                ]
                            },
                            "aggregations": {
                                "by_shipdate": {
                                    "terms": {
                                        "field": "shipDate",
                                        "size": 10,
                                        "min_doc_count": 1,
                                        "shard_min_doc_count": 0,
                                        "show_term_doc_count_error": false,
                                        "order": [
                                            {
                                                "_count": "desc"
                                            },
                                            {
                                                "_key": "asc"
                                            }
                                        ]
                                    },
                                    "aggregations": {
                                        "by_arrival": {
                                            "terms": {
                                                "field": "arrivalDate",
                                                "size": 10,
                                                "min_doc_count": 1,
                                                "shard_min_doc_count": 0,
                                                "show_term_doc_count_error": false,
                                                "order": [
                                                    {
                                                        "_count": "desc"
                                                    },
                                                    {
                                                        "_key": "asc"
                                                    }
                                                ]
                                            },
                                            "aggregations": {
                                                "docs": {
                                                    "top_hits": {
                                                        "size": 10,
                                                        "_source": [
                                                            "id"
                                                        ]
                                                    }
                                                },
                                                "quantity": {
                                                    "sum": {
                                                        "field": "transferQuantity"
                                                    }
                                                },
                                                "transfercost": {
                                                    "sum": {
                                                        "field": "transferCost"
                                                    }
                                                },
                                                "revenueRecovered": {
                                                    "sum": {
                                                        "field": "revenueRecovered"
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

代码:

TopHitsAggregationBuilder topHitsAggregationBuilder = AggregationBuilders.topHits("top").fetchSource("id", "").size(1);

    AggregationBuilder itemAgg = AggregationBuilders.terms("by_item").field("item.keyword");

    AggregationBuilder destinationAgg = AggregationBuilders.terms("by_destination").field("destinationLocation.keyword");

    AggregationBuilder transportAgg = AggregationBuilders.terms("by_trans").field("transportMode.keyword");

    AggregationBuilder sourceAgg = AggregationBuilders.terms("by_sourcelocation").field("sourceLocation.keyword");

    AggregationBuilder shipDateAgg = AggregationBuilders.terms("by_shipdate").field("shipDate");

    AggregationBuilder arrivalDateAgg = AggregationBuilders.terms("by_arrival").field("arrivalDate");

    AggregationBuilder quantityAgg = AggregationBuilders.sum("quantity").field("transferQuantity");
    AggregationBuilder transferCostAgg = AggregationBuilders.sum("transfercost").field("transferCost");
    AggregationBuilder revenueRecoveredAgg = AggregationBuilders.sum("revenueRecovered").field("revenueRecovered");


    AggregationBuilder aggregation = itemAgg.subAggregation(destinationAgg
            .subAggregation(transportAgg.subAggregation(sourceAgg.subAggregation(shipDateAgg.subAggregation(arrivalDateAgg)
                    .subAggregation(topHitsAggregationBuilder).subAggregation(quantityAgg)
                    .subAggregation(transferCostAgg).subAggregation(revenueRecoveredAgg)))));

    //searchSourceBuilder.size(0);
    searchSourceBuilder.aggregation(aggregation);
    searchRequest.source(searchSourceBuilder);

实际发电量为:

 {
    "aggregations": {
        "by_item": {
            "terms": {
                "field": "item.keyword",
                "size": 10,
                "min_doc_count": 1,
                "shard_min_doc_count": 0,
                "show_term_doc_count_error": false,
                "order": [
                    {
                        "_count": "desc"
                    },
                    {
                        "_key": "asc"
                    }
                ]
            },
            "aggregations": {
                "by_destination": {
                    "terms": {
                        "field": "destinationLocation.keyword",
                        "size": 10,
                        "min_doc_count": 1,
                        "shard_min_doc_count": 0,
                        "show_term_doc_count_error": false,
                        "order": [
                            {
                                "_count": "desc"
                            },
                            {
                                "_key": "asc"
                            }
                        ]
                    },
                    "aggregations": {
                        "by_trans": {
                            "terms": {
                                "field": "transportMode.keyword",
                                "size": 10,
                                "min_doc_count": 1,
                                "shard_min_doc_count": 0,
                                "show_term_doc_count_error": false,
                                "order": [
                                    {
                                        "_count": "desc"
                                    },
                                    {
                                        "_key": "asc"
                                    }
                                ]
                            },
                            "aggregations": {
                                "by_sourcelocation": {
                                    "terms": {
                                        "field": "sourceLocation.keyword",
                                        "size": 10,
                                        "min_doc_count": 1,
                                        "shard_min_doc_count": 0,
                                        "show_term_doc_count_error": false,
                                        "order": [
                                            {
                                                "_count": "desc"
                                            },
                                            {
                                                "_key": "asc"
                                            }
                                        ]
                                    },
                                    "aggregations": {
                                        "by_shipdate": {
                                            "terms": {
                                                "field": "shipDate",
                                                "size": 10,
                                                "min_doc_count": 1,
                                                "shard_min_doc_count": 0,
                                                "show_term_doc_count_error": false,
                                                "order": [
                                                    {
                                                        "_count": "desc"
                                                    },
                                                    {
                                                        "_key": "asc"
                                                    }
                                                ]
                                            },
                                            "aggregations": {
                                                "by_arrival": {
                                                    "terms": {
                                                        "field": "arrivalDate",
                                                        "size": 10,
                                                        "min_doc_count": 1,
                                                        "shard_min_doc_count": 0,
                                                        "show_term_doc_count_error": false,
                                                        "order": [
                                                            {
                                                                "_count": "desc"
                                                            },
                                                            {
                                                                "_key": "asc"
                                                            }
                                                        ]
                                                    }
                                                },
                                                "top": {
                                                    "top_hits": {
                                                        "from": 0,
                                                        "size": 1,
                                                        "version": false,
                                                        "explain": false,
                                                        "_source": {
                                                            "includes": [
                                                                "id"
                                                            ],
                                                            "excludes": [
                                                                ""
                                                            ]
                                                        }
                                                    }
                                                },
                                                "quantity": {
                                                    "sum": {
                                                        "field": "transferQuantity"
                                                    }
                                                },
                                                "transfercost": {
                                                    "sum": {
                                                        "field": "transferCost"
                                                    }
                                                },
                                                "revenueRecovered": {
                                                    "sum": {
                                                        "field": "revenueRecovered"
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

Top_hits 聚合不接受子聚合。所以解决方案是改变 以下查询

.subAggregation(topHitsAggregationBuilder.subAggregation(quantityAgg) 

.subAggregation(topHitsAggregationBuilder).subAggregation(quantityAgg)