我正在尝试使用特殊键从 Elastic search 获取结果 示例:
average salary, by city, gender
所以我检查 metrics_aggregations
我能够
AvgAggregationBuilder totalDuration = AggregationBuilders.avg("salaryAvg").field("salary");
它确实返回了最高级别 - 没有特定字段的平均工资
所以我尝试使用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 search 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);
关于java - 如何从 Avg Aggregation Builder elasticsearch 获取特定键的平均值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60718944/