Java Code Examples for org.elasticsearch.search.aggregations.Aggregations#get()
The following examples show how to use
org.elasticsearch.search.aggregations.Aggregations#get() .
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: ElasticsearchHelper.java From herd with Apache License 2.0 | 6 votes |
/** * Returns the aggregation that is associated with the specified name. This method also validates that the retrieved aggregation exists. * * @param searchResponse the response of the search request * @param aggregationName the name of the aggregation * * @return the aggregation */ public Terms getAggregation(SearchResponse searchResponse, String aggregationName) { // Retrieve the aggregations from the search response. Aggregations aggregations = getAggregationsFromSearchResponse(searchResponse); // Retrieve the specified aggregation. Terms aggregation = aggregations.get(aggregationName); // Fail if retrieved aggregation is null. if (aggregation == null) { // Log the error along with the search response contents. LOGGER.error("Failed to retrieve \"{}\" aggregation from the search response. searchResponse={}", aggregationName, jsonHelper.objectToJson(searchResponse)); // Throw an exception. throw new IllegalStateException("Invalid search result."); } return aggregation; }
Example 2
Source File: StatsAction.java From foxtrot with Apache License 2.0 | 6 votes |
private List<BucketResponse<StatsValue>> buildNestedStats(List<String> nesting, Aggregations aggregations) { final String field = nesting.get(0); final List<String> remainingFields = (nesting.size() > 1) ? nesting.subList(1, nesting.size()) : new ArrayList<>(); Terms terms = aggregations.get(Utils.sanitizeFieldForAggregation(field)); List<BucketResponse<StatsValue>> bucketResponses = Lists.newArrayList(); for (Terms.Bucket bucket : terms.getBuckets()) { BucketResponse<StatsValue> bucketResponse = new BucketResponse<>(); bucketResponse.setKey(String.valueOf(bucket.getKey())); if (nesting.size() == 1) { bucketResponse.setResult(buildStatsValue(getParameter().getField(), bucket.getAggregations())); } else { bucketResponse.setBuckets(buildNestedStats(remainingFields, bucket.getAggregations())); } bucketResponses.add(bucketResponse); } return bucketResponses; }
Example 3
Source File: TrendAction.java From foxtrot with Apache License 2.0 | 6 votes |
private TrendResponse buildResponse(TrendRequest request, Aggregations aggregations) { String field = request.getField(); Map<String, List<TrendResponse.Count>> trendCounts = new TreeMap<>(); Terms terms = aggregations.get(Utils.sanitizeFieldForAggregation(field)); for(Terms.Bucket bucket : terms.getBuckets()) { final String key = String.valueOf(bucket.getKey()); List<TrendResponse.Count> counts = Lists.newArrayList(); Aggregations subAggregations = bucket.getAggregations(); Histogram histogram = subAggregations.get(Utils.getDateHistogramKey(request.getTimestamp())); for(Histogram.Bucket histogramBucket : histogram.getBuckets()) { if(!CollectionUtils.isNullOrEmpty(getParameter().getUniqueCountOn())) { String uniqueCountKey = Utils.sanitizeFieldForAggregation(getParameter().getUniqueCountOn()); Cardinality cardinality = histogramBucket.getAggregations() .get(uniqueCountKey); counts.add(new TrendResponse.Count(((DateTime)histogramBucket.getKey()).getMillis(), cardinality.getValue())); } else { counts.add(new TrendResponse.Count(((DateTime)histogramBucket.getKey()).getMillis(), histogramBucket.getDocCount())); } } trendCounts.put(key, counts); } return new TrendResponse(trendCounts); }
Example 4
Source File: AggregationTest.java From elasticsearch-sql with Apache License 2.0 | 6 votes |
@Test public void reverseToRootGroupByOnNestedFieldWithFilterTestWithReverseNestedAndEmptyPath() throws Exception { Aggregations result = query(String.format("SELECT COUNT(*) FROM %s/nestedType GROUP BY nested(message.info),filter('myFilter',message.info = 'a'),reverse_nested(someField,'')", TEST_INDEX_NESTED_TYPE)); InternalNested nested = result.get("message.info@NESTED"); InternalFilter filter = nested.getAggregations().get("myFilter@FILTER"); Terms infos = filter.getAggregations().get("message.info"); Assert.assertEquals(1,infos.getBuckets().size()); for(Terms.Bucket bucket : infos.getBuckets()) { InternalReverseNested reverseNested = bucket.getAggregations().get("someField@NESTED"); Terms terms = reverseNested.getAggregations().get("someField"); Terms.Bucket internalBucket = terms.getBuckets().get(0); long count = ((ValueCount) internalBucket.getAggregations().get("COUNT(*)")).getValue(); String key = internalBucket.getKey().toString(); if(key.equalsIgnoreCase("b")) { Assert.assertEquals(2, count); } else { throw new Exception(String.format("Unexpected key. expected: only a . found: %s", key)); } } }
Example 5
Source File: AggregationTest.java From elasticsearch-sql with Apache License 2.0 | 6 votes |
@Test public void groupByTestWithFilter() throws Exception { Aggregations result = query(String.format("SELECT COUNT(*) FROM %s/account GROUP BY filter(gender='m'),gender", TEST_INDEX_ACCOUNT)); InternalFilter filter = result.get("filter(gender = 'm')@FILTER"); Terms gender = filter.getAggregations().get("gender"); for(Terms.Bucket bucket : gender.getBuckets()) { String key = bucket.getKey().toString(); long count = ((ValueCount) bucket.getAggregations().get("COUNT(*)")).getValue(); if(key.equalsIgnoreCase("m")) { Assert.assertEquals(507, count); } else { throw new Exception(String.format("Unexpected key. expected: only m. found: %s", key)); } } }
Example 6
Source File: AggregationTest.java From elasticsearch-sql with Apache License 2.0 | 6 votes |
@Test public void multipleGroupBysWithSize() throws Exception { Set expectedAges = new HashSet<Integer>(ContiguousSet.create(Range.closed(20, 40), DiscreteDomain.integers())); Map<String, Set<Integer>> buckets = new HashMap<>(); Aggregations result = query(String.format("SELECT COUNT(*) FROM %s/account GROUP BY gender, terms('alias'='ageAgg','field'='age','size'=3)", TEST_INDEX_ACCOUNT)); Terms gender = result.get("gender"); Assert.assertEquals(2,gender.getBuckets().size()); for(Terms.Bucket genderBucket : gender.getBuckets()) { String genderKey = genderBucket.getKey().toString(); buckets.put(genderKey, new HashSet<Integer>()); Terms ageBuckets = genderBucket.getAggregations().get("ageAgg"); Assert.assertEquals(3,ageBuckets.getBuckets().size()); } }
Example 7
Source File: AggregationServiceImpl.java From microservices-platform with Apache License 2.0 | 6 votes |
/** * 赋值周趋势统计 */ private void setStatWeek(Map<String, Object> result, Aggregations aggregations) { ParsedDateHistogram agg = aggregations.get("statWeek"); List<String> items = new ArrayList<>(); List<Long> uv = new ArrayList<>(); List<Long> pv = new ArrayList<>(); Cardinality cardinality; for (Histogram.Bucket bucket : agg.getBuckets()) { items.add(bucket.getKeyAsString()); pv.add(bucket.getDocCount()); cardinality = bucket.getAggregations().get("uv"); uv.add(cardinality.getValue()); } result.put("statWeek_items", items); result.put("statWeek_uv", uv); result.put("statWeek_pv", pv); }
Example 8
Source File: AggregationServiceImpl.java From microservices-platform with Apache License 2.0 | 5 votes |
/** * 赋值单字段统计 */ private void setTermsData(Map<String, Object> result, Aggregations aggregations, String key) { Terms terms = aggregations.get(key); List<String> legendData = new ArrayList<>(); List<AggItemVo> datas = new ArrayList<>(); for (Terms.Bucket bucket : terms.getBuckets()) { legendData.add((String)bucket.getKey()); AggItemVo item = new AggItemVo(); item.setName((String)bucket.getKey()); item.setValue(bucket.getDocCount()); datas.add(item); } result.put(key+"_legendData", legendData); result.put(key+"_datas", datas); }
Example 9
Source File: AggregationTest.java From elasticsearch-sql with Apache License 2.0 | 5 votes |
@Test public void reverseToRootGroupByOnNestedFieldWithFilterAndSumOnReverseNestedField() throws Exception { Aggregations result = query(String.format("SELECT sum(reverse_nested(myNum)) bla FROM %s/nestedType GROUP BY nested(message.info),filter('myFilter',message.info = 'a')", TEST_INDEX_NESTED_TYPE)); InternalNested nested = result.get("message.info@NESTED"); InternalFilter filter = nested.getAggregations().get("myFilter@FILTER"); Terms infos = filter.getAggregations().get("message.info"); Assert.assertEquals(1,infos.getBuckets().size()); for(Terms.Bucket bucket : infos.getBuckets()) { InternalReverseNested reverseNested = bucket.getAggregations().get("myNum@NESTED"); InternalSum sum = reverseNested.getAggregations().get("bla"); Assert.assertEquals(5.0,sum.getValue(),0.000001); } }
Example 10
Source File: NpmSearchFacetHosted.java From nexus-public with Eclipse Public License 1.0 | 5 votes |
public Content searchV1(final Parameters parameters) throws IOException { String text = npmSearchParameterExtractor.extractText(parameters); int size = npmSearchParameterExtractor.extractSize(parameters); int from = npmSearchParameterExtractor.extractFrom(parameters); // npm search V1 endpoint currently returns an empty result set if no text is provided in the request NpmSearchResponse response; if (text.isEmpty()) { response = npmSearchResponseFactory.buildEmptyResponse(); } else { QueryStringQueryBuilder query = QueryBuilders.queryStringQuery(text) .allowLeadingWildcard(true) .analyzeWildcard(true); TermsBuilder terms = AggregationBuilders.terms("name") .field("assets.attributes.npm.name") .size(v1SearchMaxResults) .subAggregation(AggregationBuilders.topHits("versions") .addSort(SortBuilders.fieldSort("assets.attributes.npm.search_normalized_version") .order(SortOrder.DESC)) .setTrackScores(true) .setSize(1)); SearchResponse searchResponse = searchQueryService.search( repositoryQuery(query).inRepositories(getRepository()), singletonList(terms)); Aggregations aggregations = searchResponse.getAggregations(); Terms nameTerms = aggregations.get("name"); response = npmSearchResponseFactory.buildResponseForResults(nameTerms.getBuckets(), size, from); } String content = npmSearchResponseMapper.writeString(response); return new Content(new StringPayload(content, ContentTypes.APPLICATION_JSON)); }
Example 11
Source File: ElasticsearchHelper.java From herd with Apache License 2.0 | 5 votes |
/** * Returns the sub-aggregation that is associated with the specified nested aggregation. This method also validates that the retrieved sub-aggregation * exists. * * @param searchResponse the response of the search request * @param nestedAggregationName the name of the nested aggregation * @param subAggregationName the name of the sub-aggregation * * @return the aggregation */ public Terms getNestedAggregation(SearchResponse searchResponse, String nestedAggregationName, String subAggregationName) { // Retrieve the aggregations from the search response. Aggregations searchResponseAggregations = getAggregationsFromSearchResponse(searchResponse); // Retrieve the nested aggregation. Nested nestedAggregation = searchResponseAggregations.get(nestedAggregationName); // Fail if the retrieved nested aggregation is null. if (nestedAggregation == null) { // Log the error along with the search response contents. LOGGER.error("Failed to retrieve \"{}\" nested aggregation from the search response. searchResponse={}", nestedAggregationName, jsonHelper.objectToJson(searchResponse)); // Throw an exception. throw new IllegalStateException("Invalid search result."); } // Retrieve the aggregations from the nested aggregation. Aggregations nestedAggregationAggregations = getAggregationsFromNestedAggregation(nestedAggregation, searchResponse); // Retrieve the sub-aggregation. Terms subAggregation = nestedAggregationAggregations.get(subAggregationName); // Fail if retrieved sub-aggregation is null. if (subAggregation == null) { // Log the error along with the search response contents. LOGGER.error("Failed to retrieve \"{}\" sub-aggregation from \"{}\" nested aggregation. searchResponse={} nestedAggregation={}", subAggregationName, nestedAggregationName, jsonHelper.objectToJson(searchResponse), jsonHelper.objectToJson(nestedAggregation)); // Throw an exception. throw new IllegalStateException("Invalid search result."); } return subAggregation; }
Example 12
Source File: ElasticsearchSearchDao.java From metron with Apache License 2.0 | 5 votes |
private Map<String, Map<String, Long>> getFacetCounts(List<String> fields, Aggregations aggregations, Map<String, FieldType> commonColumnMetadata) { Map<String, Map<String, Long>> fieldCounts = new HashMap<>(); for (String field: fields) { Map<String, Long> valueCounts = new HashMap<>(); if(aggregations != null ) { Aggregation aggregation = aggregations.get(getFacetAggregationName(field)); if (aggregation instanceof Terms) { Terms terms = (Terms) aggregation; terms.getBuckets().stream().forEach(bucket -> valueCounts.put(formatKey(bucket.getKey(), commonColumnMetadata.get(field)), bucket.getDocCount())); } } fieldCounts.put(field, valueCounts); } return fieldCounts; }
Example 13
Source File: AggregationTest.java From elasticsearch-sql with Apache License 2.0 | 5 votes |
@Test public void reverseAnotherNestedGroupByOnNestedFieldWithFilterAndSumOnReverseNestedField() throws Exception { Aggregations result = query(String.format("SELECT sum(reverse_nested(comment.likes,'~comment')) bla FROM %s/nestedType GROUP BY nested(message.info),filter('myFilter',message.info = 'a')", TEST_INDEX_NESTED_TYPE)); InternalNested nested = result.get("message.info@NESTED"); InternalFilter filter = nested.getAggregations().get("myFilter@FILTER"); Terms infos = filter.getAggregations().get("message.info"); Assert.assertEquals(1,infos.getBuckets().size()); for(Terms.Bucket bucket : infos.getBuckets()) { InternalReverseNested reverseNested = bucket.getAggregations().get("comment.likes@NESTED_REVERSED"); InternalNested innerNested = reverseNested.getAggregations().get("comment.likes@NESTED"); InternalSum sum = innerNested.getAggregations().get("bla"); Assert.assertEquals(4.0,sum.getValue(),0.000001); } }
Example 14
Source File: AggregationTest.java From elasticsearch-sql with Apache License 2.0 | 5 votes |
@Test public void percentileTest() throws IOException, SqlParseException, SQLFeatureNotSupportedException { Aggregations result = query(String.format("SELECT PERCENTILES(age) FROM %s/account", TEST_INDEX_ACCOUNT)); Percentiles percentiles = result.get("PERCENTILES(age)"); Assert.assertTrue(Math.abs(percentiles.percentile(1.0) - 20.0) < 0.001 ); Assert.assertTrue(Math.abs(percentiles.percentile(5.0) - 21.0) < 0.001 ); Assert.assertTrue(Math.abs(percentiles.percentile(25.0) - 25.0) < 0.001 ); Assert.assertTrue(Math.abs(percentiles.percentile(75.0) - 35.0) < 0.001 ); Assert.assertTrue(Math.abs(percentiles.percentile(95.0) - 39.0) < 0.001 ); Assert.assertTrue(Math.abs(percentiles.percentile(99.0) - 40.0) < 0.001 ); }
Example 15
Source File: AggregationTest.java From elasticsearch-sql with Apache License 2.0 | 5 votes |
@Test public void termsWithOrder() throws Exception { Aggregations result = query(String.format("SELECT count(*) FROM %s/dog GROUP BY terms('field'='dog_name', 'alias'='dog_name', order='desc')", TEST_INDEX_DOG)); Terms name = result.get("dog_name"); Assert.assertEquals("snoopy",name.getBuckets().get(0).getKeyAsString()); Assert.assertEquals("rex",name.getBuckets().get(1).getKeyAsString()); result = query(String.format("SELECT count(*) FROM %s/dog GROUP BY terms('field'='dog_name', 'alias'='dog_name', order='asc')", TEST_INDEX_DOG)); name = result.get("dog_name"); Assert.assertEquals("rex",name.getBuckets().get(0).getKeyAsString()); Assert.assertEquals("snoopy",name.getBuckets().get(1).getKeyAsString()); }
Example 16
Source File: AggregationTest.java From elasticsearch-sql with Apache License 2.0 | 5 votes |
@Test public void orderByAscTest() throws IOException, SqlParseException, SQLFeatureNotSupportedException { ArrayList<Long> agesCount = new ArrayList<>(); Aggregations result = query(String.format("SELECT COUNT(*) FROM %s/account GROUP BY age ORDER BY COUNT(*)", TEST_INDEX_ACCOUNT)); Terms age = result.get("age"); for(Terms.Bucket bucket : age.getBuckets()) { agesCount.add(((ValueCount) bucket.getAggregations().get("COUNT(*)")).getValue()); } ArrayList<Long> sortedAgesCount = (ArrayList<Long>)agesCount.clone(); Collections.sort(sortedAgesCount); Assert.assertTrue("The list is not ordered ascending", agesCount.equals(agesCount)); }
Example 17
Source File: AggregationServiceImpl.java From microservices-platform with Apache License 2.0 | 4 votes |
/** * 赋值月统计 */ private void setCurrMonth(Map<String, Object> result, Aggregations aggregations) { ParsedDateRange currMonth = aggregations.get("currMonth"); Range.Bucket bucket = currMonth.getBuckets().get(0); result.put("currMonth_pv", bucket.getDocCount()); }
Example 18
Source File: AggregationTest.java From elasticsearch-sql with Apache License 2.0 | 4 votes |
@Test public void countTest() throws IOException, SqlParseException, SQLFeatureNotSupportedException { Aggregations result = query(String.format("SELECT COUNT(*) FROM %s/account", TEST_INDEX_ACCOUNT)); ValueCount count = result.get("COUNT(*)"); Assert.assertEquals(1000, count.getValue()); }
Example 19
Source File: AggregationTest.java From elasticsearch-sql with Apache License 2.0 | 4 votes |
@Test public void minTest() throws IOException, SqlParseException, SQLFeatureNotSupportedException { Aggregations result = query(String.format("SELECT MIN(age) FROM %s/account", TEST_INDEX_ACCOUNT)); Min min = result.get("MIN(age)"); assertThat(min.getValue(), equalTo(20.0)); }
Example 20
Source File: AggregationTest.java From elasticsearch-sql with Apache License 2.0 | 3 votes |
@Test public void termsWithSize() throws Exception { Map<String, Set<Integer>> buckets = new HashMap<>(); Aggregations result = query(String.format("SELECT COUNT(*) FROM %s/account GROUP BY terms('alias'='ageAgg','field'='age','size'=3)", TEST_INDEX_ACCOUNT)); Terms gender = result.get("ageAgg"); Assert.assertEquals(3,gender.getBuckets().size()); }