Skip to content

Commit 7d50b9a

Browse files
committed
fix: handle String values from Redis FT.INFO in count() method (#639)
Fixes ClassCastException when count() method receives "num_docs" as a String from Redis FT.INFO command instead of a numeric type. The method now properly handles both String and Number types by parsing or converting them to long. - Parse String values using Long.parseLong() - Convert Number values using longValue() - Return 0 for null or unexpected types - Added test case to verify fix in EntityStreamsIssuesTest Closes #639
1 parent 9dce4fc commit 7d50b9a

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

redis-om-spring/src/main/java/com/redis/om/spring/search/stream/SearchStreamImpl.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,17 @@ public long count() {
496496
return searchResult.getTotalResults();
497497
} else {
498498
var info = search.getInfo();
499-
return (long) info.get("num_docs");
499+
Object numDocsValue = info.get("num_docs");
500+
501+
// Handle different return types from Redis (fixes issue #639)
502+
if (numDocsValue instanceof String) {
503+
return Long.parseLong((String) numDocsValue);
504+
} else if (numDocsValue instanceof Number) {
505+
return ((Number) numDocsValue).longValue();
506+
} else {
507+
// Fallback to 0 if the value is null or unexpected type
508+
return 0L;
509+
}
500510
}
501511
}
502512

tests/src/test/java/com/redis/om/spring/search/stream/EntityStreamsIssuesTest.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,4 +609,37 @@ void testEqAgainstContentWithForwardSlash() {
609609
doc2Repository.deleteAll(List.of(doc1, doc2));
610610
}
611611

612+
@Test
613+
void testIssue639_CountMethodHandlesStringNumDocs() {
614+
// Issue #639: ClassCastException in count() when num_docs is returned as String
615+
// This test ensures that count() properly handles String values from Redis FT.INFO
616+
617+
// Clear existing data
618+
docRepository.deleteAll();
619+
620+
// Create exactly 21 documents (matching the issue report)
621+
List<Doc> docs = new ArrayList<>();
622+
for (int i = 1; i <= 21; i++) {
623+
Doc doc = Doc.of("first" + i, "second" + i);
624+
doc.setId("doc" + i);
625+
docs.add(doc);
626+
}
627+
docRepository.saveAll(docs);
628+
629+
// Test count with no filter - this triggers the info.get("num_docs") path
630+
long count = entityStream.of(Doc.class).count();
631+
assertThat(count).isEqualTo(21);
632+
633+
// Test count with filter - uses query-based count
634+
long filteredCount = entityStream.of(Doc.class)
635+
.filter(Doc$.FIRST.startsWith("first"))
636+
.count();
637+
assertThat(filteredCount).isEqualTo(21);
638+
639+
// Clean up
640+
docRepository.deleteAll();
641+
long finalCount = entityStream.of(Doc.class).count();
642+
assertThat(finalCount).isEqualTo(0);
643+
}
644+
612645
}

0 commit comments

Comments
 (0)