spring-projects/spring-ai · error · IllegalStateException
Failed to execute count query
Error message
Failed to execute count query
What it means
The count-query path in RedisVectorStore executes an FT.SEARCH with a count-only query and returns result.getTotalResults(). Any exception during execution (connection failure, missing index, bad query) is caught, logged, and rethrown as IllegalStateException 'Failed to execute count query' with the cause attached. The constructor context (RedisVectorStore public) indicates this can surface while wiring/validating the store.
Source
Thrown at vector-stores/spring-ai-redis-store/src/main/java/org/springframework/ai/vectorstore/redis/RedisVectorStore.java:1213
* @param filterExpression the Redis filter expression string
* @return the count of matching documents
*/
private long executeCountQuery(String filterExpression) {
// Create a query with the filter, limiting to 0 results to only get count
Query query = new Query(filterExpression).returnFields("id") // Minimal field to
// return
.limit(0, 0) // No actual results, just count
.dialect(2); // Use dialect 2 for advanced query features
try {
SearchResult result = this.jedisClient.ftSearch(this.indexName, query);
return result.getTotalResults();
}
catch (Exception e) {
if (logger.isErrorEnabled()) {
logger.error("Error executing count query: " + e.getMessage(), e);
}
throw new IllegalStateException("Failed to execute count query", e);
}
}
private float[] normalize(float[] vector) {
// Calculate the magnitude of the vector
float magnitude = 0.0f;
for (float value : vector) {
magnitude += value * value;
}
magnitude = (float) Math.sqrt(magnitude);
// Avoid division by zero
if (magnitude == 0.0f) {
return vector;
}
// Normalize the vector
float[] normalized = new float[vector.length];View on GitHub (pinned to 98a7beda4f)
Solutions
- Read the wrapped cause (getCause()) for the concrete Redis error (e.g. 'no such index')
- Ensure the index exists (store initialized / FT.INFO succeeds) before counting
- Verify Redis connectivity and that RediSearch is loaded
- If the filter-based count fails, validate filter fields against the index schema
Defensive patterns
Strategy: try-catch
Validate before calling
// Ensure index exists before counting
try { jedisClient.ftInfo(indexName); } catch (JedisDataException e) { throw new IllegalStateException("Index not created yet: " + indexName); } Try / catch
try { long n = store.count(); } catch (IllegalStateException e) { log.error("Count failed", e.getCause()); /* check index existence / connectivity, retry */ } Prevention
- Initialize the store (index creation) before any count call
- Monitor Redis connectivity with health checks
- Validate count filters against the index schema
- Ensure RediSearch module is loaded and version-compatible
When it happens
Trigger: Executing a count query when the RediSearch index does not exist yet, the Redis connection fails or times out, or the underlying search reply cannot be parsed (module/version mismatch).
Common situations: Calling count before afterPropertiesSet created the index; Redis Stack modules missing; transient network errors to Redis; count query referencing a filter against non-indexed fields.
Understand the failure class
Background: Database query failed: Internal Server Error 500s wrapping SQL, Prisma, and connection failures — what to check first — this error's family across 16 libraries.
Related errors
- Failed to delete documents by filter
- Not allowed filter identifier name:
- Field type {0} not supported
- Expression type {0} not supported for numeric fields
- Numeric value must be a Number
AI-assisted analysis of spring-projects/spring-ai@98a7beda4f (2026-09-11).
Data as JSON: /api/errors/33ab2e29cdeaa110.
Report an issue: GitHub.