org.elasticsearch.search.aggregations.metrics.max.MaxBuilder Java Examples

The following examples show how to use org.elasticsearch.search.aggregations.metrics.max.MaxBuilder. 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: AST_Stats.java    From elasticsearch-rest-command with The Unlicense 5 votes vote down vote up
public static AbstractAggregationBuilder newMax(Function func) {
	MaxBuilder max;
	if (func.fieldtype == Field.SCRIPT)
		max = AggregationBuilders.max(Function.genStatField(func))
				.script(func.field);
	else
		max = AggregationBuilders.max(Function.genStatField(func))
				.field(func.field);
	return max;
}
 
Example #2
Source File: AggregationBuilders.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
/**
 * Create a new {@link Max} aggregation with the given name.
 */
public static MaxBuilder max(String name) {
    return new MaxBuilder(name);
}
 
Example #3
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 #4
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 #5
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);
        } 
    }
}