【问题标题】:Elasticsearch Subaggregation of tophits JAVA API not workingtophits JAVA API的Elasticsearch子聚合不起作用
【发布时间】:2019-10-31 16:53:31
【问题描述】:

我想在 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

这是我已经在 J​​AVA 中尝试过的

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 版本。

提前致谢

【问题讨论】:

    标签: java elasticsearch elasticsearch-aggregation elasticsearch-java-api


    【解决方案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")
                    )
            );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-10-10
      • 2015-12-30
      • 1970-01-01
      • 1970-01-01
      • 2022-01-25
      • 2014-10-09
      • 1970-01-01
      相关资源
      最近更新 更多