org.elasticsearch.search.aggregations.bucket.histogram.Histogram Java Examples

The following examples show how to use org.elasticsearch.search.aggregations.bucket.histogram.Histogram. 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: AggregationResponseHandle.java    From jetlinks-community with Apache License 2.0 6 votes vote down vote up
private static <A extends Aggregation> void route(Bucket bucket, A a) {
    if (a instanceof Terms) {
        bucket.setBuckets(terms(a));
    } else if (a instanceof Range) {
        bucket.setBuckets(range(a));
    } else if (a instanceof Histogram) {
        bucket.setBuckets(range(a));
    } else if (a instanceof Avg) {
        bucket.setAvg(avg(a));
    } else if (a instanceof Min) {
        bucket.setMin(min(a));
    } else if (a instanceof Max) {
        bucket.setMax(max(a));
    } else if (a instanceof Sum) {
        bucket.setSum(sum(a));
    } else if (a instanceof Stats) {
        stats(bucket, a);
    }  else if (a instanceof ValueCount) {
        bucket.setValueCount(count(a));
    } else {
        throw new UnsupportedOperationException("不支持的聚合类型");
    }
}
 
Example #2
Source File: SalesTrendsQueryAdapter.java    From micronaut-microservices-poc with Apache License 2.0 6 votes vote down vote up
@Override
SalesTrendsQuery.Result extractResult(SearchResponse searchResponse) {
    SalesTrendsQuery.Result.ResultBuilder result = SalesTrendsQuery.Result.builder();

    Filter filterAgg = searchResponse.getAggregations().get("agg_filter");
    Histogram agg = filterAgg.getAggregations().get("sales");
    for (Histogram.Bucket b : agg.getBuckets()){
        DateTime key = (DateTime)b.getKey();
        Sum sum = b.getAggregations().get("total_premium");
        result.periodSale(
                new SalesTrendsQuery.PeriodSales(
                        LocalDate.of(key.getYear(),key.getMonthOfYear(),key.getDayOfMonth()),
                        b.getKeyAsString(),
                        SalesResult.of(b.getDocCount(), BigDecimal.valueOf(sum.getValue()).setScale(2, BigDecimal.ROUND_HALF_UP))
                )
        );
    }

    return result.build();
}
 
Example #3
Source File: CommonWebpageDAO.java    From Gather-Platform with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 统计指定网站每天抓取数量
 *
 * @param domain 网站域名
 * @return
 */
public Map<Date, Long> countDomainByGatherTime(String domain) {
    AggregationBuilder aggregation =
            AggregationBuilders
                    .dateHistogram("agg")
                    .field("gatherTime")
                    .dateHistogramInterval(DateHistogramInterval.DAY).order(Histogram.Order.KEY_DESC);
    SearchRequestBuilder searchRequestBuilder = client.prepareSearch(INDEX_NAME)
            .setTypes(TYPE_NAME)
            .setQuery(QueryBuilders.matchQuery("domain", domain))
            .addAggregation(aggregation);
    SearchResponse response = searchRequestBuilder.execute().actionGet();
    Histogram agg = response.getAggregations().get("agg");
    Map<Date, Long> result = Maps.newHashMap();
    for (Histogram.Bucket entry : agg.getBuckets()) {
        DateTime key = (DateTime) entry.getKey();    // Key
        long docCount = entry.getDocCount();         // Doc count
        result.put(key.toDate(), docCount);
    }
    return result;
}
 
Example #4
Source File: CommonWebpageDAO.java    From spider with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 统计指定网站每天抓取数量
 *
 * @param domain 网站域名
 * @return
 */
public Map<Date, Long> countDomainByGatherTime(String domain) {
    AggregationBuilder aggregation =
            AggregationBuilders
                    .dateHistogram("agg")
                    .field("gatherTime")
                    .dateHistogramInterval(DateHistogramInterval.DAY).order(Histogram.Order.KEY_DESC);
    SearchRequestBuilder searchRequestBuilder = client.prepareSearch(INDEX_NAME)
            .setTypes(TYPE_NAME)
            .setQuery(QueryBuilders.matchQuery("domain", domain))
            .addAggregation(aggregation);
    SearchResponse response = searchRequestBuilder.execute().actionGet();
    Histogram agg = response.getAggregations().get("agg");
    Map<Date, Long> result = Maps.newHashMap();
    for (Histogram.Bucket entry : agg.getBuckets()) {
        DateTime key = (DateTime) entry.getKey();    // Key
        long docCount = entry.getDocCount();         // Doc count
        result.put(key.toDate(), docCount);
    }
    return result;
}
 
