org.elasticsearch.search.aggregations.support.ValueType Java Examples

The following examples show how to use org.elasticsearch.search.aggregations.support.ValueType. 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: GeoCentroidParser.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
public AggregatorFactory parse(String aggregationName, XContentParser parser, SearchContext context) throws IOException {
    ValuesSourceParser<ValuesSource.GeoPoint> vsParser = ValuesSourceParser.geoPoint(aggregationName, InternalGeoCentroid.TYPE, context)
            .targetValueType(ValueType.GEOPOINT)
            .formattable(true)
            .build();
    XContentParser.Token token;
    String currentFieldName = null;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            currentFieldName = parser.currentName();
        } else if (vsParser.token(currentFieldName, token, parser)) {
            continue;
        } else {
            throw new SearchParseException(context, "Unknown key for a " + token + " in aggregation [" + aggregationName + "]: ["
                    + currentFieldName + "].", parser.getTokenLocation());
        }
    }
    return new GeoCentroidAggregator.Factory(aggregationName, vsParser.config());
}
 
Example #2
Source File: GeoBoundsParser.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public AggregatorFactory parse(String aggregationName, XContentParser parser, SearchContext context) throws IOException {
    ValuesSourceParser<GeoPoint> vsParser = ValuesSourceParser.geoPoint(aggregationName, InternalGeoBounds.TYPE, context)
            .targetValueType(ValueType.GEOPOINT)
            .formattable(true)
            .build();
    boolean wrapLongitude = true;
    XContentParser.Token token;
    String currentFieldName = null;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            currentFieldName = parser.currentName();
        } else if (vsParser.token(currentFieldName, token, parser)) {
            continue;
            
        } else if (token == XContentParser.Token.VALUE_BOOLEAN) {
            if ("wrap_longitude".equals(currentFieldName) || "wrapLongitude".equals(currentFieldName)) {
                wrapLongitude = parser.booleanValue();
            } else {
                throw new SearchParseException(context, "Unknown key for a " + token + " in aggregation [" + aggregationName + "]: ["
                        + currentFieldName + "].", parser.getTokenLocation());
            }
        } else {
            throw new SearchParseException(context, "Unknown key for a " + token + " in aggregation [" + aggregationName + "]: ["
                    + currentFieldName + "].", parser.getTokenLocation());
        }
    }
    return new GeoBoundsAggregator.Factory(aggregationName, vsParser.config(), wrapLongitude);
}
 
Example #3
Source File: BaseParser.java    From elasticsearch-linear-regression with Apache License 2.0 5 votes vote down vote up
@Override
protected B createFactory(final String aggregationName,
    final ValuesSourceType valuesSourceType,
    final ValueType targetValueType, final Map<ParseField, Object> otherOptions) {
  final B builder = createInnerFactory(aggregationName, otherOptions);
  final String mode = (String) otherOptions.get(MULTIVALUE_MODE_FIELD);
  if (mode != null) {
    builder.multiValueMode(MultiValueMode.fromString(mode));
  }
  return builder;
}
 
Example #4
Source File: GeoPointClusteringAggregationBuilder.java    From elasticsearch-aggregation-geoclustering with Apache License 2.0 5 votes vote down vote up
/**
 * Read from a stream.
 */
public GeoPointClusteringAggregationBuilder(StreamInput in) throws IOException {
    super(in, CoreValuesSourceType.GEOPOINT, ValueType.GEOPOINT);
    zoom = in.readInt();
    radius = in.readInt();
    extent = in.readInt();
    ratio = in.readDouble();
    requiredSize = in.readVInt();
    shardSize = in.readVInt();
}
 
Example #5
Source File: ElasticsearchCatalogService.java    From staccato with Apache License 2.0 4 votes vote down vote up
/**
 * Produces a list of all unique values for a given field in a collection.
 *
 * @param collection The collection that will be queried.
 * @param path The request path containing property fieldsExtension and values for which to find unique values
 * @return A list of unique values
 */
