Search in sources :

Example 1 with GeoShapeQueryBuilder

use of org.elasticsearch.index.query.GeoShapeQueryBuilder in project elasticsearch by elastic.

the class GeoShapeQueryTests method testShapeFilterWithDefinedGeoCollection.

public void testShapeFilterWithDefinedGeoCollection() throws Exception {
    createIndex("shapes");
    client().admin().indices().prepareCreate("test").addMapping("type", "location", "type=geo_shape,tree=quadtree").execute().actionGet();
    XContentBuilder docSource = jsonBuilder().startObject().startObject("location").field("type", "geometrycollection").startArray("geometries").startObject().field("type", "point").startArray("coordinates").value(100.0).value(0.0).endArray().endObject().startObject().field("type", "linestring").startArray("coordinates").startArray().value(101.0).value(0.0).endArray().startArray().value(102.0).value(1.0).endArray().endArray().endObject().endArray().endObject().endObject();
    client().prepareIndex("test", "type", "1").setSource(docSource).setRefreshPolicy(IMMEDIATE).get();
    GeoShapeQueryBuilder filter = QueryBuilders.geoShapeQuery("location", ShapeBuilders.newGeometryCollection().polygon(ShapeBuilders.newPolygon(new CoordinatesBuilder().coordinate(99.0, -1.0).coordinate(99.0, 3.0).coordinate(103.0, 3.0).coordinate(103.0, -1.0).coordinate(99.0, -1.0)))).relation(ShapeRelation.INTERSECTS);
    SearchResponse result = client().prepareSearch("test").setTypes("type").setQuery(QueryBuilders.matchAllQuery()).setPostFilter(filter).get();
    assertSearchResponse(result);
    assertHitCount(result, 1);
    filter = QueryBuilders.geoShapeQuery("location", ShapeBuilders.newGeometryCollection().polygon(ShapeBuilders.newPolygon(new CoordinatesBuilder().coordinate(199.0, -11.0).coordinate(199.0, 13.0).coordinate(193.0, 13.0).coordinate(193.0, -11.0).coordinate(199.0, -11.0)))).relation(ShapeRelation.INTERSECTS);
    result = client().prepareSearch("test").setTypes("type").setQuery(QueryBuilders.matchAllQuery()).setPostFilter(filter).get();
    assertSearchResponse(result);
    assertHitCount(result, 0);
    filter = QueryBuilders.geoShapeQuery("location", ShapeBuilders.newGeometryCollection().polygon(ShapeBuilders.newPolygon(new CoordinatesBuilder().coordinate(99.0, -1.0).coordinate(99.0, 3.0).coordinate(103.0, 3.0).coordinate(103.0, -1.0).coordinate(99.0, -1.0))).polygon(ShapeBuilders.newPolygon(new CoordinatesBuilder().coordinate(199.0, -11.0).coordinate(199.0, 13.0).coordinate(193.0, 13.0).coordinate(193.0, -11.0).coordinate(199.0, -11.0)))).relation(ShapeRelation.INTERSECTS);
    result = client().prepareSearch("test").setTypes("type").setQuery(QueryBuilders.matchAllQuery()).setPostFilter(filter).get();
    assertSearchResponse(result);
    assertHitCount(result, 1);
    // no shape
    filter = QueryBuilders.geoShapeQuery("location", ShapeBuilders.newGeometryCollection());
    result = client().prepareSearch("test").setTypes("type").setQuery(QueryBuilders.matchAllQuery()).setPostFilter(filter).get();
    assertSearchResponse(result);
    assertHitCount(result, 0);
}
Also used : CoordinatesBuilder(org.elasticsearch.common.geo.builders.CoordinatesBuilder) GeoShapeQueryBuilder(org.elasticsearch.index.query.GeoShapeQueryBuilder) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 2 with GeoShapeQueryBuilder

use of org.elasticsearch.index.query.GeoShapeQueryBuilder in project elasticsearch by elastic.

the class GeoShapeQueryTests method testShapeFetchingPath.

