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

时间:2015-12-08 20:27:46

标签: java elasticsearch

有没有办法在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中的两个不同字段上应用分组。

1 个答案:

答案 0 :(得分:1)

您构建聚合的方式有误,您对Nameby_year聚合使用相同的字段by_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));