Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GeoCircleFilterConverter for performing point and radius searches #14

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ protected void registerDefaultFilterConverters() {
register("geo_bounding_box", new GeoBoundingBoxFilterConverter());
register("geo_distance", new GeoDistanceFilterConverter());
register("geo_shape", new GeoShapeFilterConverter());
register("geo_shape_circle", new GeoCircleFilterConverter());
register("geo_polygon", new GeoPolygonFilterConverter());
register("query", new QueryFilterConverter());
register("type", new TypeFilterConverter());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.scaleset.search.es.filter;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.scaleset.geo.geojson.GeoJsonModule;
import com.scaleset.search.Filter;
import com.scaleset.utils.Coerce;
import com.spatial4j.core.context.jts.JtsSpatialContext;
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.GeometryFactory;
import org.elasticsearch.common.geo.ShapeRelation;
import org.elasticsearch.common.geo.builders.ShapeBuilder;
import org.elasticsearch.index.query.FilterBuilder;
import org.elasticsearch.index.query.FilterBuilders;

public class GeoCircleFilterConverter implements FilterConverter {

private Coerce coerce = new Coerce(new ObjectMapper().registerModule(new GeoJsonModule()));

public static final JtsSpatialContext SPATIAL_CONTEXT = JtsSpatialContext.GEO;
public static final GeometryFactory FACTORY = SPATIAL_CONTEXT.getGeometryFactory();
protected final boolean multiPolygonMayOverlap = false;

@Override
public FilterBuilder convert(Filter filter) {
String field = filter.getString("field");
Coordinate geometry = filter.get(Coordinate.class, "geometry");
String radius = filter.getString("radius");
ShapeBuilder shapeBuilder = ShapeBuilder.newCircleBuilder().center(geometry).radius(radius);
FilterBuilder result = FilterBuilders.geoWithinFilter(field, shapeBuilder);
return result;
}

}