@@ -447,10 +447,30 @@ public void testPointQuery() throws Exception {
447
447
public void testContainsShapeQuery () throws Exception {
448
448
// Create a random geometry collection.
449
449
Rectangle mbr = xRandomRectangle (random (), xRandomPoint (random ()), true );
450
- GeometryCollectionBuilder gcb = createGeometryCollectionWithin (random (), mbr );
450
+ boolean usePrefixTrees = randomBoolean ();
451
+ GeometryCollectionBuilder gcb ;
452
+ if (usePrefixTrees ) {
453
+ gcb = createGeometryCollectionWithin (random (), mbr );
454
+ } else {
455
+ // vector strategy does not yet support multipoint queries
456
+ gcb = new GeometryCollectionBuilder ();
457
+ int numShapes = RandomNumbers .randomIntBetween (random (), 1 , 4 );
458
+ for (int i = 0 ; i < numShapes ; ++i ) {
459
+ ShapeBuilder shape ;
460
+ do {
461
+ shape = RandomShapeGenerator .createShapeWithin (random (), mbr );
462
+ } while (shape instanceof MultiPointBuilder );
463
+ gcb .shape (shape );
464
+ }
465
+ }
451
466
452
- client ().admin ().indices ().prepareCreate ("test" ).addMapping ("type" , "location" , "type=geo_shape,tree=quadtree" )
453
- .get ();
467
+ if (usePrefixTrees ) {
468
+ client ().admin ().indices ().prepareCreate ("test" ).addMapping ("type" , "location" , "type=geo_shape,tree=quadtree" )
469
+ .execute ().actionGet ();
470
+ } else {
471
+ client ().admin ().indices ().prepareCreate ("test" ).addMapping ("type" , "location" , "type=geo_shape" )
472
+ .execute ().actionGet ();
473
+ }
454
474
455
475
XContentBuilder docSource = gcb .toXContent (jsonBuilder ().startObject ().field ("location" ), null ).endObject ();
456
476
client ().prepareIndex ("test" ).setId ("1" ).setSource (docSource ).setRefreshPolicy (IMMEDIATE ).get ();
@@ -727,4 +747,77 @@ public void testEnvelopeSpanningDateline() throws IOException {
727
747
assertNotEquals ("1" , response .getHits ().getAt (0 ).getId ());
728
748
assertNotEquals ("1" , response .getHits ().getAt (1 ).getId ());
729
749
}
750
+
751
+ public void testGeometryCollectionRelations () throws IOException {
752
+ XContentBuilder mapping = XContentFactory .jsonBuilder ().startObject ()
753
+ .startObject ("doc" )
754
+ .startObject ("properties" )
755
+ .startObject ("geo" ).field ("type" , "geo_shape" ).endObject ()
756
+ .endObject ()
757
+ .endObject ()
758
+ .endObject ();
759
+
760
+ createIndex ("test" , Settings .builder ().put ("index.number_of_shards" , 1 ).build (), "doc" , mapping );
761
+
762
+ EnvelopeBuilder envelopeBuilder = new EnvelopeBuilder (new Coordinate (-10 , 10 ), new Coordinate (10 , -10 ));
763
+
764
+ client ().index (new IndexRequest ("test" )
765
+ .source (jsonBuilder ().startObject ().field ("geo" , envelopeBuilder ).endObject ())
766
+ .setRefreshPolicy (IMMEDIATE )).actionGet ();
767
+
768
+ {
769
+ // A geometry collection that is fully within the indexed shape
770
+ GeometryCollectionBuilder builder = new GeometryCollectionBuilder ();
771
+ builder .shape (new PointBuilder (1 , 2 ));
772
+ builder .shape (new PointBuilder (-2 , -1 ));
773
+ SearchResponse response = client ().prepareSearch ("test" )
774
+ .setQuery (geoShapeQuery ("geo" , builder .buildGeometry ()).relation (ShapeRelation .CONTAINS ))
775
+ .get ();
776
+ assertEquals (1 , response .getHits ().getTotalHits ().value );
777
+ response = client ().prepareSearch ("test" )
778
+ .setQuery (geoShapeQuery ("geo" , builder .buildGeometry ()).relation (ShapeRelation .INTERSECTS ))
779
+ .get ();
780
+ assertEquals (1 , response .getHits ().getTotalHits ().value );
781
+ response = client ().prepareSearch ("test" )
782
+ .setQuery (geoShapeQuery ("geo" , builder .buildGeometry ()).relation (ShapeRelation .DISJOINT ))
783
+ .get ();
784
+ assertEquals (0 , response .getHits ().getTotalHits ().value );
785
+ }
786
+ // A geometry collection that is partially within the indexed shape
787
+ {
788
+ GeometryCollectionBuilder builder = new GeometryCollectionBuilder ();
789
+ builder .shape (new PointBuilder (1 , 2 ));
790
+ builder .shape (new PointBuilder (20 , 30 ));
791
+ SearchResponse response = client ().prepareSearch ("test" )
792
+ .setQuery (geoShapeQuery ("geo" , builder .buildGeometry ()).relation (ShapeRelation .CONTAINS ))
793
+ .get ();
794
+ assertEquals (0 , response .getHits ().getTotalHits ().value );
795
+ response = client ().prepareSearch ("test" )
796
+ .setQuery (geoShapeQuery ("geo" , builder .buildGeometry ()).relation (ShapeRelation .INTERSECTS ))
797
+ .get ();
798
+ assertEquals (1 , response .getHits ().getTotalHits ().value );
799
+ response = client ().prepareSearch ("test" )
800
+ .setQuery (geoShapeQuery ("geo" , builder .buildGeometry ()).relation (ShapeRelation .DISJOINT ))
801
+ .get ();
802
+ assertEquals (0 , response .getHits ().getTotalHits ().value );
803
+ }
804
+ {
805
+ // A geometry collection that is disjoint with the indexed shape
806
+ GeometryCollectionBuilder builder = new GeometryCollectionBuilder ();
807
+ builder .shape (new PointBuilder (-20 , -30 ));
808
+ builder .shape (new PointBuilder (20 , 30 ));
809
+ SearchResponse response = client ().prepareSearch ("test" )
810
+ .setQuery (geoShapeQuery ("geo" , builder .buildGeometry ()).relation (ShapeRelation .CONTAINS ))
811
+ .get ();
812
+ assertEquals (0 , response .getHits ().getTotalHits ().value );
813
+ response = client ().prepareSearch ("test" )
814
+ .setQuery (geoShapeQuery ("geo" , builder .buildGeometry ()).relation (ShapeRelation .INTERSECTS ))
815
+ .get ();
816
+ assertEquals (0 , response .getHits ().getTotalHits ().value );
817
+ response = client ().prepareSearch ("test" )
818
+ .setQuery (geoShapeQuery ("geo" , builder .buildGeometry ()).relation (ShapeRelation .DISJOINT ))
819
+ .get ();
820
+ assertEquals (1 , response .getHits ().getTotalHits ().value );
821
+ }
822
+ }
730
823
}
0 commit comments