org.elasticsearch.search.aggregations.metrics.cardinality.Cardinality Java Examples

The following examples show how to use org.elasticsearch.search.aggregations.metrics.cardinality.Cardinality. 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: AggregationServiceImpl.java    From microservices-platform with Apache License 2.0 6 votes vote down vote up
/**
 * 赋值周趋势统计
 */
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 #2
Source File: AggregationServiceImpl.java    From microservices-platform with Apache License 2.0 6 votes vote down vote up
/**
 * 赋值天趋势统计
 */
private void setStatDate(Map<String, Object> result, Aggregations aggregations) {
    ParsedDateHistogram agg = aggregations.get("statDate");
    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(getTimeByDatetimeStr(bucket.getKeyAsString()));
        pv.add(bucket.getDocCount());

        cardinality = bucket.getAggregations().get("uv");
        uv.add(cardinality.getValue());
    }
    result.put("statDate_items", items);
    result.put("statDate_uv", uv);
    result.put("statDate_pv", pv);
}
 
Example #3
Source File: CardinalityEstimationTask.java    From siren-join with GNU Affero General Public License v3.0 6 votes vote down vote up
protected void executeCardinalityRequest(final NodeTaskContext context, final NodeTaskReporter reporter) {
  logger.debug("Executing async cardinality action");
  final SearchRequest cardinalityRequest = this.getCardinalityRequest(context.getNode(), context.getVisitor().getParentRequest());
  context.getClient().execute(SearchAction.INSTANCE, cardinalityRequest, new ActionListener<SearchResponse>() {

    @Override
    public void onResponse(SearchResponse searchResponse) {
      Cardinality c = searchResponse.getAggregations().get(context.getNode().getLookupPath());
      context.getNode().setCardinality(c.getValue());
      reporter.success(context);
    }

    @Override
    public void onFailure(Throwable e) {
      reporter.failure(e);
    }

  });
}
 
Example #4
Source File: Search.java    From elasticsearch-rest-command with The Unlicense 6 votes vote down vote up
private long executeCardinary(String field) throws IOException{
	
	SearchResponse cardResponse = cardSearch.setAggregations(
			JsonXContent.contentBuilder()
			.startObject()
               	.startObject("LIMIT")
               		.startObject("cardinality")
               			.field("field", field)
               		.endObject()
               	.endObject()
			).get();

	Cardinality limit = cardResponse.getAggregations().get("LIMIT");
	
	return limit.getValue();		
}
 
Example #5
Source File: GroupAction.java    From foxtrot with Apache License 2.0 6 votes vote down vote up
private Map<String, Object> getMap(List<String> fields, Aggregations aggregations) {
    final String field = fields.get(0);
    final List<String> remainingFields = (fields.size() > 1)
                                         ? fields.subList(1, fields.size())
                                         : new ArrayList<>();
    Terms terms = aggregations.get(Utils.sanitizeFieldForAggregation(field));
    Map<String, Object> levelCount = Maps.newHashMap();
    for (Terms.Bucket bucket : terms.getBuckets()) {
        if (fields.size() == 1) {
            if (!CollectionUtils.isNullOrEmpty(getParameter().getUniqueCountOn())) {
                String key = Utils.sanitizeFieldForAggregation(getParameter().getUniqueCountOn());
                Cardinality cardinality = bucket.getAggregations()
                        .get(key);
                levelCount.put(String.valueOf(bucket.getKey()), cardinality.getValue());
            }
            else {
                levelCount.put(String.valueOf(bucket.getKey()), bucket.getDocCount());
            }
        }
        else {
            levelCount.put(String.valueOf(bucket.getKey()), getMap(remainingFields, bucket.getAggregations()));
        }
    }
    return levelCount;

}
 
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: CountAction.java    From foxtrot with Apache License 2.0 6 votes vote down vote up
@Override
public ActionResponse getResponse(org.elasticsearch.action.ActionResponse response, CountRequest parameter) {
    if (parameter.isDistinct()) {
        Aggregations aggregations = ((SearchResponse) response).getAggregations();
        Cardinality cardinality = aggregations.get(Utils.sanitizeFieldForAggregation(parameter.getField()));
        if (cardinality == null) {
            return new CountResponse(0);
        }
        else {
            return new CountResponse(cardinality.getValue());
        }
    }
    else {
        return new CountResponse(((SearchResponse) response).getHits()
                                         .getTotalHits());
    }

}
 
Example #8
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 #9
Source File: AggregationServiceImpl.java    From microservices-platform with Apache License 2.0 5 votes vote down vote up
/**
 * 赋值当天统计
 */
private void setCurrDate(Map<String, Object> result, Aggregations aggregations) {
    ParsedDateRange currDate = aggregations.get("currDate");
    Range.Bucket bucket = currDate.getBuckets().get(0);
    Cardinality cardinality = bucket.getAggregations().get("uv");
    result.put("currDate_pv", bucket.getDocCount());
    result.put("currDate_uv", cardinality.getValue());
}
 
