tophits JAVA API 的 Elasticsearch 子聚合不起作用

Elasticsearch Subaggregation of tophits JAVA API not working

我想在 JAVA API 中编写 elasticsearch 聚合代码以查找字段折叠和结果分组。

json聚合代码如下所示 我从 elasticsearch docs

那里得到了这些代码

'dedup_by_score' 聚合具有称为 'top_hit' 聚合的子聚合 并将其用于桶排序的术语聚合。

... some query
  "aggs": {
    "dedup_by_score": {
      "terms": {
        "field": "keyword",
        "order": {
          "top_hit": "desc"
        },
        "size": 10
      },
      "aggs": {
        "top_hit": {
          "max": {
            "script": {
              "source": "_score"
            }
          }
        }
      }
    }
  }

我想将此 json 查询转换为 JAVA

这就是我在 JAVA

中已经尝试过的
AggregationBuilder aggregation = AggregationBuilders.terms("dedup_by_score")
    .field("keyword")
    .order(BucketOrder.aggregation("top_hit", false))
    .size(10)
    .subAggregation(
        AggregationBuilders.topHits("top_hit")
        .subAggregation(
            AggregationBuilders.max("max").script(new Script("_score"))
        )
    );

但是我从 Elasticsearch 收到如下错误

{
"type":"aggregation_initialization_exception",
"reason":"Aggregator [top_hit] of type [top_hits] cannot accept sub-aggregations"
}

如何修复此 Java 代码?我现在用的是Elasticsearch 6.7.1版本

提前致谢

热门聚合不能有子聚合。试试这个:

AggregationBuilder aggregation = AggregationBuilders.terms("dedup_by_score")
        .field("keyword")
        .order(BucketOrder.aggregation("top_hit", false))
        .size(10)
        .subAggregation(
            AggregationBuilders.max("max").script(new Script("_score"))
                .subAggregation(
                    AggregationBuilders.topHits("top_hit")
                )
        );