如何从 Avg Aggregation Builder elasticsearch 获取特定键的平均值

How to get Average from AvgAggregationBuilder elastic search for spesific keys

我正在尝试使用特殊键从 Elastic search 中获取结果 示例:

average salary, by city, gender

所以我在 metrics_aggregations

查看

而且我能够

AvgAggregationBuilder totalDuration = AggregationBuilders.avg("salaryAvg").field("salary");

它做到了return最高水平-没有特定领域的平均工资

所以我尝试使用 subAggregation

AvgAggregationBuilder totalDuration =     AggregationBuilders
            .avg("salaryAvg").field("salary")
            .subAggregation(AggregationBuilders.terms("city").field("gender"));

但我遇到了以下错误

Server Error. 21525\nElasticsearchStatusException[Elasticsearch exception [type=aggregation_initialization_exception, reason=Aggregator [salaryAvg] of type [avg] cannot accept sub-aggregations]]\n\tat org.elasticsearch.rest.BytesRestResponse.errorFromXConten

如何使用elasticsearch查询 我想要的是与 SQL - group by

非常相似的东西

我的弹性响应数据的每一行都是这样的:

widgetResponse.getHits().getHits()[0].getSourceAsMap().get("attributes")


{HashMap$Node@53898} "city" -> "new York"
{HashMap$Node@53899} "pageId" -> "123"
{HashMap$Node@53900} "salary" -> "45765"
{HashMap$Node@53900} "gender" -> "female"
{HashMap$Node@53901} "userId" -> "324"
{HashMap$Node@53902} "accountId" -> "456"


widgetResponse.getHits().getHits()[1].getSourceAsMap().get("attributes")


{HashMap$Node@53898} "city" -> "new Jersy"
{HashMap$Node@53899} "pageId" -> "123"
{HashMap$Node@53899} "salary" -> "77777"
{HashMap$Node@53900} "gender" -> "female"
{HashMap$Node@53901} "userId" -> "334"
{HashMap$Node@53902} "accountId" -> "999"

如何使用 Elastic 搜索支持它 JAVA API?

我也试过了

运行它:

        AvgAggregationBuilder totalDuration0 = AggregationBuilders.avg(TOTAL_DURATION_AVG).field(TOTAL_DURATION);
    TermsAggregationBuilder totalDuration = AggregationBuilders.terms(TOTAL_DURATION_AVG)
            .field(ATTRIBUTES_WIDGET_ID).subAggregation(totalDuration0);

我收到了这个回复

平均数和行数似乎不同 对于此列表中的每一行 但我不知道 "key" 指向哪个 看起来像一个字节数组

但它与我拥有的实际密钥不匹配 也 [key] 似乎与不同的平均值重复 那我在这里错过了什么。 ?

我能够通过单个字段获得平均值,但不能超过一个字段

    final AvgAggregationBuilder totalDuration = AggregationBuilders.avg(avgSalary).field(SALARY_AVG);
    final TermsAggregationBuilder totalDurationByWidgetId = AggregationBuilders.terms(SALARY_AVG)
            .field(GENDER).subAggregation(avgSalary);