Example #10
Source File: AggregationServiceImpl.java    From microservices-platform with Apache License 2.0 5 votes vote down vote up
/**
 * 赋值小时内统计-当前在线数
 */
private void setCurrHour(Map<String, Object> result, Aggregations aggregations) {
    ParsedDateRange currDate = aggregations.get("currHour");
    Range.Bucket bucket = currDate.getBuckets().get(0);
    Cardinality cardinality = bucket.getAggregations().get("uv");
    result.put("currHour_uv", cardinality.getValue());
}
 
Example #11
Source File: FactSearchManager.java    From act-platform with ISC License 5 votes vote down vote up
private int retrieveSearchObjectsResultCount(SearchResponse response) {
  Aggregation objectsCountAggregation = resolveChildAggregation(response.getAggregations(), OBJECTS_COUNT_AGGREGATION_NAME);
  if (!(objectsCountAggregation instanceof Cardinality)) {
    LOGGER.warning("Could not retrieve result count when searching for Objects.");
    return -1;
  }

  // Retrieve Object count from the cardinality aggregation.
  return (int) Cardinality.class.cast(objectsCountAggregation).getValue();
}
 
Example #12
Source File: Search.java    From elasticsearch-rest-command with The Unlicense 5 votes vote down vote up
public static String getValueFromAggregation(Aggregation a, Function f){
	
	String value = null;
	switch(f.type){
	case Function.SUM :
		value = String.valueOf(((Sum) a).getValue());
		break;
	case Function.COUNT :
		value = String.valueOf(((ValueCount) a).getValue());
		break;
	case Function.DC :
		value = String.valueOf(((Cardinality) a).getValue());
		break;
	case Function.AVG :
		value = String.valueOf(((Avg) a).getValue());
		break;
	case Function.MAX :
		value = String.valueOf(((Max) a).getValue());
		break;
	case Function.MIN :
		value = String.valueOf(((Min) a).getValue());
		break;
	}
	
	return value;
	
	
}
 
Example #13
Source File: DistributedTableMetadataManager.java    From foxtrot with Apache License 2.0 5 votes vote down vote up
private void evaluateStringEstimation(Aggregation value, String table, String key, FieldType type,
                                      Map<String, EstimationData> estimationDataMap, long hits) {
    Cardinality cardinality = (Cardinality)value;
    logger.info("table:{} field:{} type:{} aggregationType:{} value:{} ", table, key, type,
            CARDINALITY, cardinality.getValue()
    );
    estimationDataMap.put(key, CardinalityEstimationData.builder()
            .cardinality(cardinality.getValue())
            .count(hits)
            .build());
}
 
Example #14
Source File: DistributedTableMetadataManager.java    From foxtrot with Apache License 2.0 5 votes vote down vote up
private void evaluateDoubleEstimation(Aggregation value, String table, String key, FieldType type,
                                      Map<String, EstimationData> estimationDataMap, long hits) {
    if(value instanceof Percentiles) {
        Percentiles percentiles = (Percentiles)value;
        double[] values = new double[10];
        for(int i = 10; i <= 100; i += 10) {
            final Double percentile = percentiles.percentile(i);
            values[(i / 10) - 1] = percentile.isNaN() ? 0 : percentile;
        }
        logger.info("table:{} field:{} type:{} aggregationType:{} value:{}", table, key, type,
                "percentile", values
        );
        estimationDataMap.put(key, PercentileEstimationData.builder()
                .values(values)
                .count(hits)
                .build());
    } else if(value instanceof Cardinality) {
        Cardinality cardinality = (Cardinality) value;
        logger.info("table:{} field:{} type:{} aggregationType:{} value:{}", table, key, type,
                CARDINALITY, cardinality.getValue()
        );
        EstimationData estimationData = estimationDataMap.get(key.replace("_", ""));
        if (estimationData instanceof PercentileEstimationData) {
            ((PercentileEstimationData) estimationData).setCardinality(cardinality.getValue());
        } else {
            estimationDataMap.put(key.replace("_", ""), PercentileEstimationData.builder()
                    .cardinality(cardinality.getValue())
                    .build());
        }
    }
}
 
Example #15
Source File: AggregateResponseParser.java    From molgenis with GNU Lesser General Public License v3.0 5 votes vote down vote up
private Cardinality getDistinctAggregation(Aggregations aggs, Attribute attr) {
  Aggregation agg = aggs.get(attr.getName() + FieldConstants.AGGREGATION_DISTINCT_POSTFIX);
  if (agg == null) {
    throw new RuntimeException("Missing cardinality aggregation");
  }
  if (!(agg instanceof Cardinality)) {
    throw new RuntimeException("Aggregation is not a cardinality aggregation");
  }
  return (Cardinality) agg;
}