下面是如何使用 Min Aggregation 与Java API。
这里有一个例子关于如何创建聚合的要求:
MetricsAggregationBuilder aggregation =
AggregationBuilders
.min("agg")
.field("height");
导入聚合定义类:
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 与Java API。
这里有一个例子关于如何创建聚合的要求:
MetricsAggregationBuilder aggregation =
AggregationBuilders
.max("agg")
.field("height");
导入聚合定义类:
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 与Java API。
这里有一个例子关于如何创建聚合的要求:
MetricsAggregationBuilder aggregation =
AggregationBuilders
.sum("agg")
.field("height");
导入聚合定义类:
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聚合与Java API。
这里有一个例子关于如何创建聚合的要求:
MetricsAggregationBuilder aggregation =
AggregationBuilders
.avg("agg")
.field("height");
导入聚合定义类:
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 与Java API。
这里有一个例子关于如何创建聚合的要求:
MetricsAggregationBuilder aggregation =
AggregationBuilders
.stats("agg")
.field("height");
导入聚合定义类:
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 与Java API。
这里有一个例子关于如何创建聚合的要求:
MetricsAggregationBuilder aggregation =
AggregationBuilders
.extendedStats("agg")
.field("height");
导入聚合定义类:
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 与Java API。
这里有一个例子关于如何创建聚合的要求:
MetricsAggregationBuilder aggregation =
AggregationBuilders
.count("agg")
.field("height");
导入聚合定义类:
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 与Java API。
这里有一个例子关于如何创建聚合的要求:
MetricsAggregationBuilder aggregation =
AggregationBuilders
.percentiles("agg")
.field("height");
MetricsAggregationBuilder aggregation =
AggregationBuilders
.percentiles("agg")
.field("height")
.percentiles(1.0, 5.0, 10.0, 20.0, 30.0, 75.0, 95.0, 99.0);
使用聚合反应编辑
导入聚合定义类:
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与Java API。
这里有一个例子关于如何创建聚合的要求:
MetricsAggregationBuilder aggregation =
AggregationBuilders
.percentileRanks("agg")
.field("height")
.percentiles(1.24, 1.91, 2.22);
导入聚合定义类:
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 与Java API。
这里有一个例子关于如何创建聚合的要求:
MetricsAggregationBuilder aggregation =
AggregationBuilders
.cardinality("agg")
.field("tags");
导入聚合定义类:
import org.elasticsearch.search.aggregations.metrics.cardinality.Cardinality;
// sr is here your SearchResponse object
Cardinality agg = sr.getAggregations().get("agg");
long value = agg.getValue();
下面是如何使用地理边界聚合与Java API。
这里有一个例子关于如何创建聚合的要求:
GeoBoundsBuilder aggregation =
AggregationBuilders
.geoBounds("agg")
.field("address.location")
.wrapLongitude(true);
使用聚合反应编辑
导入聚合定义类:
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与Java API。
这里有一个例子关于如何创建聚合的要求:
AggregationBuilder aggregation =
AggregationBuilders
.terms("agg").field("gender")
.subAggregation(
AggregationBuilders.topHits("top")
);
from
,
size
,
sort
,
highlight
,
explain
…
AggregationBuilder aggregation =
AggregationBuilders
.terms("agg").field("gender")
.subAggregation(
AggregationBuilders.topHits("top")
.setExplain(true)
.setSize(1)
.setFrom(10)
);
导入聚合定义类:
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",...}]
下面是如何使用脚本化的度规聚合与Java API。
不要忘记添加Groovy在您的类路径中,如果你想运行Groovy脚本在嵌入式数据节点(例如单元测试)。例如,使用Maven,添加这种依赖性 pom.xml
文件:
org.codehaus.groovy
groovy-all
2.3.2
indy
这里有一个例子关于如何创建聚合的要求:
MetricsAggregationBuilder aggregation =
AggregationBuilders
.scriptedMetric("agg")
.initScript("_agg['heights'] = []")
.mapScript(new Script("if (doc['gender'].value == \"male\") " +
"{ _agg.heights.add(doc['height'].value) } " +
"else " +
"{ _agg.heights.add(-1 * doc['height'].value) }"));
你也可以指定一个
combine
脚本将在每个碎片上执行:
MetricsAggregationBuilder aggregation =
AggregationBuilders
.scriptedMetric("agg")
.initScript(new Script("_agg['heights'] = []"))
.mapScript(new Script("if (doc['gender'].value == \"male\") " +
"{ _agg.heights.add(doc['height'].value) } " +
"else " +
"{ _agg.heights.add(-1 * doc['height'].value) }"))
.combineScript(new Script("heights_sum = 0; for (t in _agg.heights) { heights_sum += t }; return heights_sum"));
你也可以指定一个
reduce
脚本将执行被请求的节点:
MetricsAggregationBuilder aggregation =
AggregationBuilders
.scriptedMetric("agg")
.initScript(new Script("_agg['heights'] = []"))
.mapScript(new Script("if (doc['gender'].value == \"male\") " +
"{ _agg.heights.add(doc['height'].value) } " +
"else " +
"{ _agg.heights.add(-1 * doc['height'].value) }"))
.combineScript(new Script("heights_sum = 0; for (t in _agg.heights) { heights_sum += t }; return heights_sum"))
.reduceScript(new Script("heights_sum = 0; for (a in _aggs) { heights_sum += a }; return heights_sum"));
导入聚合定义类:
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]