在Elasticsearch中添加多个Sum Aggregations

时间:2017-06-01 18:54:43

标签: java elasticsearch query-builder elasticsearch-2.0 elasticsearch-query

我有一个json查询版本,在这样的聚合中添加两个字段的总和 -

DownloadManager downloadManager = DownloadManager.class.cast(getSystemService(DOWNLOAD_SERVICE));
Uri uri = downloadManager.getUriForDownloadedFile(id);

我需要使用Java QueryBuilder for Elasticsearch将我的数据转换为此查询

到目前为止 -

 "aggs": {
            "field1Sum": {
                "sum": {
                    "field": "field1"
                }
            },
            "field2Sum": {
                "sum": {
                    "field": "field2"
                }
            }
    }

任何帮助?

1 个答案:

答案 0 :(得分:1)

SearchResponse sr = client.prepareSearch()
    .setQuery(QueryBuilders.matchAllQuery())
    .addAggregation(
            AggregationBuilders.sum(field2Sum).field(field2)
    )
    .addAggregation(
            AggregationBuilders.sum(field1Sum).field(field1)
    )
    .get();

Link