@@ -483,10 +483,30 @@ public void testPointQuery() throws Exception {
483
483
public void testContainsShapeQuery () throws Exception {
484
484
// Create a random geometry collection.
485
485
Rectangle mbr = xRandomRectangle (random (), xRandomPoint (random ()), true );
486
- GeometryCollectionBuilder gcb = createGeometryCollectionWithin (random (), mbr );
486
+ boolean usePrefixTrees = randomBoolean ();
487
+ GeometryCollectionBuilder gcb ;
488
+ if (usePrefixTrees ) {
489
+ gcb = createGeometryCollectionWithin (random (), mbr );
490
+ } else {
491
+ // vector strategy does not yet support multipoint queries
492
+ gcb = new GeometryCollectionBuilder ();
493
+ int numShapes = RandomNumbers .randomIntBetween (random (), 1 , 4 );
494
+ for (int i = 0 ; i < numShapes ; ++i ) {
495
+ ShapeBuilder shape ;
496
+ do {
497
+ shape = RandomShapeGenerator .createShapeWithin (random (), mbr );
498
+ } while (shape instanceof MultiPointBuilder );
499
+ gcb .shape (shape );
500
+ }
501
+ }
487
502
488
- client ().admin ().indices ().prepareCreate ("test" ).addMapping ("type" , "location" , "type=geo_shape,tree=quadtree" )
489
- .get ();
503
+ if (usePrefixTrees ) {
504
+ client ().admin ().indices ().prepareCreate ("test" ).addMapping ("type" , "location" , "type=geo_shape,tree=quadtree" )
505
+ .execute ().actionGet ();
506
+ } else {
507
+ client ().admin ().indices ().prepareCreate ("test" ).addMapping ("type" , "location" , "type=geo_shape" )
508
+ .execute ().actionGet ();
509
+ }
490
510
491
511
XContentBuilder docSource = gcb .toXContent (jsonBuilder ().startObject ().field ("location" ), null ).endObject ();
492
512
client ().prepareIndex ("test" , "type" , "1" ).setSource (docSource ).setRefreshPolicy (IMMEDIATE ).get ();
@@ -763,4 +783,77 @@ public void testEnvelopeSpanningDateline() throws IOException {
763
783
assertNotEquals ("1" , response .getHits ().getAt (0 ).getId ());
764
784
assertNotEquals ("1" , response .getHits ().getAt (1 ).getId ());
765
785
}
786
+
787
+ public void testGeometryCollectionRelations () throws IOException {
788
+ XContentBuilder mapping = XContentFactory .jsonBuilder ().startObject ()
789
+ .startObject ("doc" )
790
+ .startObject ("properties" )
791
+ .startObject ("geo" ).field ("type" , "geo_shape" ).endObject ()
792
+ .endObject ()
793
+ .endObject ()
794
+ .endObject ();
795
+
796
+ createIndex ("test" , Settings .builder ().put ("index.number_of_shards" , 1 ).build (), "doc" , mapping );
797
+
798
+ EnvelopeBuilder envelopeBuilder = new EnvelopeBuilder (new Coordinate (-10 , 10 ), new Coordinate (10 , -10 ));
799
+
800
+ client ().index (new IndexRequest ("test" )
801
+ .source (jsonBuilder ().startObject ().field ("geo" , envelopeBuilder ).endObject ())
802
+ .setRefreshPolicy (IMMEDIATE )).actionGet ();
803
+
804
+ {
805
+ // A geometry collection that is fully within the indexed shape
806
+ GeometryCollectionBuilder builder = new GeometryCollectionBuilder ();
807
+ builder .shape (new PointBuilder (1 , 2 ));
808
+ builder .shape (new PointBuilder (-2 , -1 ));
809
+ SearchResponse response = client ().prepareSearch ("test" )
810
+ .setQuery (geoShapeQuery ("geo" , builder .buildGeometry ()).relation (ShapeRelation .CONTAINS ))
811
+ .get ();
812
+ assertEquals (1 , response .getHits ().getTotalHits ().value );
813
+ response = client ().prepareSearch ("test" )
814
+ .setQuery (geoShapeQuery ("geo" , builder .buildGeometry ()).relation (ShapeRelation .INTERSECTS ))
815
+ .get ();
816
+ assertEquals (1 , response .getHits ().getTotalHits ().value );
817
+ response = client ().prepareSearch ("test" )
818
+ .setQuery (geoShapeQuery ("geo" , builder .buildGeometry ()).relation (ShapeRelation .DISJOINT ))
819
+ .get ();
820
+ assertEquals (0 , response .getHits ().getTotalHits ().value );
821
+ }
822
+ // A geometry collection that is partially within the indexed shape
823
+ {
824
+ GeometryCollectionBuilder builder = new GeometryCollectionBuilder ();
825
+ builder .shape (new PointBuilder (1 , 2 ));
826
+ builder .shape (new PointBuilder (20 , 30 ));
827
+ SearchResponse response = client ().prepareSearch ("test" )
828
+ .setQuery (geoShapeQuery ("geo" , builder .buildGeometry ()).relation (ShapeRelation .CONTAINS ))
829
+ .get ();
830
+ assertEquals (0 , response .getHits ().getTotalHits ().value );
831
+ response = client ().prepareSearch ("test" )
832
+ .setQuery (geoShapeQuery ("geo" , builder .buildGeometry ()).relation (ShapeRelation .INTERSECTS ))
833
+ .get ();
834
+ assertEquals (1 , response .getHits ().getTotalHits ().value );
835
+ response = client ().prepareSearch ("test" )
836
+ .setQuery (geoShapeQuery ("geo" , builder .buildGeometry ()).relation (ShapeRelation .DISJOINT ))
837
+ .get ();
838
+ assertEquals (0 , response .getHits ().getTotalHits ().value );
839
+ }
840
+ {
841
+ // A geometry collection that is disjoint with the indexed shape
842
+ GeometryCollectionBuilder builder = new GeometryCollectionBuilder ();
843
+ builder .shape (new PointBuilder (-20 , -30 ));
844
+ builder .shape (new PointBuilder (20 , 30 ));
845
+ SearchResponse response = client ().prepareSearch ("test" )
846
+ .setQuery (geoShapeQuery ("geo" , builder .buildGeometry ()).relation (ShapeRelation .CONTAINS ))
847
+ .get ();
848
+ assertEquals (0 , response .getHits ().getTotalHits ().value );
849
+ response = client ().prepareSearch ("test" )
850
+ .setQuery (geoShapeQuery ("geo" , builder .buildGeometry ()).relation (ShapeRelation .INTERSECTS ))
851
+ .get ();
852
+ assertEquals (0 , response .getHits ().getTotalHits ().value );
853
+ response = client ().prepareSearch ("test" )
854
+ .setQuery (geoShapeQuery ("geo" , builder .buildGeometry ()).relation (ShapeRelation .DISJOINT ))
855
+ .get ();
856
+ assertEquals (1 , response .getHits ().getTotalHits ().value );
857
+ }
858
+ }
766
859
}
0 commit comments