gpt4 book ai didi

java - ElasticSearch - 按两个不同字段分组

转载 作者:行者123 更新时间:2023-12-01 10:46:09 25 4
gpt4 key购买 nike

有没有办法在elasticsearch中的两个字段上应用group by?

TermsBuilder yearAgg = AggregationBuilders.terms("by_year").field("year").subAggregation(AggregationBuilders.terms("by_name")).field("Name").subAggregation(sumMarks);
// create the bool filter for the condition above
String[] names = { "stokes", "roshan" };
BoolQueryBuilder aggFilter = QueryBuilders.boolQuery().must(QueryBuilders.termsQuery("Name", names));

// create the filter aggregation and add the year sub-aggregation
FilterAggregationBuilder aggregation = AggregationBuilders.filter("agg").filter(aggFilter).subAggregation(yearAgg);

// create the request and execute it
SearchResponse response = client.prepareSearch("bighalf").setTypes("excel").addAggregation(aggregation).execute().actionGet();
System.out.println(response.toString());

我尝试根据两个不同的术语应用分组,但没有得到预期的结果。

分组后的响应:

{
"aggregations": {
"agg": {
"doc_count": 2,
"by_year": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "roshan",
"doc_count": 1,
"by_name": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "roshan",
"doc_count": 1
}
]
},
"sum_marks": {
"value": 85
}
},
{
"key": "stokes",
"doc_count": 1,
"by_name": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "stokes",
"doc_count": 1
}
]
},
"sum_marks": {
"value": 91
}
}
]
}
}
}
}

我只能看到“by_name”分组下的文档计数。有没有更好的方法在elasticsearch中的两个不同字段上应用分组。

最佳答案

您构建聚合的方式有误,您对 by_yearby_name 使用了相同的字段 Name聚合。

// your code
TermsBuilder yearAgg = AggregationBuilders.terms("by_year")
.field("year").subAggregation(AggregationBuilders.terms("by_name")).field("Name").subAggregation(sumMarks);
^
|
This parenthesis is wrong, it should go at the end

像这样做

TermsBuilder yearAgg = AggregationBuilders.terms("by_year").field("year")
.subAggregation(AggregationBuilders.terms("by_name").field("Name").subAggregation(sumMarks));

关于java - ElasticSearch - 按两个不同字段分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34165386/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com