Skip to content

Commit aadfae6

Browse files
committed
Pass Page.size() to GEORADIUS COUNT.
We now reuse the page size to initially limit Geo results within Redis. Closes #1242
1 parent a147385 commit aadfae6

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

src/main/java/org/springframework/data/redis/core/RedisQueryEngine.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.springframework.data.keyvalue.core.SpelSortAccessor;
3333
import org.springframework.data.keyvalue.core.query.KeyValueQuery;
3434
import org.springframework.data.redis.connection.RedisGeoCommands.GeoLocation;
35+
import org.springframework.data.redis.connection.RedisGeoCommands.GeoRadiusCommandArgs;
3536
import org.springframework.data.redis.core.convert.GeoIndexedPropertyValue;
3637
import org.springframework.data.redis.core.convert.RedisData;
3738
import org.springframework.data.redis.repository.query.RedisOperationChain;
@@ -104,8 +105,14 @@ private <T> List<T> doFind(RedisOperationChain criteria, long offset, int rows,
104105

105106
if (criteria.getNear() != null) {
106107

108+
GeoRadiusCommandArgs limit = GeoRadiusCommandArgs.newGeoRadiusArgs();
109+
110+
if (rows > 0) {
111+
limit = limit.limit(rows);
112+
}
113+
107114
GeoResults<GeoLocation<byte[]>> x = connection.geoRadius(geoKey(keyspace + ":", criteria.getNear()),
108-
new Circle(criteria.getNear().getPoint(), criteria.getNear().getDistance()));
115+
new Circle(criteria.getNear().getPoint(), criteria.getNear().getDistance()), limit);
109116
for (GeoResult<GeoLocation<byte[]>> y : x) {
110117
allKeys.add(y.getContent().getName());
111118
}

src/test/java/org/springframework/data/redis/repository/RedisRepositoryIntegrationTestBase.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import org.springframework.data.domain.Page;
3737
import org.springframework.data.domain.PageRequest;
3838
import org.springframework.data.domain.Pageable;
39+
import org.springframework.data.domain.Slice;
3940
import org.springframework.data.domain.Sort;
4041
import org.springframework.data.geo.Distance;
4142
import org.springframework.data.geo.Metrics;
@@ -443,6 +444,28 @@ void nearQueryShouldReturnResultsCorrectlyOnNestedProperty() {
443444
assertThat(result).contains(p2).doesNotContain(p1);
444445
}
445446

447+
@Test // GH-1242
448+
void nearQueryShouldConsiderLimit() {
449+
450+
City palermo = new City();
451+
palermo.location = new Point(13.361389D, 38.115556D);
452+
453+
City catania = new City();
454+
catania.location = new Point(15.087269D, 37.502669D);
455+
456+
Person p1 = new Person("foo", "bar");
457+
p1.hometown = palermo;
458+
459+
Person p2 = new Person("two", "two");
460+
p2.hometown = catania;
461+
462+
repo.saveAll(Arrays.asList(p1, p2));
463+
464+
Slice<Person> result = repo.findByHometownLocationNear(new Point(15D, 37D), new Distance(200, Metrics.KILOMETERS),
465+
Pageable.ofSize(1));
466+
assertThat(result).containsOnly(p2);
467+
}
468+
446469
@Test // DATAREDIS-849
447470
void shouldReturnNewObjectInstanceOnImmutableSave() {
448471

@@ -508,6 +531,8 @@ public static interface PersonRepository
508531

509532
List<Person> findByHometownLocationNear(Point point, Distance distance);
510533

534+
Slice<Person> findByHometownLocationNear(Point point, Distance distance, Pageable pageable);
535+
511536
@Override
512537
<S extends Person> List<S> findAll(Example<S> example);
513538
}

0 commit comments

Comments
 (0)