org.elasticsearch.search.aggregations.metrics.avg.AvgBuilder Java Examples

The following examples show how to use org.elasticsearch.search.aggregations.metrics.avg.AvgBuilder. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AnalyticsServiceElasticsearch.java    From hawkular-apm with Apache License 2.0 5 votes vote down vote up
@Override
public List<NodeTimeseriesStatistics> getNodeTimeseriesStatistics(String tenantId, Criteria criteria, long interval) {
    String index = client.getIndex(tenantId);
    if (!refresh(index)) {
        return null;
    }

    AvgBuilder avgBuilder = AggregationBuilders
            .avg("avg")
            .field(ElasticsearchUtil.ACTUAL_FIELD);

    TermsBuilder componentsBuilder = AggregationBuilders
            .terms("components")
            .field("componentType")
            .size(criteria.getMaxResponseSize())
            .subAggregation(avgBuilder);

    DateHistogramBuilder histogramBuilder = AggregationBuilders
            .dateHistogram("histogram")
            .interval(interval)
            .field(ElasticsearchUtil.TIMESTAMP_FIELD)
            .subAggregation(componentsBuilder);

    BoolQueryBuilder query = buildQuery(criteria, ElasticsearchUtil.TRANSACTION_FIELD, NodeDetails.class);
    SearchRequestBuilder request = getNodeDetailsRequest(index, criteria, query, 0)
            .addAggregation(histogramBuilder);

    SearchResponse response = getSearchResponse(request);
    DateHistogram histogram = response.getAggregations().get("histogram");

    return histogram.getBuckets().stream()
            .map(AnalyticsServiceElasticsearch::toNodeTimeseriesStatistics)
            .collect(Collectors.toList());
}
 
Example #2
Source File: AST_Stats.java    From elasticsearch-rest-command with The Unlicense 5 votes vote down vote up
public static AbstractAggregationBuilder newAvg(Function func) {
	AvgBuilder avg;
	if (func.fieldtype == Field.SCRIPT)
		avg = AggregationBuilders.avg(Function.genStatField(func))
				.script(func.field);
	else
		avg = AggregationBuilders.avg(Function.genStatField(func))
				.field(func.field);

	return avg;
}
 
Example #3
Source File: AggregationBuilders.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
/**
 * Create a new {@link Avg} aggregation with the given name.
 */
public static AvgBuilder avg(String name) {
    return new AvgBuilder(name);
}
 
Example #4
Source File: SimpleTests.java    From elasticsearch-topk-plugin with Apache License 2.0 4 votes vote down vote up
@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);
        } 
    }
}
 
Example #5
Source File: SimpleTests.java    From elasticsearch-topk-plugin with Apache License 2.0 4 votes vote down vote up
@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);
        } 
    }
}
 
Example #6
Source File: SimpleTests.java    From elasticsearch-topk-plugin with Apache License 2.0 4 votes vote down vote up
@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);
        } 
    }
}