return AggregationBuilders.cardinality(field.getAlias()).field(field.getParams().get(0).value.toString()); } else { Integer precision_threshold = (Integer) (field.getParams().get(1).value); return AggregationBuilders.cardinality(field.getAlias()).precisionThreshold(precision_threshold).field(field.getParams().get(0).value.toString());
searchSourceBuilder.aggregation(AggregationBuilders.cardinality(AGG_CARDINALITY).field(field));
dateHistogramBuilder.subAggregation(AggregationBuilders.cardinality(AGG_CARDINALITY).field(field));
protected List<AggregationBuilder> getElasticsearchCardinalityAggregations(CardinalityAggregation agg) { List<AggregationBuilder> cardinalityAggs = new ArrayList<>(); String fieldName = agg.getPropertyName(); if (Element.ID_PROPERTY_NAME.equals(fieldName) || Edge.LABEL_PROPERTY_NAME.equals(fieldName) || Edge.OUT_VERTEX_ID_PROPERTY_NAME.equals(fieldName) || Edge.IN_VERTEX_ID_PROPERTY_NAME.equals(fieldName) || ExtendedDataRow.TABLE_NAME.equals(fieldName) || ExtendedDataRow.ROW_ID.equals(fieldName) || ExtendedDataRow.ELEMENT_ID.equals(fieldName) || ExtendedDataRow.ELEMENT_TYPE.equals(fieldName)) { Map<String, Object> metadata = new HashMap<>(); metadata.put(AGGREGATION_METADATA_FIELD_NAME_KEY, fieldName); if (Element.ID_PROPERTY_NAME.equals(fieldName) || ExtendedDataRow.ELEMENT_ID.equals(fieldName)) { fieldName = ELEMENT_ID_FIELD_NAME; } else if (ExtendedDataRow.ELEMENT_TYPE.equals(fieldName)) { fieldName = ELEMENT_TYPE_FIELD_NAME; } String aggregationName = createAggregationName(agg.getAggregationName(), "0"); CardinalityAggregationBuilder cardinalityAgg = AggregationBuilders.cardinality(aggregationName); cardinalityAgg.setMetaData(metadata); cardinalityAgg.field(fieldName); cardinalityAggs.add(cardinalityAgg); } else { throw new VertexiumException("Cannot use cardinality aggregation on properties with visibility: " + fieldName); } return cardinalityAggs; }
protected List<AggregationBuilder> getElasticsearchCardinalityAggregations(CardinalityAggregation agg) { List<AggregationBuilder> cardinalityAggs = new ArrayList<>(); String fieldName = agg.getPropertyName(); if (Element.ID_PROPERTY_NAME.equals(fieldName) || Edge.LABEL_PROPERTY_NAME.equals(fieldName) || Edge.OUT_VERTEX_ID_PROPERTY_NAME.equals(fieldName) || Edge.IN_VERTEX_ID_PROPERTY_NAME.equals(fieldName) || ExtendedDataRow.TABLE_NAME.equals(fieldName) || ExtendedDataRow.ROW_ID.equals(fieldName) || ExtendedDataRow.ELEMENT_ID.equals(fieldName) || ExtendedDataRow.ELEMENT_TYPE.equals(fieldName)) { Map<String, Object> metadata = new HashMap<>(); metadata.put(AGGREGATION_METADATA_FIELD_NAME_KEY, fieldName); if (Element.ID_PROPERTY_NAME.equals(fieldName) || ExtendedDataRow.ELEMENT_ID.equals(fieldName)) { fieldName = ELEMENT_ID_FIELD_NAME; } else if (ExtendedDataRow.ELEMENT_TYPE.equals(fieldName)) { fieldName = ELEMENT_TYPE_FIELD_NAME; } String aggregationName = createAggregationName(agg.getAggregationName(), "0"); CardinalityAggregationBuilder cardinalityAgg = AggregationBuilders.cardinality(aggregationName); cardinalityAgg.setMetaData(metadata); cardinalityAgg.field(fieldName); cardinalityAggs.add(cardinalityAgg); } else { throw new VertexiumException("Cannot use cardinality aggregation on properties with visibility: " + fieldName); } return cardinalityAggs; }
/** * Create count aggregation. * * @param field The count function * @return AggregationBuilder use to count result */ private ValuesSourceAggregationBuilder makeCountAgg(MethodField field) { // Cardinality is approximate DISTINCT. if ("DISTINCT".equals(field.getOption())) { if (field.getParams().size() == 1) { return AggregationBuilders.cardinality(field.getAlias()).field(field.getParams().get(0).value.toString()); } else { Integer precision_threshold = (Integer) (field.getParams().get(1).value); return AggregationBuilders.cardinality(field.getAlias()).precisionThreshold(precision_threshold).field(field.getParams().get(0).value.toString()); } } String fieldName = field.getParams().get(0).value.toString(); // In case of count(*) we use '_index' as field parameter to count all documents if ("*".equals(fieldName)) { KVValue kvValue = new KVValue(null, "_index"); field.getParams().set(0, kvValue); return AggregationBuilders.count(field.getAlias()).field(kvValue.toString()); } else { return AggregationBuilders.count(field.getAlias()).field(fieldName); } }
private static void setCardinalityAggregation(SearchSourceBuilder searchSource, CardinalityAggregation aggregation, Boolean value) { if (!value) { return; } CardinalityAggregationBuilder cardinalityAggregation = AggregationBuilders.cardinality(aggregation.name()).field(aggregation.field()); long precisionThreshold = aggregation.precisionThreshold(); if (precisionThreshold > 0) { cardinalityAggregation.precisionThreshold(precisionThreshold); } searchSource.aggregation(cardinalityAggregation); }
public FilterAggregationBuilder addCountDistinctAggregation(QueryState state){ FilterAggregationBuilder result = AggregationBuilders.filter("cardinality_aggs", QueryBuilders.matchAllQuery()); for(Column col : state.getHeading().columns()){ result.subAggregation( AggregationBuilders.cardinality(col.getLabel()).field(col.getColumn()).precisionThreshold(state.getIntProp(Utils.PROP_PRECISION_THRESHOLD, 3000)) ); } return result; }
protected SearchRequest getCardinalityRequest(FilterJoinNode node, ActionRequest parentRequest) { String[] lookupIndices = node.getLookupIndices(); String[] lookupTypes = node.getLookupTypes(); String lookupPath = node.getLookupPath(); // Build the search source with the aggregate definition SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); sourceBuilder.size(0).aggregation(AggregationBuilders.cardinality(lookupPath).field(lookupPath)); // Build search request with reference to the parent request SearchRequest searchRequest = new SearchRequest(parentRequest); searchRequest.indices(lookupIndices).types(lookupTypes).source(sourceBuilder); return searchRequest; }
.setSize(0) .addAggregation(termsAgg) .addAggregation(AggregationBuilders.cardinality(COUNT_AGGREGATION).field(GROUP_FIELD)); LOGGER.debug("ES Query: {}", srb.toString());
searchSourceBuilder.aggregation(AggregationBuilders.cardinality(AGG_CARDINALITY).field(field));
/** * Creates an AggregationBuilder for the supplied type */ public AggregationBuilder toBuilder(String type) { switch (type) { case AGG_CARDINALITY: return AggregationBuilders.cardinality(getId()).field(getField()); case AGG_COUNT: return AggregationBuilders.count(getId()).field(getField()); case AGG_SUM: return AggregationBuilders.sum(getId()).field(getField()); case AGG_AVG: return AggregationBuilders.avg(getId()).field(getField()); case AGG_MAX: return AggregationBuilders.max(getId()).field(getField()); case AGG_MIN: return AggregationBuilders.min(getId()).field(getField()); default: throw new IllegalArgumentException("Unknown aggregate type: " + type); } }
private AggregationBuilder buildObjectsAggregation(FactSearchCriteria criteria) { // 1. Reduce to Facts matching the search criteria. return filter(FILTER_FACTS_AGGREGATION_NAME, buildFactsQuery(criteria)) // 2. Map to nested Object documents. .subAggregation(nested(NESTED_OBJECTS_AGGREGATION_NAME, "objects") // 3. Reduce to Objects matching the search criteria. .subAggregation(filter(FILTER_OBJECTS_AGGREGATION_NAME, buildObjectsQuery(criteria)) // 4. Calculate the number of unique Objects by id. This will give the 'count' value. // If 'count' is smaller than MAX_RESULT_WINDOW a correct value is expected, thus, // the precision threshold is set to MAX_RESULT_WINDOW. .subAggregation(cardinality(OBJECTS_COUNT_AGGREGATION_NAME) .field("objects.id") .precisionThreshold(MAX_RESULT_WINDOW) ) // 5. Reduce to buckets of unique Objects by id, restricted to the search criteria's limit. // This will give the actual search results. .subAggregation(terms(UNIQUE_OBJECTS_AGGREGATION_NAME) .field("objects.id") .size(calculateMaximumSize(criteria)) // 6. Map to the unique Object's source. Set size to 1, because all Objects in one // bucket are the same (ignoring 'direction' which isn't relevant for Object search). .subAggregation(topHits(UNIQUE_OBJECTS_SOURCE_AGGREGATION_NAME) .size(1) ) ) ) ); }
dateHistogramBuilder.subAggregation(AggregationBuilders.cardinality(AGG_CARDINALITY).field(field));