@Override
public List<String> getValuesForField(CollectionMetadata collection, List<String> path) {
    String fieldName = path.get(path.size() - 1);

    List<String> values = new LinkedList<>();
    fieldName = "properties." + fieldName;

    // build the term aggregation from the last subcatalog property in the url path
    TermsAggregationBuilder aggregationBuilder = new TermsAggregationBuilder(fieldName + "_Agg", ValueType.STRING).size(10000);
    aggregationBuilder.field(fieldName);

    // the query based on all of the unique subcatalog value previously selected
    QueryBuilder query = QueryBuilders.boolQuery();
    for (int i = 2; i * 2 <= path.size(); i = i + 2) {
        String property = "properties." + path.get(i);
        String value = path.get(i + 1);
        QueryBuilder pathQuery = QueryBuilders.termQuery(property, value);

        ((BoolQueryBuilder) query).must(pathQuery);
    }

    SearchRequest request = new SearchRequest().indices(indexAliasLookup.getReadAlias(collection.getId()))
            .searchType(SearchType.DFS_QUERY_THEN_FETCH)
            .source(new SearchSourceBuilder().query(query).aggregation(aggregationBuilder).size(0));

    SearchResponse response;
    try {
        response = client.search(request, RequestOptions.DEFAULT);
    } catch (Exception ex) {
        log.error("Error getting aggregations.", ex);
        throw new RuntimeException("Error getting aggregations.", ex);
    }

    if (response == null) return Collections.EMPTY_LIST;

    ParsedTerms terms = response.getAggregations().get(fieldName + "_Agg");
    if (terms != null) {
        List<ParsedTerms.ParsedBucket> buckets = (List<ParsedTerms.ParsedBucket>) terms.getBuckets();
        if (buckets != null && buckets.size() > 0) {
            for (ParsedTerms.ParsedBucket bucket : buckets) {
                values.add(bucket.getKeyAsString());
            }
        }
        return values;
    }
    return Collections.EMPTY_LIST;
}
 
Example #6
Source File: IpRangeParser.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public AggregatorFactory parse(String aggregationName, XContentParser parser, SearchContext context) throws IOException {

    ValuesSourceParser<ValuesSource.Numeric> vsParser = ValuesSourceParser.numeric(aggregationName, InternalIPv4Range.TYPE, context)
            .targetValueType(ValueType.IP)
            .formattable(false)
            .build();

    List<RangeAggregator.Range> ranges = null;
    boolean keyed = false;

    XContentParser.Token token;
    String currentFieldName = null;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            currentFieldName = parser.currentName();
        } else if (vsParser.token(currentFieldName, token, parser)) {
            continue;
        } else if (token == XContentParser.Token.START_ARRAY) {
            if ("ranges".equals(currentFieldName)) {
                ranges = new ArrayList<>();
                while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
                    double from = Double.NEGATIVE_INFINITY;
                    String fromAsStr = null;
                    double to = Double.POSITIVE_INFINITY;
                    String toAsStr = null;
                    String key = null;
                    String mask = null;
                    String toOrFromOrMaskOrKey = null;
                    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
                        if (token == XContentParser.Token.FIELD_NAME) {
                            toOrFromOrMaskOrKey = parser.currentName();
                        } else if (token == XContentParser.Token.VALUE_NUMBER) {
                            if ("from".equals(toOrFromOrMaskOrKey)) {
                                from = parser.doubleValue();
                            } else if ("to".equals(toOrFromOrMaskOrKey)) {
                                to = parser.doubleValue();
                            }
                        } else if (token == XContentParser.Token.VALUE_STRING) {
                            if ("from".equals(toOrFromOrMaskOrKey)) {
                                fromAsStr = parser.text();
                            } else if ("to".equals(toOrFromOrMaskOrKey)) {
                                toAsStr = parser.text();
                            } else if ("key".equals(toOrFromOrMaskOrKey)) {
                                key = parser.text();
                            } else if ("mask".equals(toOrFromOrMaskOrKey)) {
                                mask = parser.text();
                            }
                        }
                    }
                    RangeAggregator.Range range = new RangeAggregator.Range(key, from, fromAsStr, to, toAsStr);
                    if (mask != null) {
                        parseMaskRange(mask, range, aggregationName, context);
                    }
                    ranges.add(range);
                }
            } else {
                throw new SearchParseException(context, "Unknown key for a " + token + " in [" + aggregationName + "]: ["
                        + currentFieldName + "].", parser.getTokenLocation());
            }
        } else if (token == XContentParser.Token.VALUE_BOOLEAN) {
            if ("keyed".equals(currentFieldName)) {
                keyed = parser.booleanValue();
            } else {
                throw new SearchParseException(context, "Unknown key for a " + token + " in [" + aggregationName + "]: ["
                        + currentFieldName + "].", parser.getTokenLocation());
            }
        } else {
            throw new SearchParseException(context, "Unexpected token " + token + " in [" + aggregationName + "].",
                    parser.getTokenLocation());
        }
    }

    if (ranges == null) {
        throw new SearchParseException(context, "Missing [ranges] in ranges aggregator [" + aggregationName + "]",
                parser.getTokenLocation());
    }

    return new RangeAggregator.Factory(aggregationName, vsParser.config(), InternalIPv4Range.FACTORY, ranges, keyed);
}
 