Example #5
Source File: AggregationTest.java    From elasticsearch-sql with Apache License 2.0 6 votes vote down vote up
@Test
public void histogramOnNestedField() throws Exception {
    Aggregations result = query(String.format("select count(*) from %s/nestedType group by histogram('field'='message.dayOfWeek','nested'='message','interval'='2' , 'alias' = 'someAlias' )", TEST_INDEX_NESTED_TYPE));
    InternalNested nested  = result.get("message@NESTED");
    Histogram histogram = nested.getAggregations().get("someAlias");
    for(Histogram.Bucket bucket : histogram.getBuckets()){
        long count = ((ValueCount) bucket.getAggregations().get("COUNT(*)")).getValue();
        String key = ((Double)bucket.getKey()).intValue()+"";
        if(key.equals("0") || key.equals("4")){
            Assert.assertEquals(2,count);
        }
        else if (key.equals("2")){
            Assert.assertEquals(1,count);
        }
        else{
            Assert.assertTrue("only 0 2 4 keys are allowed got:" + key,false);
        }
    }


}
 
Example #6
Source File: HistogramAction.java    From foxtrot with Apache License 2.0 6 votes vote down vote up
private HistogramResponse buildResponse(Aggregations aggregations) {
    if (aggregations == null) {
        return new HistogramResponse(Collections.<HistogramResponse.Count>emptyList());
    }


    String dateHistogramKey = Utils.getDateHistogramKey(getParameter().getField());
    Histogram dateHistogram = aggregations.get(dateHistogramKey);
    Collection<? extends Histogram.Bucket> buckets = dateHistogram.getBuckets();
    List<HistogramResponse.Count> counts = new ArrayList<>(buckets.size());
    for (Histogram.Bucket bucket : buckets) {
        if (!CollectionUtils.isNullOrEmpty(getParameter().getUniqueCountOn())) {
            String key = Utils.sanitizeFieldForAggregation(getParameter().getUniqueCountOn());
            Cardinality cardinality = bucket.getAggregations()
                    .get(key);
            counts.add(new HistogramResponse.Count(((DateTime) bucket.getKey()).getMillis(),
                                                   cardinality.getValue()));
        }
        else {
            counts.add(new HistogramResponse.Count(((DateTime) bucket.getKey()).getMillis(), bucket.getDocCount()));
        }
    }
    return new HistogramResponse(counts);
}
 
Example #7
Source File: TrendAction.java    From foxtrot with Apache License 2.0 6 votes vote down vote up
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 #8
Source File: ElasticsearchDataQuery.java    From frostmourne with MIT License 5 votes vote down vote up
private ElasticsearchDataResult parseResult(SearchResponse searchResponse, String timestampField) {
    ElasticsearchDataResult dataResult = new ElasticsearchDataResult();
    dataResult.setTimestampField(timestampField);
    dataResult.setScrollId(searchResponse.getScrollId());
    dataResult.setTotal(searchResponse.getHits().getTotalHits());
    List<Map<String, Object>> logs = new ArrayList<>();
    for (SearchHit hit : searchResponse.getHits()) {
        logs.add(hit.getSourceAsMap());
        if (dataResult.getFields() == null) {
            Set<String> keys = hit.getSourceAsMap().keySet();
            List<String> fields = new ArrayList<>(keys);
            dataResult.setFields(fields);
            if (fields.size() > 7) {
                dataResult.setHeadFields(fields.subList(0, 6));
            } else {
                dataResult.setHeadFields(fields);
            }
        }
    }
    dataResult.setLogs(logs);
    if (searchResponse.getAggregations() == null) {
        return dataResult;
    }
    Histogram dateHistogram = searchResponse.getAggregations().get("date_hist");
    if (dateHistogram != null) {
        StatItem statItem = new StatItem();
        for (Histogram.Bucket bucket : dateHistogram.getBuckets()) {
            statItem.getKeys().add(bucket.getKeyAsString());
            statItem.getValues().add((double) bucket.getDocCount());
        }
        dataResult.setStatItem(statItem);
    }

    return dataResult;
}
 
