本文整理汇总了Java中org.elasticsearch.search.aggregations.metrics.avg.Avg类的典型用法代码示例。如果您正苦于以下问题:Java Avg类的具体用法?Java Avg怎么用?Java Avg使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Avg类属于org.elasticsearch.search.aggregations.metrics.avg包,在下文中一共展示了Avg类的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Java代码示例。
示例1: testSortingByMetric
import org.elasticsearch.search.aggregations.metrics.avg.Avg; //导入依赖的package包/类
@Test
public void testSortingByMetric() {
String termsColors = "colors", avgPrice = "avg_price";
srb =
client.prepareSearch("cars").addAggregation(
AggregationBuilders.terms(termsColors).field("color")
.order(Order.aggregation(avgPrice, true))
.subAggregation(AggregationBuilders.avg(avgPrice).field("price")));
SearchResponse response = srb.execute().actionGet();
// read agg
Terms colorsTerms = (Terms) response.getAggregations().get("colors");
colorsTerms.getBuckets().forEach(
bucket -> {
double averagePrice = ((Avg) bucket.getAggregations().get(avgPrice)).getValue();
System.out.println(String.format("Key: %s, Doc count: %d, Average Price: %f",
bucket.getKey(), bucket.getDocCount(), averagePrice));
});
}
开发者ID:destiny1020,项目名称:elasticsearch-java-client-examples,代码行数:21,代码来源:SortingMulValBucketsExamples.java
示例2: testQueryAvgAndAll
import org.elasticsearch.search.aggregations.metrics.avg.Avg; //导入依赖的package包/类
@Test
public void testQueryAvgAndAll() {
String avgFord = "m_avg_ford", global = "all", avgAll = "m_avg_all";
srb =
client.prepareSearch("cars").setQuery(QueryBuilders.matchQuery("make", "ford"))
.setSearchType(SearchType.COUNT);
// avg on matched results
srb.addAggregation(AggregationBuilders.avg(avgFord).field("price"));
// avg on global
srb.addAggregation(AggregationBuilders.global(global).subAggregation(
AggregationBuilders.avg(avgAll).field("price")));
SearchResponse response = srb.execute().actionGet();
// read agg
Avg fordAvg = (Avg) response.getAggregations().get(avgFord);
Global globalAgg = (Global) response.getAggregations().get(global);
Avg allAvg = (Avg) globalAgg.getAggregations().get(avgAll);
System.out.println(String.format("Ford Avg: %f", fordAvg.getValue()));
System.out.println(String.format("All Avg: %f", allAvg.getValue()));
}
开发者ID:destiny1020,项目名称:elasticsearch-java-client-examples,代码行数:26,代码来源:FilterAndAggregationExamples.java
示例3: aggregate
import org.elasticsearch.search.aggregations.metrics.avg.Avg; //导入依赖的package包/类
/**
* 聚合分析
* 1. 先分组
* 2. 子分组
* 3. 最后算出子分组的平均值
*
* @param transportClient
* @throws IOException
*/
private static void aggregate(TransportClient transportClient) throws IOException {
SearchResponse searchResponse = transportClient.prepareSearch("product_index").setTypes("product")
.addAggregation(AggregationBuilders.terms("product_group_by_price").field("price")
.subAggregation(AggregationBuilders.dateHistogram("product_group_by_created_date_time").field("created_date_time")
.dateHistogramInterval(DateHistogramInterval.YEAR)
.subAggregation(AggregationBuilders.avg("product_avg_price").field("price")))
).execute().actionGet();
Map<String, Aggregation> aggregationMap = searchResponse.getAggregations().asMap();
StringTerms productGroupByPrice = (StringTerms) aggregationMap.get("product_group_by_price");
Iterator<Terms.Bucket> productGroupByPriceIterator = productGroupByPrice.getBuckets().iterator();
while (productGroupByPriceIterator.hasNext()) {
Terms.Bucket productGroupByPriceBucket = productGroupByPriceIterator.next();
logger.info("--------------------------------:" + productGroupByPriceBucket.getKey() + ":" + productGroupByPriceBucket.getDocCount());
Histogram productGroupByPrice1 = (Histogram) productGroupByPriceBucket.getAggregations().asMap().get("product_group_by_price");
Iterator<org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket> groupByCreateDateTimeIterator = productGroupByPrice1.getBuckets().iterator();
while (groupByCreateDateTimeIterator.hasNext()) {
Histogram.Bucket groupByCreateDateTimeBucket = groupByCreateDateTimeIterator.next();
logger.info("--------------------------------:" + groupByCreateDateTimeBucket.getKey() + ":" + groupByCreateDateTimeBucket.getDocCount());
Avg avgPrice = (Avg) groupByCreateDateTimeBucket.getAggregations().asMap().get("product_avg_price");
logger.info("--------------------------------:" + avgPrice.getValue());
}
}
}
开发者ID:judasn,项目名称:Elasticsearch-Tutorial-zh-CN,代码行数:40,代码来源:BaseDemo.java
示例4: toNodeTimeseriesStatistics
import org.elasticsearch.search.aggregations.metrics.avg.Avg; //导入依赖的package包/类
private static NodeTimeseriesStatistics toNodeTimeseriesStatistics(Bucket bucket) {
Terms term = bucket.getAggregations().get("components");
NodeTimeseriesStatistics s = new NodeTimeseriesStatistics();
s.setTimestamp(bucket.getKeyAsDate().getMillis());
term.getBuckets().forEach(b -> {
Avg avg = b.getAggregations().get("avg");
NodeComponentTypeStatistics component = new NodeComponentTypeStatistics((long)avg.getValue(), b.getDocCount());
s.getComponentTypes().put(b.getKey(), component);
});
return s;
}
开发者ID:hawkular,项目名称:hawkular-apm,代码行数:12,代码来源:AnalyticsServiceElasticsearch.java
示例5: getValueFromAggregation
import org.elasticsearch.search.aggregations.metrics.avg.Avg; //导入依赖的package包/类
public static String getValueFromAggregation(Aggregation a, Function f){
String value = null;
switch(f.type){
case Function.SUM :
value = String.valueOf(((Sum) a).getValue());
break;
case Function.COUNT :
value = String.valueOf(((ValueCount) a).getValue());
break;
case Function.DC :
value = String.valueOf(((Cardinality) a).getValue());
break;
case Function.AVG :
value = String.valueOf(((Avg) a).getValue());
break;
case Function.MAX :
value = String.valueOf(((Max) a).getValue());
break;
case Function.MIN :
value = String.valueOf(((Min) a).getValue());
break;
}
return value;
}
开发者ID:huangchen007,项目名称:elasticsearch-rest-command,代码行数:29,代码来源:Search.java
示例6: testFilteredQueryWithAggAndGlobalAgg
import org.elasticsearch.search.aggregations.metrics.avg.Avg; //导入依赖的package包/类
@Test
public void testFilteredQueryWithAggAndGlobalAgg() {
String avgFiltered = "m_avg", global = "all", avgAll = "m_avg_all";
srb =
client
.prepareSearch("cars")
.setQuery(
QueryBuilders.filteredQuery(QueryBuilders.matchAllQuery(), FilterBuilders
.rangeFilter("price").gt(10000))).setSearchType(SearchType.COUNT);
// agg on filtered results
srb.addAggregation(AggregationBuilders.avg(avgFiltered).field("price"));
// agg on global
srb.addAggregation(AggregationBuilders.global(global).subAggregation(
AggregationBuilders.avg(avgAll).field("price")));
SearchResponse response = srb.execute().actionGet();
// read agg
Avg filteredAvg = (Avg) response.getAggregations().get(avgFiltered);
Global globalAgg = (Global) response.getAggregations().get(global);
Avg allAvg = (Avg) globalAgg.getAggregations().get(avgAll);
System.out.println(String.format("Filtered Avg: %f", filteredAvg.getValue()));
System.out.println(String.format("All Avg: %f", allAvg.getValue()));
}
开发者ID:destiny1020,项目名称:elasticsearch-java-client-examples,代码行数:28,代码来源:FilterAndAggregationExamples.java
示例7: getValue
import org.elasticsearch.search.aggregations.metrics.avg.Avg; //导入依赖的package包/类
@Override
public double getValue(Aggregation aggregation) {
return ((Avg) aggregation).getValue();
}
开发者ID:justor,项目名称:elasticsearch_my,代码行数:5,代码来源:NaNSortingIT.java
示例8: avgTest
import org.elasticsearch.search.aggregations.metrics.avg.Avg; //导入依赖的package包/类
@Test
public void avgTest() throws IOException, SqlParseException, SQLFeatureNotSupportedException {
Aggregations result = query(String.format("SELECT AVG(age) FROM %s/account", TEST_INDEX));
Avg avg = result.get("AVG(age)");
assertThat(avg.getValue(), equalTo(30.171));
}
开发者ID:mazhou,项目名称:es-sql,代码行数:7,代码来源:AggregationTest.java
示例9: avgTest
import org.elasticsearch.search.aggregations.metrics.avg.Avg; //导入依赖的package包/类
@Test
public void avgTest() throws IOException, SqlParseException, SQLFeatureNotSupportedException {
Aggregations result = query(String.format("SELECT AVG(age) FROM %s/account", TestsConstants.TEST_INDEX));
Avg avg = result.get("AVG(age)");
assertThat(avg.getValue(), equalTo(30.171));
}
开发者ID:selvakumarEsra,项目名称:es4sql,代码行数:7,代码来源:AggregationTest.java
示例10: avgTest
import org.elasticsearch.search.aggregations.metrics.avg.Avg; //导入依赖的package包/类
@Test
public void avgTest() throws IOException, SqlParseException, SQLFeatureNotSupportedException {
Aggregations result = query(String.format("SELECT AVG(age) FROM %s/account", TEST_INDEX_ACCOUNT));
Avg avg = result.get("AVG(age)");
assertThat(avg.getValue(), equalTo(30.171));
}
开发者ID:NLPchina,项目名称:elasticsearch-sql,代码行数:7,代码来源:AggregationTest.java
示例11: assertTop10of50OneShardNestedAggregations
import org.elasticsearch.search.aggregations.metrics.avg.Avg; //导入依赖的package包/类
@Test
public void assertTop10of50OneShardNestedAggregations() {
client.admin().indices().prepareCreate("topk-4").setSettings(ImmutableSettings.settingsBuilder().put("index.number_of_shards", 1)).execute().actionGet();
double sum = 0;
for (int i = 0; i < 50; ++i) { // 50 values
client.prepareIndex("topk-4", "type0", "doc" + i).setSource("{ \"field0\": \"foo" + i + "\", \"field1\":" + i +" }").setRefresh(true).execute().actionGet();
}
// foo0 x 50
for (int i = 50; i < 100; ++i) {
client.prepareIndex("topk-4", "type0", "doc" + i).setSource("{ \"field0\": \"foo0\", \"field1\":" + i +" }").setRefresh(true).execute().actionGet();
sum += i;
}
SearchResponse searchResponse = client.prepareSearch("topk-4")
.setQuery(matchAllQuery())
.addAggregation(new TopKBuilder("topk").field("field0").size(10)
.subAggregation(new AvgBuilder("avg").field("field1"))
.subAggregation(new MaxBuilder("max").field("field1"))
)
.execute().actionGet();
assertEquals(100, searchResponse.getHits().getTotalHits());
TopK topk = searchResponse.getAggregations().get("topk");
assertNotNull(topk);
List<TopK.Bucket> buckets = new ArrayList<>(topk.getBuckets());
assertEquals(10, buckets.size());
assertEquals("foo0", buckets.get(0).getKey());
assertEquals(51, buckets.get(0).getDocCount());
assertEquals(2, buckets.get(0).getAggregations().asList().size());
for (Aggregation agg : buckets.get(0).getAggregations()) {
switch (agg.getName()) {
case "avg":
assertEquals(sum / 51, ((Avg) agg).getValue(), 0.01);
break;
case "max":
assertEquals(99.0, ((Max) agg).getValue(), 0.001);
break;
default:
assertTrue(false);
}
}
}
开发者ID:algolia,项目名称:elasticsearch-topk-plugin,代码行数:44,代码来源:SimpleTests.java
示例12: assertTop10of50TwoShardNestedAggregations
import org.elasticsearch.search.aggregations.metrics.avg.Avg; //导入依赖的package包/类
@Test
public void assertTop10of50TwoShardNestedAggregations() {
client.admin().indices().prepareCreate("topk-5").setSettings(ImmutableSettings.settingsBuilder().put("index.number_of_shards", 2)).execute().actionGet();
double sum = 0;
for (int i = 0; i < 50; ++i) { // 50 values
client.prepareIndex("topk-5", "type0", "doc" + i).setSource("{ \"field0\": \"foo" + i + "\", \"field1\":" + i +" }").setRefresh(true).execute().actionGet();
}
// foo0 x 50
for (int i = 50; i < 100; ++i) {
client.prepareIndex("topk-5", "type0", "doc" + i).setSource("{ \"field0\": \"foo0\", \"field1\":" + i +" }").setRefresh(true).execute().actionGet();
sum += i;
}
SearchResponse searchResponse = client.prepareSearch("topk-5")
.setQuery(matchAllQuery())
.addAggregation(new TopKBuilder("topk").field("field0").size(10)
.subAggregation(new AvgBuilder("avg").field("field1"))
.subAggregation(new MaxBuilder("max").field("field1"))
)
.execute().actionGet();
assertEquals(100, searchResponse.getHits().getTotalHits());
TopK topk = searchResponse.getAggregations().get("topk");
assertNotNull(topk);
List<TopK.Bucket> buckets = new ArrayList<>(topk.getBuckets());
assertEquals(10, buckets.size());
assertEquals("foo0", buckets.get(0).getKey());
assertEquals(51, buckets.get(0).getDocCount());
assertEquals(2, buckets.get(0).getAggregations().asList().size());
for (Aggregation agg : buckets.get(0).getAggregations()) {
switch (agg.getName()) {
case "avg":
assertEquals(sum / 51, ((Avg) agg).getValue(), 0.01);
break;
case "max":
assertEquals(99.0, ((Max) agg).getValue(), 0.001);
break;
default:
assertTrue(false);
}
}
}
开发者ID:algolia,项目名称:elasticsearch-topk-plugin,代码行数:44,代码来源:SimpleTests.java
示例13: assertTop10of50TwoShardNestedAggregationsStress
import org.elasticsearch.search.aggregations.metrics.avg.Avg; //导入依赖的package包/类
@Test
public void assertTop10of50TwoShardNestedAggregationsStress() {
client.admin().indices().prepareCreate("topk-6").setSettings(ImmutableSettings.settingsBuilder().put("index.number_of_shards", 4)).execute().actionGet();
final int N = 30000;
double sum = 0;
int n = 0;
int max = Integer.MIN_VALUE;
Random r = new Random();
for (int i = 0; i < N; ++i) {
int v = r.nextInt();
if (i % 7 == 0) {
sum += v;
++n;
if (v > max) {
max = v;
}
}
client.prepareIndex("topk-6", "type0", "doc" + i).setSource("{ \"field0\": \"foo" + (i % 7 == 0 ? "bar" : String.valueOf(i)) + "\", \"field1\":" + v +" }").setRefresh(i == N - 1).execute().actionGet();
}
try { Thread.sleep(2000); } catch (InterruptedException e) {} // FIXME: wait until all docs are searchable
SearchResponse searchResponse = client.prepareSearch("topk-6")
.setQuery(matchAllQuery())
.addAggregation(new TopKBuilder("topk").field("field0").size(10)
.subAggregation(new AvgBuilder("avg").field("field1"))
.subAggregation(new MaxBuilder("max").field("field1"))
)
.execute().actionGet();
assertEquals(N, searchResponse.getHits().getTotalHits());
TopK topk = searchResponse.getAggregations().get("topk");
assertNotNull(topk);
List<TopK.Bucket> buckets = new ArrayList<>(topk.getBuckets());
assertEquals(10, buckets.size());
assertEquals("foobar", buckets.get(0).getKey());
assertEquals(n, buckets.get(0).getDocCount());
assertEquals(2, buckets.get(0).getAggregations().asList().size());
for (Aggregation agg : buckets.get(0).getAggregations()) {
switch (agg.getName()) {
case "avg":
assertEquals(sum / n, ((Avg) agg).getValue(), 0.01);
break;
case "max":
assertEquals((double) max, ((Max) agg).getValue(), 0.001);
break;
default:
assertTrue(false);
}
}
}
开发者ID:algolia,项目名称:elasticsearch-topk-plugin,代码行数:51,代码来源:SimpleTests.java
注:本文中的org.elasticsearch.search.aggregations.metrics.avg.Avg类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论