Java Code Examples for org.elasticsearch.search.aggregations.Aggregation#getName()
The following examples show how to use
org.elasticsearch.search.aggregations.Aggregation#getName() .
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: SearchAggregationParser.java From sql4es with Apache License 2.0 | 4 votes |
/** * Parse an aggregation result based on one or more aggregated terms * @param terms * @param rs * @param row * @throws SQLException */ private void dfsAggregations(Terms terms, ESResultSet rs, List<Object> row) throws SQLException{ List<Object> currentRow = Utils.clone(row); String columnName = terms.getName(); if(!rs.getHeading().hasLabel(columnName)) throw new SQLException("Unable to identify column for aggregation named "+columnName); Column aggCol = rs.getHeading().getColumnByLabel(columnName); for(Terms.Bucket bucket : terms.getBuckets()){ if (bucket instanceof StringTerms.Bucket) { aggCol.setSqlType(Types.VARCHAR); } else if (bucket instanceof LongTerms.Bucket) { aggCol.setSqlType(Types.TIMESTAMP); //ToDO: chack Timestamp } boolean metricAggs = false; List<Aggregation> aggs = bucket.getAggregations().asList(); if(aggs.size() == 0){ currentRow.set(aggCol.getIndex(), bucket.getKey()); metricAggs = true; }else for(Aggregation agg : bucket.getAggregations().asList()){ if(agg instanceof Terms){ currentRow.set(aggCol.getIndex(), bucket.getKey()); dfsAggregations((Terms)agg, rs, currentRow); }else{ if(metricAggs == false){ currentRow.set(aggCol.getIndex(), bucket.getKey()); metricAggs = true; } String metricName = agg.getName(); if(!rs.getHeading().hasLabel(metricName)) throw new SQLException("Unable to identify column for aggregation named "+metricName); Column metricCol = rs.getHeading().getColumnByLabel(metricName); // ToDo: check it if (agg instanceof InternalAvg) { currentRow.set(metricCol.getIndex(), ((InternalAvg) agg).getValue()); } else if (agg instanceof InternalCardinality) { currentRow.set(metricCol.getIndex(), ((InternalCardinality) agg).getValue()); } else if (agg instanceof InternalMax) { currentRow.set(metricCol.getIndex(), ((InternalMax) agg).getValue()); } else if (agg instanceof InternalMin) { currentRow.set(metricCol.getIndex(), ((InternalMin) agg).getValue()); } else if (agg instanceof Percentile) { currentRow.set(metricCol.getIndex(), ((Percentile) agg).getValue()); } else if (agg instanceof InternalSum) { currentRow.set(metricCol.getIndex(), ((InternalSum) agg).getValue()); } else if (agg instanceof InternalValueCount) { currentRow.set(metricCol.getIndex(), ((InternalValueCount) agg).getValue()); } else if (agg instanceof InternalNumericMetricsAggregation.SingleValue) { currentRow.set(metricCol.getIndex(), ((InternalNumericMetricsAggregation.SingleValue) agg).getValueAsString()); } else { // ToDo: I don't know ( currentRow.set(metricCol.getIndex(), agg.getName()); } } } if(metricAggs){ rs.add(currentRow); currentRow = Utils.clone(row); } currentRow = Utils.clone(row); } }
Example 2
Source File: ObjectResultsExtractor.java From elasticsearch-sql with Apache License 2.0 | 4 votes |
private void handleNumericMetricAggregation(List<String> header, List<Object> line, Aggregation aggregation) throws ObjectResultsExtractException { String name = aggregation.getName(); if (aggregation instanceof NumericMetricsAggregation.SingleValue) { if (!header.contains(name)) { header.add(name); } line.add(((NumericMetricsAggregation.SingleValue) aggregation).value()); } //todo:Numeric MultiValue - Stats,ExtendedStats,Percentile... else if (aggregation instanceof NumericMetricsAggregation.MultiValue) { if (aggregation instanceof Stats) { String[] statsHeaders = new String[]{"count", "sum", "avg", "min", "max"}; boolean isExtendedStats = aggregation instanceof ExtendedStats; if (isExtendedStats) { String[] extendedHeaders = new String[]{"sumOfSquares", "variance", "stdDeviation"}; statsHeaders = Util.concatStringsArrays(statsHeaders, extendedHeaders); } mergeHeadersWithPrefix(header, name, statsHeaders); Stats stats = (Stats) aggregation; line.add(stats.getCount()); line.add(stats.getSum()); line.add(stats.getAvg()); line.add(stats.getMin()); line.add(stats.getMax()); if (isExtendedStats) { ExtendedStats extendedStats = (ExtendedStats) aggregation; line.add(extendedStats.getSumOfSquares()); line.add(extendedStats.getVariance()); line.add(extendedStats.getStdDeviation()); } } else if (aggregation instanceof Percentiles) { String[] percentileHeaders = new String[]{"1.0", "5.0", "25.0", "50.0", "75.0", "95.0", "99.0"}; mergeHeadersWithPrefix(header, name, percentileHeaders); Percentiles percentiles = (Percentiles) aggregation; line.add(percentiles.percentile(1.0)); line.add(percentiles.percentile(5.0)); line.add(percentiles.percentile(25.0)); line.add(percentiles.percentile(50.0)); line.add(percentiles.percentile(75)); line.add(percentiles.percentile(95.0)); line.add(percentiles.percentile(99.0)); } else { throw new ObjectResultsExtractException("unknown NumericMetricsAggregation.MultiValue:" + aggregation.getClass()); } } else { throw new ObjectResultsExtractException("unknown NumericMetricsAggregation" + aggregation.getClass()); } }
Example 3
Source File: SimpleTests.java From elasticsearch-topk-plugin with Apache License 2.0 | 4 votes |
@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 |
@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 |
@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); } } }