Example #9
Source File: AggregationResponseHandle.java    From jetlinks-community with Apache License 2.0 5 votes vote down vote up
private static List<Bucket> bucketsHandle(List<? extends Histogram.Bucket> buckets, String name) {
    return buckets
        .stream()
        .map(b -> {
            Bucket bucket = Bucket.builder()
                .key(b.getKeyAsString())
                .count(b.getDocCount())
                .name(name)
                .build();
            b.getAggregations().asList()
                .forEach(subAggregation -> route(bucket, subAggregation));
            return bucket;
        }).collect(Collectors.toList())
        ;
}
 
Example #10
Source File: BaseDemo.java    From Elasticsearch-Tutorial-zh-CN with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 聚合分析
 * 1. 先分组
 * 2. 子分组
 * 3. 最后算出子分组的平均值
 *
 * @param transportClient
 * @throws IOException
 */
private static void aggregate(TransportClient transportClient) throws IOException {

	SearchResponse searchResponse = transportClient.prepareSearch("product_index").setTypes("product")
			.addAggregation(AggregationBuilders.terms("product_group_by_price").field("price")
					.subAggregation(AggregationBuilders.dateHistogram("product_group_by_created_date_time").field("created_date_time")
							.dateHistogramInterval(DateHistogramInterval.YEAR)
							.subAggregation(AggregationBuilders.avg("product_avg_price").field("price")))
			).execute().actionGet();

	Map<String, Aggregation> aggregationMap = searchResponse.getAggregations().asMap();

	StringTerms productGroupByPrice = (StringTerms) aggregationMap.get("product_group_by_price");
	Iterator<Terms.Bucket> productGroupByPriceIterator = productGroupByPrice.getBuckets().iterator();
	while (productGroupByPriceIterator.hasNext()) {
		Terms.Bucket productGroupByPriceBucket = productGroupByPriceIterator.next();
		logger.info("--------------------------------:" + productGroupByPriceBucket.getKey() + ":" + productGroupByPriceBucket.getDocCount());

		Histogram productGroupByPrice1 = (Histogram) productGroupByPriceBucket.getAggregations().asMap().get("product_group_by_price");
		Iterator<org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket> groupByCreateDateTimeIterator = productGroupByPrice1.getBuckets().iterator();
		while (groupByCreateDateTimeIterator.hasNext()) {
			Histogram.Bucket groupByCreateDateTimeBucket = groupByCreateDateTimeIterator.next();
			logger.info("--------------------------------:" + groupByCreateDateTimeBucket.getKey() + ":" + groupByCreateDateTimeBucket.getDocCount());

			Avg avgPrice = (Avg) groupByCreateDateTimeBucket.getAggregations().asMap().get("product_avg_price");
			logger.info("--------------------------------:" + avgPrice.getValue());
		}
	}


}
 
Example #11
Source File: StatsTrendAction.java    From foxtrot with Apache License 2.0 5 votes vote down vote up
private List<StatsTrendValue> buildStatsTrendValue(String field, Aggregations aggregations) {
    String dateHistogramKey = Utils.getDateHistogramKey(getParameter().getTimestamp());
    Histogram dateHistogram = aggregations.get(dateHistogramKey);
    Collection<? extends Histogram.Bucket> buckets = dateHistogram.getBuckets();
    List<StatsTrendValue> statsValueList = Lists.newArrayList();

    final String metricKey = Utils.getExtendedStatsAggregationKey(field);
    final String percentileMetricKey = Utils.getPercentileAggregationKey(field);

    for (Histogram.Bucket bucket : buckets) {
        StatsTrendValue statsTrendValue = new StatsTrendValue();
        DateTime key = (DateTime) bucket.getKey();
        statsTrendValue.setPeriod(key.getMillis());

        val statAggregation = bucket.getAggregations()
                .getAsMap()
                .get(metricKey);
        statsTrendValue.setStats(Utils.toStats(statAggregation));
        final Aggregation rawPercentiles = bucket.getAggregations()
                .getAsMap()
                .get(percentileMetricKey);
        if (null != rawPercentiles) {
            Percentiles internalPercentile = Percentiles.class.cast(rawPercentiles);
            statsTrendValue.setPercentiles(Utils.createPercentilesResponse(internalPercentile));
        }
        statsValueList.add(statsTrendValue);
    }
    return statsValueList;
}
 
Example #12
Source File: AggregationResponseHandle.java    From jetlinks-community with Apache License 2.0 4 votes vote down vote up
public static <A extends Aggregation> List<Bucket> dateHistogram(A a) {
    Histogram histogram = (Histogram) a;
    return bucketsHandle(histogram.getBuckets(), a.getName());
}