ElasticSearch6.X版本Java Api中文详解(十二)之Aggregations解析

Aggregations

  • 使用工厂聚合生成器(AggregationBuilders),并将查询时要计算的每个聚合添加到搜索请求中:
SearchResponse sr = node.client().prepareSearch()
        .setQuery( /* your query */ )
        .addAggregation( /* add an aggregation */ )
        .execute().actionGet();
  • 要构建聚合请求,请使用AggregationBuilders helper。把它们导入到你的类中:
import org..search.aggregations.AggregationBuilders;

Structuring aggregations

正如在聚合指南中所解释的,您可以在聚合中定义子聚合。

聚合可以是度量聚合或桶聚合。

例如,这里有一个由以下三个层次组成的聚合:

  • 聚合条件(桶)
  • 数据直方图聚合(桶)
  • 平均聚合(公制)
SearchResponse sr = node.client().prepareSearch()
    .addAggregation(
        AggregationBuilders.terms("by_country").field("country")
        .subAggregation(AggregationBuilders.dateHistogram("by_year")
            .field("dateOfBirth")
            .dateHistogramInterval(DateHistogramInterval.YEAR)
            .subAggregation(AggregationBuilders.avg("avg_children").field("children"))
        )
    )
    .execute().actionGet();

Metrics aggregations

Min Aggregation

Prepare aggregation request

  • 下面是一个如何创建聚合请求的例子:
MinAggregationBuilder aggregation =
        AggregationBuilders
                .min("agg")
                .field("height");
Use aggregation response

  • 导入聚合定义类:

import org.elasticsearch.search.aggregations.metrics.min.Min;
// sr is here your SearchResponse object
Min agg = sr.getAggregations().get("agg");
double value = agg.getValue();

Max Aggregation

Prepare aggregation request

  • 下面是一个如何创建聚合请求的例子:
MaxAggregationBuilder aggregation =
        AggregationBuilders
                .max("agg")
                .field("height");
Use aggregation response
  • 导入聚合定义类:

import org.elasticsearch.search.aggregations.metrics.max.Max;
// sr is here your SearchResponse object
Max agg = sr.getAggregations().get("agg");
double value = agg.getValue();

Sum Aggregation

Prepare aggregation request
  • 下面是一个如何创建聚合请求的例子:
SumAggregationBuilder aggregation =
        AggregationBuilders
                .sum("agg")
                .field("height");

Use aggregation response

  • 导入聚合定义类:
import org.elasticsearch.search.aggregations.metrics.sum.Sum;
// sr is here your SearchResponse object
Sum agg = sr.getAggregations().get("agg");
double value = agg.getValue();

Avg Aggregation

Prepare aggregation request

  • 下面是一个如何创建聚合请求的例子:
AvgAggregationBuilder aggregation =
        AggregationBuilders
                .avg("agg")
                .field("height");

Use aggregation response

  • 导入聚合定义类:
import org.elasticsearch.search.aggregations.metrics.avg.Avg;
// sr is here your SearchResponse object
Avg agg = sr.getAggregations().get("agg");
double value = agg.getValue();

Stats Aggregation

Prepare aggregation request

  • 下面是一个如何创建聚合请求的例子:
StatsAggregationBuilder aggregation =
        AggregationBuilders
                .stats("agg")
                .field("height");

Use aggregation response

  • 导入聚合定义类:
import org.elasticsearch.search.aggregations.metrics.stats.Stats;
// sr is here your SearchResponse object
Stats agg = sr.getAggregations().get("agg");
double min = agg.getMin();
double max = agg.getMax();
double avg = agg.getAvg();
double sum = agg.getSum();
long count = agg.getCount();

Extended Stats Aggregation

Prepare aggregation request

  • 下面是一个如何创建聚合请求的例子:
ExtendedStatsAggregationBuilder aggregation =
        AggregationBuilders
                .extendedStats("agg")
                .field("height");

Use aggregation response

  • 导入聚合定义类:
import org.elasticsearch.search.aggregations.metrics.stats.extended.ExtendedStats;
// sr is here your SearchResponse object
ExtendedStats agg = sr.getAggregations().get("agg");
double min = agg.getMin();
double max = agg.getMax();
double avg = agg.getAvg();
double sum = agg.getSum();
long count = agg.getCount();
double stdDeviation = agg.getStdDeviation();
double sumOfSquares = agg.getSumOfSquares();
double variance = agg.getVariance();

Value Count Aggregation

