diff --git a/redis-om-spring/src/main/java/com/redis/om/spring/search/stream/ReturnFieldsSearchStreamImpl.java b/redis-om-spring/src/main/java/com/redis/om/spring/search/stream/ReturnFieldsSearchStreamImpl.java index cdbd0208..d17b9039 100644 --- a/redis-om-spring/src/main/java/com/redis/om/spring/search/stream/ReturnFieldsSearchStreamImpl.java +++ b/redis-om-spring/src/main/java/com/redis/om/spring/search/stream/ReturnFieldsSearchStreamImpl.java @@ -289,9 +289,13 @@ private Stream resolveStream() { if (useNoContent) { query.setNoContent(); SearchResult searchResult = entitySearchStream.getOps().search(query); - String keySample = searchResult.getDocuments().get(0).getId(); - int idBegin = keySample.indexOf(":") + 1; - resolvedStream = (Stream) searchResult.getDocuments().stream().map(Document::getId).map(key -> key.substring(idBegin)); + if (searchResult.getDocuments().size() > 0) { + String keySample = searchResult.getDocuments().get(0).getId(); + int idBegin = keySample.indexOf(":") + 1; + resolvedStream = (Stream) searchResult.getDocuments().stream().map(Document::getId).map(key -> key.substring(idBegin)); + } else { + resolvedStream = (Stream) Stream.empty(); + } } else { boolean returningFullEntity = (returning.stream().anyMatch(foi -> foi.getSearchAlias().equalsIgnoreCase("__this"))); diff --git a/redis-om-spring/src/test/java/com/redis/om/spring/search/stream/EntityStreamDocsTest.java b/redis-om-spring/src/test/java/com/redis/om/spring/search/stream/EntityStreamDocsTest.java index eff232e3..0aaf9286 100644 --- a/redis-om-spring/src/test/java/com/redis/om/spring/search/stream/EntityStreamDocsTest.java +++ b/redis-om-spring/src/test/java/com/redis/om/spring/search/stream/EntityStreamDocsTest.java @@ -2431,4 +2431,14 @@ assertThat(d.getId()).isNotNull(); }); } + + @Test void testMapAgainstEmptyResults() { + List names = entityStream // + .of(Company.class) // + .filter(Company$.NAME.startsWith("Open")) + .map(Company$.ID) + .collect(Collectors.toList()); + + assertThat(names).isEmpty(); + } } diff --git a/redis-om-spring/src/test/java/com/redis/om/spring/search/stream/EntityStreamHashTest.java b/redis-om-spring/src/test/java/com/redis/om/spring/search/stream/EntityStreamHashTest.java index 8412f0c7..6d690363 100644 --- a/redis-om-spring/src/test/java/com/redis/om/spring/search/stream/EntityStreamHashTest.java +++ b/redis-om-spring/src/test/java/com/redis/om/spring/search/stream/EntityStreamHashTest.java @@ -1520,4 +1520,14 @@ List names = companies.stream().map(Company::getName).collect(Collectors.toList()); assertThat(names).contains("RedisInc"); } + + @Test void testMapAgainstEmptyResults() { + List names = entityStream // + .of(Company.class) // + .filter(Company$.NAME.startsWith("Open")) + .map(Company$.ID) + .collect(Collectors.toList()); + + assertThat(names).isEmpty(); + } }