public void testShapeFetchingPath() throws Exception {
    createIndex("shapes");
    client().admin().indices().prepareCreate("test").addMapping("type", "location", "type=geo_shape").execute().actionGet();
    String location = "\"location\" : {\"type\":\"polygon\", \"coordinates\":[[[-10,-10],[10,-10],[10,10],[-10,10],[-10,-10]]]}";
    client().prepareIndex("shapes", "type", "1").setSource(String.format(Locale.ROOT, "{ %s, \"1\" : { %s, \"2\" : { %s, \"3\" : { %s } }} }", location, location, location, location), XContentType.JSON).setRefreshPolicy(IMMEDIATE).get();
    client().prepareIndex("test", "type", "1").setSource(jsonBuilder().startObject().startObject("location").field("type", "polygon").startArray("coordinates").startArray().startArray().value(-20).value(-20).endArray().startArray().value(20).value(-20).endArray().startArray().value(20).value(20).endArray().startArray().value(-20).value(20).endArray().startArray().value(-20).value(-20).endArray().endArray().endArray().endObject().endObject()).setRefreshPolicy(IMMEDIATE).get();
    GeoShapeQueryBuilder filter = QueryBuilders.geoShapeQuery("location", "1", "type").relation(ShapeRelation.INTERSECTS).indexedShapeIndex("shapes").indexedShapePath("location");
    SearchResponse result = client().prepareSearch("test").setQuery(QueryBuilders.matchAllQuery()).setPostFilter(filter).get();
    assertSearchResponse(result);
    assertHitCount(result, 1);
    filter = QueryBuilders.geoShapeQuery("location", "1", "type").relation(ShapeRelation.INTERSECTS).indexedShapeIndex("shapes").indexedShapePath("1.location");
    result = client().prepareSearch("test").setQuery(QueryBuilders.matchAllQuery()).setPostFilter(filter).get();
    assertSearchResponse(result);
    assertHitCount(result, 1);
    filter = QueryBuilders.geoShapeQuery("location", "1", "type").relation(ShapeRelation.INTERSECTS).indexedShapeIndex("shapes").indexedShapePath("1.2.location");
    result = client().prepareSearch("test").setQuery(QueryBuilders.matchAllQuery()).setPostFilter(filter).get();
    assertSearchResponse(result);
    assertHitCount(result, 1);
    filter = QueryBuilders.geoShapeQuery("location", "1", "type").relation(ShapeRelation.INTERSECTS).indexedShapeIndex("shapes").indexedShapePath("1.2.3.location");
    result = client().prepareSearch("test").setQuery(QueryBuilders.matchAllQuery()).setPostFilter(filter).get();
    assertSearchResponse(result);
    assertHitCount(result, 1);
    // now test the query variant
    GeoShapeQueryBuilder query = QueryBuilders.geoShapeQuery("location", "1", "type").indexedShapeIndex("shapes").indexedShapePath("location");
    result = client().prepareSearch("test").setQuery(query).get();
    assertSearchResponse(result);
    assertHitCount(result, 1);
    query = QueryBuilders.geoShapeQuery("location", "1", "type").indexedShapeIndex("shapes").indexedShapePath("1.location");
    result = client().prepareSearch("test").setQuery(query).get();
    assertSearchResponse(result);
    assertHitCount(result, 1);
    query = QueryBuilders.geoShapeQuery("location", "1", "type").indexedShapeIndex("shapes").indexedShapePath("1.2.location");
    result = client().prepareSearch("test").setQuery(query).get();
    assertSearchResponse(result);
    assertHitCount(result, 1);
    query = QueryBuilders.geoShapeQuery("location", "1", "type").indexedShapeIndex("shapes").indexedShapePath("1.2.3.location");
    result = client().prepareSearch("test").setQuery(query).get();
    assertSearchResponse(result);
    assertHitCount(result, 1);
}
Also used : GeoShapeQueryBuilder(org.elasticsearch.index.query.GeoShapeQueryBuilder) Matchers.containsString(org.hamcrest.Matchers.containsString) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 3 with GeoShapeQueryBuilder

use of org.elasticsearch.index.query.GeoShapeQueryBuilder in project elasticsearch by elastic.

the class ContextAndHeaderTransportIT method testThatGeoShapeQueryGetRequestContainsContextAndHeaders.