Example #7
Source File: DateRangeParser.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public AggregatorFactory parse(String aggregationName, XContentParser parser, SearchContext context) throws IOException {

    ValuesSourceParser<ValuesSource.Numeric> vsParser = ValuesSourceParser.numeric(aggregationName, InternalDateRange.TYPE, context)
            .targetValueType(ValueType.DATE)
            .formattable(true)
            .build();

    List<RangeAggregator.Range> ranges = null;
    boolean keyed = false;

    XContentParser.Token token;
    String currentFieldName = null;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            currentFieldName = parser.currentName();
        } else if (vsParser.token(currentFieldName, token, parser)) {
            continue;
        } else if (token == XContentParser.Token.START_ARRAY) {
            if ("ranges".equals(currentFieldName)) {
                ranges = new ArrayList<>();
                while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
                    double from = Double.NEGATIVE_INFINITY;
                    String fromAsStr = null;
                    double to = Double.POSITIVE_INFINITY;
                    String toAsStr = null;
                    String key = null;
                    String toOrFromOrKey = null;
                    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
                        if (token == XContentParser.Token.FIELD_NAME) {
                            toOrFromOrKey = parser.currentName();
                        } else if (token == XContentParser.Token.VALUE_NUMBER) {
                            if ("from".equals(toOrFromOrKey)) {
                                from = parser.doubleValue();
                            } else if ("to".equals(toOrFromOrKey)) {
                                to = parser.doubleValue();
                            } else {
                                throw new SearchParseException(context, "Unknown key for a " + token + " in [" + aggregationName
                                        + "]: [" + currentFieldName + "].", parser.getTokenLocation());
                            }
                        } else if (token == XContentParser.Token.VALUE_STRING) {
                            if ("from".equals(toOrFromOrKey)) {
                                fromAsStr = parser.text();
                            } else if ("to".equals(toOrFromOrKey)) {
                                toAsStr = parser.text();
                            } else if ("key".equals(toOrFromOrKey)) {
                                key = parser.text();
                            } else {
                                throw new SearchParseException(context, "Unknown key for a " + token + " in [" + aggregationName + "]: [" + currentFieldName + "].", parser.getTokenLocation());
                            }
                        }
                    }
                    ranges.add(new RangeAggregator.Range(key, from, fromAsStr, to, toAsStr));
                }
            }
        } else if (token == XContentParser.Token.VALUE_BOOLEAN) {
            if ("keyed".equals(currentFieldName)) {
                keyed = parser.booleanValue();
            } else {
                throw new SearchParseException(context, "Unknown key for a " + token + " in [" + aggregationName + "]: ["
                        + currentFieldName + "].", parser.getTokenLocation());
            }
        } else {
            throw new SearchParseException(context, "Unexpected token " + token + " in [" + aggregationName + "].",
                    parser.getTokenLocation());
        }
    }

    if (ranges == null) {
        throw new SearchParseException(context, "Missing [ranges] in ranges aggregator [" + aggregationName + "]",
                parser.getTokenLocation());
    }

    return new RangeAggregator.Factory(aggregationName, vsParser.config(), InternalDateRange.FACTORY, ranges, keyed);
}
 
