|
@@ -20,6 +20,9 @@
|
|
|
package org.elasticsearch.search.geo;
|
|
|
|
|
|
import org.elasticsearch.action.get.GetResponse;
|
|
|
+import org.elasticsearch.action.search.SearchAction;
|
|
|
+import org.elasticsearch.action.search.SearchPhaseExecutionException;
|
|
|
+import org.elasticsearch.action.search.SearchRequestBuilder;
|
|
|
import org.elasticsearch.action.search.SearchResponse;
|
|
|
import org.elasticsearch.common.Strings;
|
|
|
import org.elasticsearch.common.geo.GeoShapeType;
|
|
@@ -28,12 +31,21 @@ import org.elasticsearch.common.geo.builders.CircleBuilder;
|
|
|
import org.elasticsearch.common.geo.builders.CoordinatesBuilder;
|
|
|
import org.elasticsearch.common.geo.builders.EnvelopeBuilder;
|
|
|
import org.elasticsearch.common.geo.builders.GeometryCollectionBuilder;
|
|
|
+import org.elasticsearch.common.geo.builders.LineStringBuilder;
|
|
|
+import org.elasticsearch.common.geo.builders.MultiLineStringBuilder;
|
|
|
+import org.elasticsearch.common.geo.builders.MultiPointBuilder;
|
|
|
import org.elasticsearch.common.geo.builders.MultiPolygonBuilder;
|
|
|
+import org.elasticsearch.common.geo.builders.PointBuilder;
|
|
|
import org.elasticsearch.common.geo.builders.PolygonBuilder;
|
|
|
import org.elasticsearch.common.xcontent.XContentBuilder;
|
|
|
import org.elasticsearch.common.xcontent.XContentFactory;
|
|
|
import org.elasticsearch.common.xcontent.XContentType;
|
|
|
import org.elasticsearch.geometry.Geometry;
|
|
|
+import org.elasticsearch.geometry.Line;
|
|
|
+import org.elasticsearch.geometry.LinearRing;
|
|
|
+import org.elasticsearch.geometry.MultiLine;
|
|
|
+import org.elasticsearch.geometry.MultiPoint;
|
|
|
+import org.elasticsearch.geometry.Point;
|
|
|
import org.elasticsearch.geometry.Rectangle;
|
|
|
import org.elasticsearch.index.query.GeoShapeQueryBuilder;
|
|
|
import org.elasticsearch.index.query.QueryBuilders;
|
|
@@ -232,16 +244,51 @@ public abstract class GeoQueryTests extends ESSingleNodeTestCase {
|
|
|
|
|
|
GeometryCollectionBuilder builder = new GeometryCollectionBuilder().shape(mp);
|
|
|
Geometry geometry = builder.buildGeometry();
|
|
|
- SearchResponse searchResponse = client().prepareSearch(defaultIndexName)
|
|
|
- .setQuery(QueryBuilders.geoShapeQuery(defaultGeoFieldName, geometry)
|
|
|
- .relation(ShapeRelation.INTERSECTS))
|
|
|
- .get();
|
|
|
+ {
|
|
|
+ SearchResponse searchResponse = client().prepareSearch(defaultIndexName)
|
|
|
+ .setQuery(QueryBuilders.geoShapeQuery(defaultGeoFieldName, geometry)
|
|
|
+ .relation(ShapeRelation.INTERSECTS))
|
|
|
+ .get();
|
|
|
|
|
|
- assertSearchResponse(searchResponse);
|
|
|
- assertThat(searchResponse.getHits().getTotalHits().value, equalTo(2L));
|
|
|
- assertThat(searchResponse.getHits().getHits().length, equalTo(2));
|
|
|
- assertThat(searchResponse.getHits().getAt(0).getId(), not(equalTo("2")));
|
|
|
- assertThat(searchResponse.getHits().getAt(1).getId(), not(equalTo("2")));
|
|
|
+ assertSearchResponse(searchResponse);
|
|
|
+ assertThat(searchResponse.getHits().getTotalHits().value, equalTo(2L));
|
|
|
+ assertThat(searchResponse.getHits().getHits().length, equalTo(2));
|
|
|
+ assertThat(searchResponse.getHits().getAt(0).getId(), not(equalTo("2")));
|
|
|
+ assertThat(searchResponse.getHits().getAt(1).getId(), not(equalTo("2")));
|
|
|
+ }
|
|
|
+ {
|
|
|
+ SearchResponse searchResponse = client().prepareSearch(defaultIndexName)
|
|
|
+ .setQuery(QueryBuilders.geoShapeQuery(defaultGeoFieldName, geometry)
|
|
|
+ .relation(ShapeRelation.WITHIN))
|
|
|
+ .get();
|
|
|
+
|
|
|
+ assertSearchResponse(searchResponse);
|
|
|
+ assertThat(searchResponse.getHits().getTotalHits().value, equalTo(2L));
|
|
|
+ assertThat(searchResponse.getHits().getHits().length, equalTo(2));
|
|
|
+ assertThat(searchResponse.getHits().getAt(0).getId(), not(equalTo("2")));
|
|
|
+ assertThat(searchResponse.getHits().getAt(1).getId(), not(equalTo("2")));
|
|
|
+ }
|
|
|
+ {
|
|
|
+ SearchResponse searchResponse = client().prepareSearch(defaultIndexName)
|
|
|
+ .setQuery(QueryBuilders.geoShapeQuery(defaultGeoFieldName, geometry)
|
|
|
+ .relation(ShapeRelation.DISJOINT))
|
|
|
+ .get();
|
|
|
+
|
|
|
+ assertSearchResponse(searchResponse);
|
|
|
+ assertThat(searchResponse.getHits().getTotalHits().value, equalTo(1L));
|
|
|
+ assertThat(searchResponse.getHits().getHits().length, equalTo(1));
|
|
|
+ assertThat(searchResponse.getHits().getAt(0).getId(), equalTo("2"));
|
|
|
+ }
|
|
|
+ {
|
|
|
+ SearchResponse searchResponse = client().prepareSearch(defaultIndexName)
|
|
|
+ .setQuery(QueryBuilders.geoShapeQuery(defaultGeoFieldName, geometry)
|
|
|
+ .relation(ShapeRelation.CONTAINS))
|
|
|
+ .get();
|
|
|
+
|
|
|
+ assertSearchResponse(searchResponse);
|
|
|
+ assertThat(searchResponse.getHits().getTotalHits().value, equalTo(0L));
|
|
|
+ assertThat(searchResponse.getHits().getHits().length, equalTo(0));
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
public void testIndexPointsRectangle() throws Exception {
|
|
@@ -450,4 +497,138 @@ public abstract class GeoQueryTests extends ESSingleNodeTestCase {
|
|
|
assertNotEquals("3", searchHits.getAt(0).getId());
|
|
|
assertNotEquals("3", searchHits.getAt(1).getId());
|
|
|
}
|
|
|
+
|
|
|
+ public void testWithInQueryLine() throws Exception {
|
|
|
+ String mapping = Strings.toString(createDefaultMapping());
|
|
|
+ client().admin().indices().prepareCreate("test").setMapping(mapping).get();
|
|
|
+ ensureGreen();
|
|
|
+
|
|
|
+ Line line = new Line(new double[]{-25, -25}, new double[]{-35, -35});
|
|
|
+
|
|
|
+ try {
|
|
|
+ client().prepareSearch("test")
|
|
|
+ .setQuery(QueryBuilders.geoShapeQuery(defaultGeoFieldName, line).relation(ShapeRelation.WITHIN)).get();
|
|
|
+ } catch (
|
|
|
+ SearchPhaseExecutionException e) {
|
|
|
+ assertThat(e.getCause().getMessage(),
|
|
|
+ containsString("Field [geo] found an unsupported shape Line"));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void testQueryWithinMultiLine() throws Exception {
|
|
|
+ String mapping = Strings.toString(createDefaultMapping());
|
|
|
+ client().admin().indices().prepareCreate("test").setMapping(mapping).get();
|
|
|
+ ensureGreen();
|
|
|
+
|
|
|
+ CoordinatesBuilder coords1 = new CoordinatesBuilder()
|
|
|
+ .coordinate(-35,-35)
|
|
|
+ .coordinate(-25,-25);
|
|
|
+ CoordinatesBuilder coords2 = new CoordinatesBuilder()
|
|
|
+ .coordinate(-15,-15)
|
|
|
+ .coordinate(-5,-5);
|
|
|
+ LineStringBuilder lsb1 = new LineStringBuilder(coords1);
|
|
|
+ LineStringBuilder lsb2 = new LineStringBuilder(coords2);
|
|
|
+ MultiLineStringBuilder mlb = new MultiLineStringBuilder().linestring(lsb1).linestring(lsb2);
|
|
|
+ MultiLine multiline = (MultiLine) mlb.buildGeometry();
|
|
|
+
|
|
|
+ GeoShapeQueryBuilder builder = QueryBuilders.geoShapeQuery(defaultGeoFieldName, multiline).relation(ShapeRelation.WITHIN);
|
|
|
+ SearchRequestBuilder searchRequestBuilder = client().prepareSearch("test").setQuery(builder);
|
|
|
+ SearchPhaseExecutionException e = expectThrows(SearchPhaseExecutionException.class, searchRequestBuilder::get);
|
|
|
+ assertThat(e.getCause().getMessage(),
|
|
|
+ containsString("Field [" + defaultGeoFieldName + "] found an unsupported shape Line"));
|
|
|
+ }
|
|
|
+
|
|
|
+ public void testQueryLinearRing() throws Exception {
|
|
|
+ String mapping = Strings.toString(createDefaultMapping());
|
|
|
+ client().admin().indices().prepareCreate("test").setMapping(mapping).get();
|
|
|
+ ensureGreen();
|
|
|
+
|
|
|
+ LinearRing linearRing = new LinearRing(new double[]{-25, -35, -25}, new double[]{-25, -35, -25});
|
|
|
+
|
|
|
+ // LinearRing extends Line implements Geometry: expose the build process
|
|
|
+ GeoShapeQueryBuilder queryBuilder = new GeoShapeQueryBuilder(defaultGeoFieldName, linearRing);
|
|
|
+ SearchRequestBuilder searchRequestBuilder = new SearchRequestBuilder(client(), SearchAction.INSTANCE);
|
|
|
+ searchRequestBuilder.setQuery(queryBuilder);
|
|
|
+ searchRequestBuilder.setIndices("test");
|
|
|
+ SearchPhaseExecutionException e = expectThrows(SearchPhaseExecutionException.class, searchRequestBuilder::get);
|
|
|
+ assertThat(e.getCause().getMessage(),
|
|
|
+ containsString("Field [" + defaultGeoFieldName + "] found an unsupported shape LinearRing"));
|
|
|
+ }
|
|
|
+
|
|
|
+ public void testQueryPoint() throws Exception {
|
|
|
+ String mapping = Strings.toString(createDefaultMapping());
|
|
|
+ client().admin().indices().prepareCreate("test").setMapping(mapping).get();
|
|
|
+ ensureGreen();
|
|
|
+
|
|
|
+ client().prepareIndex("test").setId("1").setSource(jsonBuilder()
|
|
|
+ .startObject()
|
|
|
+ .field(defaultGeoFieldName, "POINT(-35 -25)")
|
|
|
+ .endObject()).setRefreshPolicy(IMMEDIATE).get();
|
|
|
+
|
|
|
+ PointBuilder pb = new PointBuilder().coordinate(-35, -25);
|
|
|
+ Point point = pb.buildGeometry();
|
|
|
+ {
|
|
|
+ SearchResponse response = client().prepareSearch("test")
|
|
|
+ .setQuery(QueryBuilders.geoShapeQuery(defaultGeoFieldName, point)).get();
|
|
|
+ SearchHits searchHits = response.getHits();
|
|
|
+ assertEquals(1, searchHits.getTotalHits().value);
|
|
|
+ }
|
|
|
+ {
|
|
|
+ SearchResponse response = client().prepareSearch("test")
|
|
|
+ .setQuery(QueryBuilders.geoShapeQuery(defaultGeoFieldName, point).relation(ShapeRelation.WITHIN)).get();
|
|
|
+ SearchHits searchHits = response.getHits();
|
|
|
+ assertEquals(1, searchHits.getTotalHits().value);
|
|
|
+ }
|
|
|
+ {
|
|
|
+ SearchResponse response = client().prepareSearch("test")
|
|
|
+ .setQuery(QueryBuilders.geoShapeQuery(defaultGeoFieldName, point).relation(ShapeRelation.CONTAINS)).get();
|
|
|
+ SearchHits searchHits = response.getHits();
|
|
|
+ assertEquals(1, searchHits.getTotalHits().value);
|
|
|
+ }
|
|
|
+ {
|
|
|
+ SearchResponse response = client().prepareSearch("test")
|
|
|
+ .setQuery(QueryBuilders.geoShapeQuery(defaultGeoFieldName, point).relation(ShapeRelation.DISJOINT)).get();
|
|
|
+ SearchHits searchHits = response.getHits();
|
|
|
+ assertEquals(0, searchHits.getTotalHits().value);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void testQueryMultiPoint() throws Exception {
|
|
|
+ String mapping = Strings.toString(createDefaultMapping());
|
|
|
+ client().admin().indices().prepareCreate("test").setMapping(mapping).get();
|
|
|
+ ensureGreen();
|
|
|
+
|
|
|
+ client().prepareIndex("test").setId("1").setSource(jsonBuilder()
|
|
|
+ .startObject()
|
|
|
+ .field(defaultGeoFieldName, "POINT(-35 -25)")
|
|
|
+ .endObject()).setRefreshPolicy(IMMEDIATE).get();
|
|
|
+
|
|
|
+ MultiPointBuilder mpb = new MultiPointBuilder().coordinate(-35,-25).coordinate(-15,-5);
|
|
|
+ MultiPoint multiPoint = mpb.buildGeometry();
|
|
|
+
|
|
|
+ {
|
|
|
+ SearchResponse response = client().prepareSearch("test")
|
|
|
+ .setQuery(QueryBuilders.geoShapeQuery(defaultGeoFieldName, multiPoint)).get();
|
|
|
+ SearchHits searchHits = response.getHits();
|
|
|
+ assertEquals(1, searchHits.getTotalHits().value);
|
|
|
+ }
|
|
|
+ {
|
|
|
+ SearchResponse response = client().prepareSearch("test")
|
|
|
+ .setQuery(QueryBuilders.geoShapeQuery(defaultGeoFieldName, multiPoint).relation(ShapeRelation.WITHIN)).get();
|
|
|
+ SearchHits searchHits = response.getHits();
|
|
|
+ assertEquals(1, searchHits.getTotalHits().value);
|
|
|
+ }
|
|
|
+ {
|
|
|
+ SearchResponse response = client().prepareSearch("test")
|
|
|
+ .setQuery(QueryBuilders.geoShapeQuery(defaultGeoFieldName, multiPoint).relation(ShapeRelation.CONTAINS)).get();
|
|
|
+ SearchHits searchHits = response.getHits();
|
|
|
+ assertEquals(0, searchHits.getTotalHits().value);
|
|
|
+ }
|
|
|
+ {
|
|
|
+ SearchResponse response = client().prepareSearch("test")
|
|
|
+ .setQuery(QueryBuilders.geoShapeQuery(defaultGeoFieldName, multiPoint).relation(ShapeRelation.DISJOINT)).get();
|
|
|
+ SearchHits searchHits = response.getHits();
|
|
|
+ assertEquals(0, searchHits.getTotalHits().value);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|