public void testThatGeoShapeQueryGetRequestContainsContextAndHeaders() throws Exception {
    transportClient().prepareIndex(lookupIndex, "type", "1").setSource(jsonBuilder().startObject().field("name", "Munich Suburban Area").startObject("location").field("type", "polygon").startArray("coordinates").startArray().startArray().value(11.34).value(48.25).endArray().startArray().value(11.68).value(48.25).endArray().startArray().value(11.65).value(48.06).endArray().startArray().value(11.37).value(48.13).endArray().startArray().value(11.34).value(48.25).endArray().endArray().endArray().endObject().endObject()).get();
    // second document
    transportClient().prepareIndex(queryIndex, "type", "1").setSource(jsonBuilder().startObject().field("name", "Munich Center").startObject("location").field("type", "point").startArray("coordinates").value(11.57).value(48.13).endArray().endObject().endObject()).get();
    transportClient().admin().indices().prepareRefresh(lookupIndex, queryIndex).get();
    GeoShapeQueryBuilder queryBuilder = QueryBuilders.geoShapeQuery("location", "1", "type").indexedShapeIndex(lookupIndex).indexedShapePath("location");
    SearchResponse searchResponse = transportClient().prepareSearch(queryIndex).setQuery(queryBuilder).get();
    assertNoFailures(searchResponse);
    assertHitCount(searchResponse, 1);
    assertThat(requests, hasSize(greaterThan(0)));
    assertGetRequestsContainHeaders();
}
Also used : GeoShapeQueryBuilder(org.elasticsearch.index.query.GeoShapeQueryBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 4 with GeoShapeQueryBuilder

use of org.elasticsearch.index.query.GeoShapeQueryBuilder in project elasticsearch by elastic.

the class GeoShapeQueryTests method testShapeFilterWithRandomGeoCollection.

public void testShapeFilterWithRandomGeoCollection() throws Exception {
    // Create a random geometry collection.
    GeometryCollectionBuilder gcb = RandomShapeGenerator.createGeometryCollection(random());
    logger.info("Created Random GeometryCollection containing {} shapes", gcb.numShapes());
    client().admin().indices().prepareCreate("test").addMapping("type", "location", "type=geo_shape,tree=quadtree").execute().actionGet();
    XContentBuilder docSource = gcb.toXContent(jsonBuilder().startObject().field("location"), null).endObject();
    client().prepareIndex("test", "type", "1").setSource(docSource).setRefreshPolicy(IMMEDIATE).get();
    ShapeBuilder filterShape = (gcb.getShapeAt(randomIntBetween(0, gcb.numShapes() - 1)));
    GeoShapeQueryBuilder filter = QueryBuilders.geoShapeQuery("location", filterShape);
    filter.relation(ShapeRelation.INTERSECTS);
    SearchResponse result = client().prepareSearch("test").setTypes("type").setQuery(QueryBuilders.matchAllQuery()).setPostFilter(filter).get();
    assertSearchResponse(result);
    assertHitCount(result, 1);
}
Also used : GeometryCollectionBuilder(org.elasticsearch.common.geo.builders.GeometryCollectionBuilder) ShapeBuilder(org.elasticsearch.common.geo.builders.ShapeBuilder) GeoShapeQueryBuilder(org.elasticsearch.index.query.GeoShapeQueryBuilder) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 5 with GeoShapeQueryBuilder

use of org.elasticsearch.index.query.GeoShapeQueryBuilder in project elasticsearch by elastic.

the class GeoShapeQueryTests method testContainsShapeQuery.

public void testContainsShapeQuery() throws Exception {
    // Create a random geometry collection.
    Rectangle mbr = xRandomRectangle(random(), xRandomPoint(random()), true);
    GeometryCollectionBuilder gcb = createGeometryCollectionWithin(random(), mbr);
    client().admin().indices().prepareCreate("test").addMapping("type", "location", "type=geo_shape,tree=quadtree").execute().actionGet();
    XContentBuilder docSource = gcb.toXContent(jsonBuilder().startObject().field("location"), null).endObject();
    client().prepareIndex("test", "type", "1").setSource(docSource).setRefreshPolicy(IMMEDIATE).get();
    // index the mbr of the collection
    EnvelopeBuilder env = new EnvelopeBuilder(new Coordinate(mbr.getMinX(), mbr.getMaxY()), new Coordinate(mbr.getMaxX(), mbr.getMinY()));
    docSource = env.toXContent(jsonBuilder().startObject().field("location"), null).endObject();
    client().prepareIndex("test", "type", "2").setSource(docSource).setRefreshPolicy(IMMEDIATE).get();
    ShapeBuilder filterShape = (gcb.getShapeAt(randomIntBetween(0, gcb.numShapes() - 1)));
    GeoShapeQueryBuilder filter = QueryBuilders.geoShapeQuery("location", filterShape).relation(ShapeRelation.CONTAINS);
    SearchResponse response = client().prepareSearch("test").setTypes("type").setQuery(QueryBuilders.matchAllQuery()).setPostFilter(filter).get();
    assertSearchResponse(response);
    assertThat(response.getHits().getTotalHits(), greaterThan(0L));
}
Also used : GeometryCollectionBuilder(org.elasticsearch.common.geo.builders.GeometryCollectionBuilder) ShapeBuilder(org.elasticsearch.common.geo.builders.ShapeBuilder) GeoShapeQueryBuilder(org.elasticsearch.index.query.GeoShapeQueryBuilder) Coordinate(com.vividsolutions.jts.geom.Coordinate) Rectangle(org.locationtech.spatial4j.shape.Rectangle) RandomShapeGenerator.xRandomRectangle(org.elasticsearch.test.geo.RandomShapeGenerator.xRandomRectangle) EnvelopeBuilder(org.elasticsearch.common.geo.builders.EnvelopeBuilder) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Aggregations

SearchResponse (org.elasticsearch.action.search.SearchResponse)5 GeoShapeQueryBuilder (org.elasticsearch.index.query.GeoShapeQueryBuilder)5 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)4 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)3 GeometryCollectionBuilder (org.elasticsearch.common.geo.builders.GeometryCollectionBuilder)2 ShapeBuilder (org.elasticsearch.common.geo.builders.ShapeBuilder)2 Coordinate (com.vividsolutions.jts.geom.Coordinate)1 CoordinatesBuilder (org.elasticsearch.common.geo.builders.CoordinatesBuilder)1 EnvelopeBuilder (org.elasticsearch.common.geo.builders.EnvelopeBuilder)1 RandomShapeGenerator.xRandomRectangle (org.elasticsearch.test.geo.RandomShapeGenerator.xRandomRectangle)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 Rectangle (org.locationtech.spatial4j.shape.Rectangle)1