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

  1. Read the wrapped cause (getCause()) for the concrete Redis error (e.g. 'no such index')
  2. Ensure the index exists (store initialized / FT.INFO succeeds) before counting
  3. Verify Redis connectivity and that RediSearch is loaded
  4. 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

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


AI-assisted analysis of spring-projects/spring-ai@98a7beda4f (2026-09-11). Data as JSON: /api/errors/33ab2e29cdeaa110. Report an issue: GitHub.