Prepare aggregation request

  • 下面是一个如何创建聚合请求的例子:
ValueCountAggregationBuilder aggregation =
        AggregationBuilders
                .count("agg")
                .field("height");

Use aggregation response
导入聚合定义类:

import org.elasticsearch.search.aggregations.metrics.valuecount.ValueCount;
// sr is here your SearchResponse object
ValueCount agg = sr.getAggregations().get("agg");
long value = agg.getValue();

Percentile Aggregation

Prepare aggregation request
下面是一个如何创建聚合请求的例子:

PercentilesAggregationBuilder aggregation =
        AggregationBuilders
                .percentiles("agg")
                .field("height");

你能使用自己百分比参数代替默认的。

PercentilesAggregationBuilder aggregation =
        AggregationBuilders
                .percentiles("agg")
                .field("height")
                .percentiles(1.0, 5.0, 10.0, 20.0, 30.0, 75.0, 95.0, 99.0);

Use aggregation response
导入聚合定义类:

import org.elasticsearch.search.aggregations.metrics.percentiles.Percentile;
import org.elasticsearch.search.aggregations.metrics.percentiles.Percentiles;
// sr is here your SearchResponse object
Percentiles agg = sr.getAggregations().get("agg");
// For each entry
for (Percentile entry : agg) {
    double percent = entry.getPercent();    // Percent
    double value = entry.getValue();        // Value

    logger.info("percent [{}], value [{}]", percent, value);
}

percent [1.0], value [0.814338896154595]
percent [5.0], value [0.8761912455821302]
percent [25.0], value [1.173346540141847]
percent [50.0], value [1.5432023318692198]
percent [75.0], value [1.923915462033674]
percent [95.0], value [2.2273644908535335]
percent [99.0], value [2.284989339108279]

Percentile Ranks Aggregation

Prepare aggregation request
下面是一个如何创建聚合请求的例子:

PercentileRanksAggregationBuilder aggregation =
        AggregationBuilders
                .percentileRanks("agg")
                .field("height")
                .values(1.24, 1.91, 2.22);

Use aggregation response
导入聚合定义类:

import org.elasticsearch.search.aggregations.metrics.percentiles.Percentile;
import org.elasticsearch.search.aggregations.metrics.percentiles.PercentileRanks;
// sr is here your SearchResponse object
PercentileRanks agg = sr.getAggregations().get("agg");
// For each entry
for (Percentile entry : agg) {
    double percent = entry.getPercent();    // Percent
    double value = entry.getValue();        // Value

    logger.info("percent [{}], value [{}]", percent, value);
}

percent [29.664353095090945], value [1.24]
percent [73.9335313461868], value [1.91]
percent [94.40095147327283], value [2.22]

Cardinality Aggregation

Prepare aggregation request
下面是一个如何创建聚合请求的例子:

CardinalityAggregationBuilder aggregation =
        AggregationBuilders
                .cardinality("agg")
                .field("tags");

Use aggregation response
导入聚合定义类:

import org.elasticsearch.search.aggregations.metrics.cardinality.Cardinality;
// sr is here your SearchResponse object
Cardinality agg = sr.getAggregations().get("agg");
long value = agg.getValue();

Geo Bounds Aggregation

Prepare aggregation request
下面是一个如何创建聚合请求的例子:

GeoBoundsBuilder aggregation =
        GeoBoundsAggregationBuilder
                .geoBounds("agg")
                .field("address.location")
                .wrapLongitude(true);

Use aggregation response
导入聚合定义类:

import org.elasticsearch.search.aggregations.metrics.geobounds.GeoBounds;
// sr is here your SearchResponse object
GeoBounds agg = sr.getAggregations().get("agg");
GeoPoint bottomRight = agg.bottomRight();
GeoPoint topLeft = agg.topLeft();
logger.info("bottomRight {}, topLeft {}", bottomRight, topLeft);

bottomRight [40.70500764381921, 13.952946866893775], topLeft [53.49603022435221, -4.190029308156676]

Top Hits Aggregation

Prepare aggregation request
下面是一个如何创建聚合请求的例子:

AggregationBuilder aggregation =
    AggregationBuilders
        .terms("agg").field("gender")
        .subAggregation(
            AggregationBuilders.topHits("top")
        );

你可以使用更多的搜索条件来约束搜索结果,例如:

AggregationBuilder aggregation =
    AggregationBuilders
        .terms("agg").field("gender")
        .subAggregation(
            AggregationBuilders.topHits("top")
                .explain(true)
                .size(1)
                .from(10)
        );

