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;
}
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);
}
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);
}
Aggregations