public SearchResult(SearchResponse resp, Select select) throws SqlParseException { Aggregations aggs = resp.getAggregations(); if (aggs.get("filter") != null) { InternalFilter inf = aggs.get("filter"); aggs = inf.getAggregations(); } if (aggs.get("group by") != null) { InternalTerms terms = aggs.get("group by"); Collection<Bucket> buckets = terms.getBuckets(); this.total = buckets.size(); results = new ArrayList<>(buckets.size()); for (Bucket bucket : buckets) { Map<String, Object> aggsMap = toAggsMap(bucket.getAggregations().getAsMap()); aggsMap.put("docCount", bucket.getDocCount()); results.add(aggsMap); } } else { results = new ArrayList<>(1); this.total = 1; Map<String, Object> map = new HashMap<>(); for (Aggregation aggregation : aggs) { map.put(aggregation.getName(), covenValue(aggregation)); } results.add(map); } }
// Response helpers protected Multimap<String, FacetValue> processAggregations(Aggregations aggregations) { Multimap<String, FacetValue> stats = ArrayListMultimap.create(); if (aggregations != null) { for (Aggregation aggregation : aggregations.asList()) { if (aggregation instanceof StringTerms) { for (Terms.Bucket value : ((Terms) aggregation).getBuckets()) { FacetValue facetValue = new FacetValue(value.getKey(), value.getDocCount()); stats.put(aggregation.getName(), facetValue); } } else if (aggregation instanceof InternalValueCount) { InternalValueCount count = (InternalValueCount) aggregation; FacetValue facetValue = new FacetValue(count.getName(), count.getValue()); stats.put(count.getName(), facetValue); } } } return stats; }
private Object covenValue(Aggregation value) throws SqlParseException { if (value instanceof InternalNumericMetricsAggregation.SingleValue) { return ((InternalNumericMetricsAggregation.SingleValue) value).value(); } else if (value instanceof InternalValueCount) { return ((InternalValueCount) value).getValue(); } else if (value instanceof InternalTopHits) { return (value); } else if (value instanceof LongTerms) { return value; } else { throw new SqlParseException("unknow this agg type " + value.getClass()); } }