Search in sources :

Example 1 with DistanceRange

use of com.enonic.xp.query.aggregation.DistanceRange in project xp by enonic.

the class GeoDistanceAggregationQueryBuilderFactory method create.

AggregationBuilder create(final GeoDistanceAggregationQuery query) {
    final String fieldName = fieldNameResolver.resolve(query.getFieldName(), IndexValueType.GEO_POINT);
    final GeoDistanceBuilder geoDistanceBuilder = new GeoDistanceBuilder(query.getName()).field(fieldName).lat(query.getOrigin().getLatitude()).lon(query.getOrigin().getLongitude());
    if (!isNullOrEmpty(query.getUnit())) {
        geoDistanceBuilder.unit(DistanceUnit.fromString(query.getUnit()));
    }
    for (final DistanceRange range : query.getRanges()) {
        final String key = range.getKey();
        if (range.getFrom() == null) {
            addUnboundedTo(geoDistanceBuilder, range, key);
        } else if (range.getTo() == null) {
            addUnboundedFrom(geoDistanceBuilder, range, key);
        } else {
            addBoundedToAndFrom(geoDistanceBuilder, range, key);
        }
    }
    return geoDistanceBuilder;
}
Also used : GeoDistanceBuilder(org.elasticsearch.search.aggregations.bucket.range.geodistance.GeoDistanceBuilder) DistanceRange(com.enonic.xp.query.aggregation.DistanceRange)

Example 2 with DistanceRange

use of com.enonic.xp.query.aggregation.DistanceRange in project xp by enonic.

the class QueryAggregationParams method geoDistanceAggregationFromParams.

private GeoDistanceAggregationQuery.Builder geoDistanceAggregationFromParams(final String name, final Map<String, Object> params) {
    final String fieldName = (String) params.get("field");
    final String unit = (String) params.get("unit");
    final Map<String, Object> originCoordinates = (Map<String, Object>) params.getOrDefault("origin", emptyMap());
    final double lat = Double.parseDouble((String) originCoordinates.get("lat"));
    final double lon = Double.parseDouble((String) originCoordinates.get("lon"));
    final GeoPoint origin = new GeoPoint(lat, lon);
    final List<Map<String, Object>> rangeListParams = (List<Map<String, Object>>) params.getOrDefault("ranges", emptyList());
    final List<DistanceRange> ranges = new ArrayList<>();
    for (Map<String, Object> rangeParams : rangeListParams) {
        final Double from = getDouble(rangeParams, "from");
        final Double to = getDouble(rangeParams, "to");
        final String key = (String) rangeParams.getOrDefault("key", null);
        final DistanceRange range = DistanceRange.create().from(from).to(to).key(key).build();
        ranges.add(range);
    }
    return GeoDistanceAggregationQuery.create(name).fieldName(fieldName).origin(origin).unit(unit).setRanges(ranges);
}
Also used : GeoPoint(com.enonic.xp.util.GeoPoint) ArrayList(java.util.ArrayList) DistanceRange(com.enonic.xp.query.aggregation.DistanceRange) ArrayList(java.util.ArrayList) Collections.emptyList(java.util.Collections.emptyList) List(java.util.List) Map(java.util.Map) Collections.emptyMap(java.util.Collections.emptyMap)

Example 3 with DistanceRange

use of com.enonic.xp.query.aggregation.DistanceRange in project xp by enonic.

the class QueryAggregationParams method geoDistanceAggregationFromParams.

private GeoDistanceAggregationQuery.Builder geoDistanceAggregationFromParams(final String name, final Map<String, Object> params) {
    final String fieldName = (String) params.get("field");
    final String unit = (String) params.get("unit");
    final Map<String, Object> originCoordinates = (Map<String, Object>) params.getOrDefault("origin", emptyMap());
    final double lat = Double.parseDouble((String) originCoordinates.get("lat"));
    final double lon = Double.parseDouble((String) originCoordinates.get("lon"));
    final GeoPoint origin = new GeoPoint(lat, lon);
    final List<Map<String, Object>> rangeListParams = (List<Map<String, Object>>) params.getOrDefault("ranges", emptyList());
    final List<DistanceRange> ranges = new ArrayList<>();
    for (Map<String, Object> rangeParams : rangeListParams) {
        final Double from = getDouble(rangeParams, "from");
        final Double to = getDouble(rangeParams, "to");
        final DistanceRange range = DistanceRange.create().from(from).to(to).build();
        ranges.add(range);
    }
    return GeoDistanceAggregationQuery.create(name).fieldName(fieldName).origin(origin).unit(unit).setRanges(ranges);
}
Also used : GeoPoint(com.enonic.xp.util.GeoPoint) ArrayList(java.util.ArrayList) DistanceRange(com.enonic.xp.query.aggregation.DistanceRange) ArrayList(java.util.ArrayList) Collections.emptyList(java.util.Collections.emptyList) List(java.util.List) Map(java.util.Map) Collections.emptyMap(java.util.Collections.emptyMap)

Aggregations

DistanceRange (com.enonic.xp.query.aggregation.DistanceRange)3 GeoPoint (com.enonic.xp.util.GeoPoint)2 ArrayList (java.util.ArrayList)2 Collections.emptyList (java.util.Collections.emptyList)2 Collections.emptyMap (java.util.Collections.emptyMap)2 List (java.util.List)2 Map (java.util.Map)2 GeoDistanceBuilder (org.elasticsearch.search.aggregations.bucket.range.geodistance.GeoDistanceBuilder)1