diff --git a/docs/content/modules/ROOT/pages/entity-streams-aggregations.adoc b/docs/content/modules/ROOT/pages/entity-streams-aggregations.adoc index c1f6ea76..6347c318 100644 --- a/docs/content/modules/ROOT/pages/entity-streams-aggregations.adoc +++ b/docs/content/modules/ROOT/pages/entity-streams-aggregations.adoc @@ -193,7 +193,7 @@ List> page2Departments = entityStream.of(Person.class) .groupBy(Person$.DEPARTMENT_NUMBER) .reduce(COUNT) .sorted(Order.asc("@count")) - .limit(5, 10) // Skip 5, take 10 + .limit(5, 10) // offset=5, limit=10 (skip 5, take 10) .toList(Integer.class, Long.class); ---- diff --git a/redis-om-spring/src/main/java/com/redis/om/spring/repository/support/SimpleRedisDocumentRepository.java b/redis-om-spring/src/main/java/com/redis/om/spring/repository/support/SimpleRedisDocumentRepository.java index b0a07aca..3c7219c2 100644 --- a/redis-om-spring/src/main/java/com/redis/om/spring/repository/support/SimpleRedisDocumentRepository.java +++ b/redis-om-spring/src/main/java/com/redis/om/spring/repository/support/SimpleRedisDocumentRepository.java @@ -648,8 +648,8 @@ public Page findAll(Example example, Pageable pageable) { SearchStream stream = entityStream.of(example.getProbeType()); var offset = pageable.getPageNumber() * pageable.getPageSize(); var limit = pageable.getPageSize(); - Page page = stream.filter(example).dialect(Dialect.TWO.getValue()).loadAll().limit(limit, Math.toIntExact( - offset)).toList(pageable, stream.getEntityClass()); + Page page = stream.filter(example).dialect(Dialect.TWO.getValue()).loadAll().limit(Math.toIntExact(offset), + limit).toList(pageable, stream.getEntityClass()); return page; } diff --git a/redis-om-spring/src/main/java/com/redis/om/spring/repository/support/SimpleRedisEnhancedRepository.java b/redis-om-spring/src/main/java/com/redis/om/spring/repository/support/SimpleRedisEnhancedRepository.java index 39b44704..adcf3c9a 100644 --- a/redis-om-spring/src/main/java/com/redis/om/spring/repository/support/SimpleRedisEnhancedRepository.java +++ b/redis-om-spring/src/main/java/com/redis/om/spring/repository/support/SimpleRedisEnhancedRepository.java @@ -580,7 +580,7 @@ public Page findAll(Example example, Pageable pageable) { SearchStream stream = entityStream.of(example.getProbeType()); var offset = pageable.getPageNumber() * pageable.getPageSize(); var limit = pageable.getPageSize(); - Page page = stream.filter(example).loadAll().limit(limit, Math.toIntExact(offset)).toList(pageable, stream + Page page = stream.filter(example).loadAll().limit(Math.toIntExact(offset), limit).toList(pageable, stream .getEntityClass()); return page; diff --git a/redis-om-spring/src/main/java/com/redis/om/spring/search/stream/AggregationStream.java b/redis-om-spring/src/main/java/com/redis/om/spring/search/stream/AggregationStream.java index b1ff197e..10e6e183 100644 --- a/redis-om-spring/src/main/java/com/redis/om/spring/search/stream/AggregationStream.java +++ b/redis-om-spring/src/main/java/com/redis/om/spring/search/stream/AggregationStream.java @@ -167,13 +167,23 @@ public interface AggregationStream { /** * Limits the number of results and applies an offset for pagination. + *

+ * This method controls the pagination of aggregation results by specifying + * how many results to skip (offset) and how many to return (limit). + *

+ *

+ * Example usage: + *

{@code
+   * // Skip the first 10 results and return the next 5
+   * stream.limit(10, 5)  // offset=10, limit=5
+   * }
* + * @param offset the number of results to skip before starting to return results (0-based) * @param limit the maximum number of results to return - * @param offset the number of results to skip * @return a new AggregationStream with the limit and offset applied * @throws IllegalArgumentException if limit or offset is negative */ - AggregationStream limit(int limit, int offset); + AggregationStream limit(int offset, int limit); /** * Applies filter expressions to the aggregation pipeline. diff --git a/redis-om-spring/src/main/java/com/redis/om/spring/search/stream/AggregationStreamImpl.java b/redis-om-spring/src/main/java/com/redis/om/spring/search/stream/AggregationStreamImpl.java index 5a5a5bbc..a6ce0884 100644 --- a/redis-om-spring/src/main/java/com/redis/om/spring/search/stream/AggregationStreamImpl.java +++ b/redis-om-spring/src/main/java/com/redis/om/spring/search/stream/AggregationStreamImpl.java @@ -252,7 +252,7 @@ public AggregationStream limit(int limit) { } @Override - public AggregationStream limit(int limit, int offset) { + public AggregationStream limit(int offset, int limit) { applyCurrentGroupBy(); aggregation.limit(offset, limit); limitSet = true; diff --git a/tests/src/test/java/com/redis/om/spring/search/stream/EntityStreamsAggregationsDocsTest.java b/tests/src/test/java/com/redis/om/spring/search/stream/EntityStreamsAggregationsDocsTest.java index 57c011a5..7d369c61 100644 --- a/tests/src/test/java/com/redis/om/spring/search/stream/EntityStreamsAggregationsDocsTest.java +++ b/tests/src/test/java/com/redis/om/spring/search/stream/EntityStreamsAggregationsDocsTest.java @@ -985,4 +985,23 @@ void testNotExistPredicate() { assertThat(releasedFilms.get(0).getFirst()).isEqualTo(1); } + @Test + void testAggregationLimitWithOffsetParameterOrder() { + // Test the correct parameter order: offset first, then limit + // We want to skip 2 games and get the next 3 + List> results = entityStream.of(Game.class) + .groupBy(Game$.TITLE) + .reduce(ReducerFunction.COUNT).as("count") + .sorted(Order.asc("@title")) + .limit(2, 3) // Skip 2, take 3 (offset=2, limit=3) + .toList(String.class, String.class); + + // We should get exactly 3 results after skipping the first 2 + assertThat(results).hasSize(3); + + // Verify we're getting the 3rd, 4th, and 5th games alphabetically + // (skipping the first 2) + assertThat(results).isNotEmpty(); + } + }