[英]How to get Average from AvgAggregationBuilder elastic search for spesific keys
I'm am trying to get results from Elastic search with spesic keys example:我正在尝试使用特殊键示例从弹性搜索中获取结果:
average salary, by city, gender
平均工资,按城市,性别
so i check at metrics_aggregations所以我检查了metrics_aggregations
and I was able我能够
AvgAggregationBuilder totalDuration = AggregationBuilders.avg("salaryAvg").field("salary");
it did return the most high level - average salary without specific fields它确实返回了最高级别 - 没有特定领域的平均工资
So I tried using subAggregation
所以我尝试使用
subAggregation
AvgAggregationBuilder totalDuration = AggregationBuilders
.avg("salaryAvg").field("salary")
.subAggregation(AggregationBuilders.terms("city").field("gender"));
but i got below error但我得到了以下错误
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
21525\\nElasticsearchStatusException[Elasticsearch 异常 [type=aggregation_initialization_exception, reason=Aggregator [salaryAvg] of type [avg] 不能接受子聚合]]\\n\\tat org.elasticsearch.rest.BytesRestResponse.errorFromXConten
how can I query it with elastic search what I want is something quite similar to SQL - group by我如何使用弹性搜索查询它我想要的是与 SQL 非常相似的东西 - group by
each row of my elastic response data seems like that:我的弹性响应数据的每一行看起来像这样:
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"
How can I support it using Elastic search JAVA API?如何使用 Elastic search JAVA API 支持它?
I tried also我也试过
running it:运行它:
AvgAggregationBuilder totalDuration0 = AggregationBuilders.avg(TOTAL_DURATION_AVG).field(TOTAL_DURATION);
TermsAggregationBuilder totalDuration = AggregationBuilders.terms(TOTAL_DURATION_AVG)
.field(ATTRIBUTES_WIDGET_ID).subAggregation(totalDuration0);
I got this response我收到了这个回复
there's seems to have a different average and how much rows for each - row in this list but I have no idea which "key" is pointing to seems like a byte array似乎有一个不同的平均值以及每个列表中有多少行 - 此列表中的行但我不知道哪个“键”指向的似乎是一个字节数组
but it doesn't match to the actual key I have also [key] seems to be duplicated with a different average so what am I missing here.但它与我拥有的实际密钥不匹配 [key] 似乎与不同的平均值重复,所以我在这里错过了什么。 ?
?
I was able to get avg by a single field, but not more than one field我能够通过单个字段获得平均值,但不能超过一个字段
final AvgAggregationBuilder totalDuration = AggregationBuilders.avg(avgSalary).field(SALARY_AVG);
final TermsAggregationBuilder totalDurationByWidgetId = AggregationBuilders.terms(SALARY_AVG)
.field(GENDER).subAggregation(avgSalary);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.