41
41
import org .springframework .data .domain .OffsetScrollPosition ;
42
42
import org .springframework .data .domain .Page ;
43
43
import org .springframework .data .domain .PageRequest ;
44
+ import org .springframework .data .domain .Range ;
44
45
import org .springframework .data .domain .ScrollPosition ;
45
46
import org .springframework .data .domain .Slice ;
46
47
import org .springframework .data .domain .Sort ;
47
48
import org .springframework .data .domain .Window ;
48
49
import org .springframework .data .geo .Box ;
49
50
import org .springframework .data .geo .Circle ;
50
51
import org .springframework .data .geo .Distance ;
52
+ import org .springframework .data .geo .GeoPage ;
53
+ import org .springframework .data .geo .GeoResult ;
51
54
import org .springframework .data .geo .GeoResults ;
52
55
import org .springframework .data .geo .Metrics ;
53
56
import org .springframework .data .geo .Point ;
@@ -96,7 +99,7 @@ MongoOperations mongoOperations() {
96
99
@ BeforeAll
97
100
static void beforeAll () {
98
101
client .getDatabase (DB_NAME ).getCollection ("user" ).createIndex (new Document ("location.coordinates" , "2d" ),
99
- new IndexOptions ());
102
+ new IndexOptions ());
100
103
}
101
104
102
105
@ BeforeEach
@@ -613,21 +616,21 @@ void testAggregationWithCollation() {
613
616
void testNear () {
614
617
615
618
List <User > users = fragment .findByLocationCoordinatesNear (new Point (-73.99171 , 40.738868 ));
616
- assertThat (users ).extracting (User ::getUsername ).containsExactly ("leia" );
619
+ assertThat (users ).extracting (User ::getUsername ).containsExactly ("leia" , "vader" );
617
620
}
618
621
619
622
@ Test
620
623
void testNearWithGeoJson () {
621
624
622
625
List <User > users = fragment .findByLocationCoordinatesNear (new GeoJsonPoint (-73.99171 , 40.738868 ));
623
- assertThat (users ).extracting (User ::getUsername ).containsExactly ("leia" );
626
+ assertThat (users ).extracting (User ::getUsername ).containsExactly ("leia" , "vader" );
624
627
}
625
628
626
629
@ Test
627
630
void testGeoWithinCircle () {
628
631
629
632
List <User > users = fragment .findByLocationCoordinatesWithin (new Circle (-78.99171 , 45.738868 , 170 ));
630
- assertThat (users ).extracting (User ::getUsername ).containsExactly ("leia" );
633
+ assertThat (users ).extracting (User ::getUsername ).containsExactly ("leia" , "vader" );
631
634
}
632
635
633
636
@ Test
@@ -636,7 +639,7 @@ void testWithinBox() {
636
639
Box box = new Box (new Point (-78.99171 , 35.738868 ), new Point (-68.99171 , 45.738868 ));
637
640
638
641
List <User > result = fragment .findByLocationCoordinatesWithin (box );
639
- assertThat (result ).extracting (User ::getUsername ).containsExactly ("leia" );
642
+ assertThat (result ).extracting (User ::getUsername ).containsExactly ("leia" , "vader" );
640
643
}
641
644
642
645
@ Test
@@ -648,18 +651,51 @@ void findsPeopleByLocationWithinPolygon() {
648
651
Point fourth = new Point (-68.99171 , 35.738868 );
649
652
650
653
List <User > result = fragment .findByLocationCoordinatesWithin (new Polygon (first , second , third , fourth ));
651
- assertThat (result ).extracting (User ::getUsername ).containsExactly ("leia" );
654
+ assertThat (result ).extracting (User ::getUsername ).containsExactly ("leia" , "vader" );
652
655
}
653
656
654
-
655
657
@ Test
656
658
void testNearWithGeoResult () {
657
659
658
- GeoResults <User > users = fragment .findByLocationCoordinatesNear (new Point (-73.99 , 40.73 ), Distance .of (2000 , Metrics .KILOMETERS ));
659
- System .out .println ("users: " + users );
660
- assertThat (users ).isNotEmpty ();
660
+ GeoResults <User > users = fragment .findByLocationCoordinatesNear (new Point (-73.99 , 40.73 ),
661
+ Distance .of (5 , Metrics .KILOMETERS ));
662
+ assertThat (users ).extracting (GeoResult ::getContent ).extracting (User ::getUsername ).containsExactly ("leia" );
663
+ }
664
+
665
+ @ Test
666
+ void testNearReturningListOfGeoResult () {
667
+
668
+ List <GeoResult <User >> users = fragment .findUserAsListByLocationCoordinatesNear (new Point (-73.99 , 40.73 ),
669
+ Distance .of (5 , Metrics .KILOMETERS ));
670
+ assertThat (users ).extracting (GeoResult ::getContent ).extracting (User ::getUsername ).containsExactly ("leia" );
671
+ }
672
+
673
+ @ Test
674
+ void testNearWithRange () {
675
+
676
+ Range <Distance > range = Distance .between (Distance .of (5 , Metrics .KILOMETERS ), Distance .of (2000 , Metrics .KILOMETERS ));
677
+ GeoResults <User > users = fragment .findByLocationCoordinatesNear (new Point (-73.99 , 40.73 ), range );
678
+
679
+ assertThat (users ).extracting (GeoResult ::getContent ).extracting (User ::getUsername ).containsExactly ("vader" );
661
680
}
662
681
682
+ @ Test
683
+ void testNearReturningGeoPage () {
684
+
685
+ // TODO: still need to create the count and extract the total elements
686
+ GeoPage <User > page1 = fragment .findByLocationCoordinatesNear (new Point (-73.99 , 40.73 ),
687
+ Distance .of (2000 , Metrics .KILOMETERS ), PageRequest .of (0 , 1 ));
688
+
689
+ assertThat (page1 .hasNext ()).isTrue ();
690
+
691
+ GeoPage <User > page2 = fragment .findByLocationCoordinatesNear (new Point (-73.99 , 40.73 ),
692
+ Distance .of (2000 , Metrics .KILOMETERS ), page1 .nextPageable ());
693
+ assertThat (page2 .hasNext ()).isFalse ();
694
+ }
695
+
696
+ /**
697
+ * GeoResults<Person> results = repository.findPersonByLocationNear(new Point(-73.99, 40.73), range);
698
+ */
663
699
private static void initUsers () {
664
700
665
701
Document luke = Document .parse ("""
@@ -753,6 +789,12 @@ private static void initUsers() {
753
789
"username": "vader",
754
790
"first_name": "Anakin",
755
791
"last_name": "Skywalker",
792
+ "location" : {
793
+ "planet" : "Death Star",
794
+ "coordinates" : {
795
+ "x" : -73.9, "y" : 40.7
796
+ }
797
+ },
756
798
"visits" : 50,
757
799
"posts": [
758
800
{
0 commit comments