Use aggregation response
导入聚合定义类:

import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.aggregations.metrics.tophits.TopHits;
// sr is here your SearchResponse object
Terms agg = sr.getAggregations().get("agg");

// For each entry
for (Terms.Bucket entry : agg.getBuckets()) {
    String key = entry.getKey();                    // bucket key
    long docCount = entry.getDocCount();            // Doc count
    logger.info("key [{}], doc_count [{}]", key, docCount);

    // We ask for top_hits for each bucket
    TopHits topHits = entry.getAggregations().get("top");
    for (SearchHit hit : topHits.getHits().getHits()) {
        logger.info(" -> id [{}], _source [{}]", hit.getId(), hit.getSourceAsString());
    }
}

key [male], doc_count [5107]
-> id [AUnzSZze9k7PKXtq04x2], _source [{“gender”:”male”,…}]
-> id [AUnzSZzj9k7PKXtq04x4], _source [{“gender”:”male”,…}]
-> id [AUnzSZzl9k7PKXtq04x5], _source [{“gender”:”male”,…}]
key [female], doc_count [4893]
-> id [AUnzSZzM9k7PKXtq04xy], _source [{“gender”:”female”,…}]
-> id [AUnzSZzp9k7PKXtq04x8], _source [{“gender”:”female”,…}]
-> id [AUnzSZ0W9k7PKXtq04yS], _source [{“gender”:”female”,…}]

Scripted Metric Aggregation

Prepare aggregation request
下面是一个如何创建聚合请求的例子:

ScriptedMetricAggregationBuilder aggregation = AggregationBuilders
    .scriptedMetric("agg")
    .initScript(new Script("params._agg.heights = []"))
    .mapScript(new Script("params._agg.heights.add(doc.gender.value == 'male' ? doc.height.value : -1.0 * doc.height.value)"));

你也可以指定一个合并脚本,它将在每个碎片上执行:

ScriptedMetricAggregationBuilder aggregation = AggregationBuilders
    .scriptedMetric("agg")
    .initScript(new Script("params._agg.heights = []"))
    .mapScript(new Script("params._agg.heights.add(doc.gender.value == 'male' ? doc.height.value : -1.0 * doc.height.value)"))
    .combineScript(new Script("double heights_sum = 0.0; for (t in params._agg.heights) { heights_sum += t } return heights_sum"));

您还可以指定一个reduce脚本,它将在获取请求的节点上执行:

ScriptedMetricAggregationBuilder aggregation = AggregationBuilders
    .scriptedMetric("agg")
    .initScript(new Script("params._agg.heights = []"))
    .mapScript(new Script("params._agg.heights.add(doc.gender.value == 'male' ? doc.height.value : -1.0 * doc.height.value)"))
    .combineScript(new Script("double heights_sum = 0.0; for (t in params._agg.heights) { heights_sum += t } return heights_sum"))
    .reduceScript(new Script("double heights_sum = 0.0; for (a in params._aggs) { heights_sum += a } return heights_sum"));

Use aggregation response
导入聚合定义类:

import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.aggregations.metrics.tophits.TopHits;
// sr is here your SearchResponse object
ScriptedMetric agg = sr.getAggregations().get("agg");
Object scriptedResult = agg.aggregation();
logger.info("scriptedResult [{}]", scriptedResult);

注意,结果取决于您构建的脚本。对于第一个例子,这基本上会产生结果:

scriptedResult object [ArrayList]
scriptedResult [ {
“heights” : [ 1.122218480146643, -1.8148918111233887, -1.7626731575142909, … ]
}, {
“heights” : [ -0.8046067304119863, -2.0785486707864553, -1.9183567430207953, … ]
}, {
“heights” : [ 2.092635728868694, 1.5697545960886536, 1.8826954461968808, … ]
}, {
“heights” : [ -2.1863201099468403, 1.6328549117346856, -1.7078288405893842, … ]
}, {
“heights” : [ 1.6043904836424177, -2.0736538674414025, 0.9898266674373053, … ]
} ]

第二个例子产生结果:

scriptedResult object [ArrayList]
scriptedResult [-41.279615707402876,
-60.88007362339038,
38.823270659734256,
14.840192739445632,
11.300902755741326]

第三个例子产生结果:

scriptedResult object [Double]
scriptedResult [2.171917696507009]

点赞

发表评论

电子邮件地址不会被公开。 必填项已用*标注