{"record":{"id":"33ab2e29cdeaa110","repo":"spring-projects/spring-ai","slug":"failed-to-execute-count-query","errorCode":null,"errorMessage":"Failed to execute count query","messagePattern":"Failed to execute count query","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"vector-stores/spring-ai-redis-store/src/main/java/org/springframework/ai/vectorstore/redis/RedisVectorStore.java","lineNumber":1213,"sourceCode":"\t * @param filterExpression the Redis filter expression string\n\t * @return the count of matching documents\n\t */\n\tprivate long executeCountQuery(String filterExpression) {\n\t\t// Create a query with the filter, limiting to 0 results to only get count\n\t\tQuery query = new Query(filterExpression).returnFields(\"id\") // Minimal field to\n\t\t\t// return\n\t\t\t.limit(0, 0) // No actual results, just count\n\t\t\t.dialect(2); // Use dialect 2 for advanced query features\n\n\t\ttry {\n\t\t\tSearchResult result = this.jedisClient.ftSearch(this.indexName, query);\n\t\t\treturn result.getTotalResults();\n\t\t}\n\t\tcatch (Exception e) {\n\t\t\tif (logger.isErrorEnabled()) {\n\t\t\t\tlogger.error(\"Error executing count query: \" + e.getMessage(), e);\n\t\t\t}\n\t\t\tthrow new IllegalStateException(\"Failed to execute count query\", e);\n\t\t}\n\t}\n\n\tprivate float[] normalize(float[] vector) {\n\t\t// Calculate the magnitude of the vector\n\t\tfloat magnitude = 0.0f;\n\t\tfor (float value : vector) {\n\t\t\tmagnitude += value * value;\n\t\t}\n\t\tmagnitude = (float) Math.sqrt(magnitude);\n\n\t\t// Avoid division by zero\n\t\tif (magnitude == 0.0f) {\n\t\t\treturn vector;\n\t\t}\n\n\t\t// Normalize the vector\n\t\tfloat[] normalized = new float[vector.length];","sourceCodeStart":1195,"sourceCodeEnd":1231,"githubUrl":"https://github.com/spring-projects/spring-ai/blob/98a7beda4f29d80a71c5837eb4053b03a93a46f7/vector-stores/spring-ai-redis-store/src/main/java/org/springframework/ai/vectorstore/redis/RedisVectorStore.java#L1195-L1231","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","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"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"// Ensure index exists before counting\ntry { jedisClient.ftInfo(indexName); } catch (JedisDataException e) { throw new IllegalStateException(\"Index not created yet: \" + indexName); }","typeGuard":null,"tryCatchPattern":"try { long n = store.count(); } catch (IllegalStateException e) { log.error(\"Count failed\", e.getCause()); /* check index existence / connectivity, retry */ }","preventionTips":["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"],"tags":["redis","vector-store","count","query-failure"],"backgroundTag":"database-query-failed","analyzedSha":"98a7beda4f29d80a71c5837eb4053b03a93a46f7","analyzedAt":"2026-09-11T14:15:49.441Z","contentChangedAt":"2026-09-11T14:15:49.441Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}