我正在尝试执行以下查询,但获取类型为 [top_hits] 的聚合器 [recommendationIds] 无法接受子聚合 下面是查询和相应的 Java 代码,我正在使用 Java Rest 高级客户端进行弹性搜索客户端,我已经尝试了多次,通过在几个地方放置顶级聚合,但它不起作用。
{
"query": {
"bool": {
"must_not": [
{
"terms": {
"id.keyword": [
""
],
"boost": 1
}
}
],
"should": [
{
"terms": {
"shipmentId.keyword": [
"123"
],
"boost": 1
}
},
{
"term": {
"exceptionId.keyword": {
"value": [
"1",
"2"
],
"boost": 1
}
}
}
],
"adjust_pure_negative": true,
"minimum_should_match": "1",
"boost": 1
}
},
"aggregations": {
"recommendationIds": {
"top_hits": {
"from": 0,
"size": 1,
"version": false,
"explain": false,
"_source": {
"includes": [
"id"
],
"excludes": [
""
]
}
},
"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"
}
]
},
"aggregations": {
"quantity": {
"sum": {
"field": "transferQuantity"
}
},
"transfercost": {
"sum": {
"field": "transferCost"
}
},
"revenueRecovered": {
"sum": {
"field": "revenueRecovered"
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
下面是生成查询的 java 代码:
List<String> excludeRecommendationIds = Stream.of("")
.collect(Collectors.toList());
BoolQueryBuilder mustNotMatchRecommendationIds =
QueryBuilders.boolQuery()
.should(QueryBuilders.termsQuery("shipmentId.keyword", asList("123")))
.should(QueryBuilders.termQuery("exceptionId.keyword", asList("1", "2")))
.mustNot(QueryBuilders.termsQuery("id.keyword", excludeRecommendationIds))
.minimumShouldMatch(1);
TopHitsAggregationBuilder topHitsAggregationBuilder = AggregationBuilders
.topHits("recommendationIds")
.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(
quantityAgg).subAggregation(transferCostAgg).subAggregation(revenueRecoveredAgg))))));
topHitsAggregationBuilder.subAggregation(aggregation);
//searchSourceBuilder.size(0);
searchSourceBuilder.query(mustNotMatchRecommendationIds);
searchSourceBuilder.aggregation(topHitsAggregationBuilder);
searchRequest.source(searchSourceBuilder);
错误是不言自明的。由于您的热门歌曲仅用于检索id
,因此改用terms
agg怎么样?那个可以接受子 aggs:
...
"aggregations": {
"recommendationIds": {
"terms": {
"field": "id.keyword",
"size": 10
},
"aggregations": {
"by_item": {
"terms": {
"field": "item
...