Example #8
Source File: HistogramParser.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public AggregatorFactory parse(String aggregationName, XContentParser parser, SearchContext context) throws IOException {

    ValuesSourceParser vsParser = ValuesSourceParser.numeric(aggregationName, InternalHistogram.TYPE, context)
            .targetValueType(ValueType.NUMERIC)
            .formattable(true)
            .build();

    boolean keyed = false;
    long minDocCount = 0;
    InternalOrder order = (InternalOrder) InternalOrder.KEY_ASC;
    long interval = -1;
    ExtendedBounds extendedBounds = null;
    long offset = 0;

    XContentParser.Token token;
    String currentFieldName = null;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            currentFieldName = parser.currentName();
        } else if (vsParser.token(currentFieldName, token, parser)) {
            continue;
        } else if (token.isValue()) {
            if ("interval".equals(currentFieldName)) {
                interval = parser.longValue();
            } else if ("min_doc_count".equals(currentFieldName) || "minDocCount".equals(currentFieldName)) {
                minDocCount = parser.longValue();
            } else if ("keyed".equals(currentFieldName)) {
                keyed = parser.booleanValue();
            } else if ("offset".equals(currentFieldName)) {
                offset = parser.longValue();
            } else {
                throw new SearchParseException(context, "Unknown key for a " + token + " in aggregation [" + aggregationName + "]: ["
                        + currentFieldName + "].", parser.getTokenLocation());
            }
        } else if (token == XContentParser.Token.START_OBJECT) {
            if ("order".equals(currentFieldName)) {
                while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
                    if (token == XContentParser.Token.FIELD_NAME) {
                        currentFieldName = parser.currentName();
                    } else if (token == XContentParser.Token.VALUE_STRING) {
                        String dir = parser.text();
                        boolean asc = "asc".equals(dir);
                        if (!asc && !"desc".equals(dir)) {
                            throw new SearchParseException(context, "Unknown order direction [" + dir + "] in aggregation ["
                                    + aggregationName + "]. Should be either [asc] or [desc]", parser.getTokenLocation());
                        }
                        order = resolveOrder(currentFieldName, asc);
                    }
                }
            } else if (context.parseFieldMatcher().match(currentFieldName, EXTENDED_BOUNDS)) {
                extendedBounds = new ExtendedBounds();
                while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
                    if (token == XContentParser.Token.FIELD_NAME) {
                        currentFieldName = parser.currentName();
                    } else if (token.isValue()) {
                        if ("min".equals(currentFieldName)) {
                            extendedBounds.min = parser.longValue(true);
                        } else if ("max".equals(currentFieldName)) {
                            extendedBounds.max = parser.longValue(true);
                        } else {
                            throw new SearchParseException(context, "Unknown extended_bounds key for a " + token + " in aggregation ["
                                    + aggregationName + "]: [" + currentFieldName + "].", parser.getTokenLocation());
                        }
                    }
                }

            } else {
                throw new SearchParseException(context, "Unknown key for a " + token + " in aggregation [" + aggregationName + "]: ["
                        + currentFieldName + "].", parser.getTokenLocation());
            }
        } else {
            throw new SearchParseException(context, "Unexpected token " + token + " in aggregation [" + aggregationName + "].",
                    parser.getTokenLocation());
        }
    }

    if (interval < 1) {
        throw new SearchParseException(context,
                "Missing required field [interval] for histogram aggregation [" + aggregationName + "]", parser.getTokenLocation());
    }

    Rounding rounding = new Rounding.Interval(interval);
    if (offset != 0) {
        rounding = new Rounding.OffsetRounding((Rounding.Interval) rounding, offset);
    }

    if (extendedBounds != null) {
        // with numeric histogram, we can process here and fail fast if the bounds are invalid
        extendedBounds.processAndValidate(aggregationName, context, ValueParser.RAW);
    }

    return new HistogramAggregator.Factory(aggregationName, vsParser.config(), rounding, order, keyed, minDocCount, extendedBounds,
            new InternalHistogram.Factory());

}
 
Example #9
Source File: BaseAggregationBuilder.java    From elasticsearch-linear-regression with Apache License 2.0 4 votes vote down vote up
public BaseAggregationBuilder(final String name) {
  super(name, ValuesSourceType.NUMERIC, ValueType.NUMERIC);
}
 
Example #10
Source File: BaseAggregationBuilder.java    From elasticsearch-linear-regression with Apache License 2.0 4 votes vote down vote up
/**
 * Read from a stream.
 */
public BaseAggregationBuilder(final StreamInput in) throws IOException {
  super(in, ValuesSourceType.NUMERIC, ValueType.NUMERIC);
}
 
Example #11
Source File: PathHierarchyAggregationBuilder.java    From elasticsearch-aggregation-pathhierarchy with MIT License 4 votes vote down vote up
private PathHierarchyAggregationBuilder(String name, ValueType valueType) {
    super(name, CoreValuesSourceType.ANY, valueType);
}
 
Example #12
Source File: DateHierarchyAggregationBuilder.java    From elasticsearch-aggregation-pathhierarchy with MIT License 4 votes vote down vote up
private DateHierarchyAggregationBuilder(String name, ValueType valueType) {
    super(name, CoreValuesSourceType.ANY, valueType);
}
 
Example #13
Source File: GeoShapeBuilder.java    From elasticsearch-plugin-geoshape with MIT License 4 votes vote down vote up
private GeoShapeBuilder(String name, ValueType valueType) {
    super(name, CoreValuesSourceType.ANY, valueType);
}
 
Example #14
Source File: GeoPointClusteringAggregationBuilder.java    From elasticsearch-aggregation-geoclustering with Apache License 2.0 4 votes vote down vote up
public GeoPointClusteringAggregationBuilder(String name) {
    super(name, CoreValuesSourceType.GEOPOINT, ValueType.GEOPOINT);
}