spring-projects / spring-projects/spring-ai

RedisVectorStore.similaritySearch() fails on AWS ElastiCache Valkey 8.x due to unsupported FT.SEARCH SORTBY clause

Open Beginner friendly
#5,709 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

status: waiting-for-triage
Dominant language
Java
Stars
9.5k
Forks
2.9k
Avg merge
1d 7h
Merged PRs (30d)
6

Description

Bug description
RedisVectorStore.doSimilaritySearch() unconditionally appends .setSortBy(DISTANCE_FIELD_NAME, true) to the FT.SEARCH query, which translates to FT.SEARCH ... SORTBY vector_score ASC.

AWS ElastiCache Valkey 8.x does not support the SORTBY clause in FT.SEARCH, causing the query to fail at runtime. Since KNN vector search already returns results ordered by distance natively, the explicit SORTBY is redundant and prevents compatibility with Valkey-based deployments.

This was already resolved in commit c160b9c on the current main/2.0 development line, which removes the .setSortBy() call. Could this fix be backported to a 1.1.x patch release? It appears to be a single-line behavioral change with no API impact. Upgrading to the 2.0 line is not feasible for applications currently on Spring Boot 3.x.

Environment

  • Spring AI version: 1.1.3
  • Java version: 21
  • Spring Boot: 3.5.6
  • Vector store: RedisVectorStore with AWS ElastiCache Valkey 8.x (Serverless)
  • Jedis version: 6.0.0

Steps to reproduce

  1. Configure RedisVectorStore with an AWS ElastiCache Valkey 8.x endpoint
  2. Index/schema initialization completes successfully
  3. Store documents via vectorStore.add() — works correctly
  4. Call vectorStore.similaritySearch(SearchRequest.builder().query("...").topK(5).build())
  5. Search fails with JedisDataException

Expected behavior
similaritySearch() should work with AWS ElastiCache Valkey, since KNN queries inherently return results sorted by distance — the explicit SORTBY is not necessary.

Minimal Complete Reproducible example
The problematic code in RedisVectorStore.java:361:

Query query = new Query(queryString)                                                                                                                                 
    .addParam(EMBEDDING_PARAM_NAME, RediSearchUtil.toByteArray(embedding))
    .returnFields(returnFields.toArray(new String[0]))
    .setSortBy(DISTANCE_FIELD_NAME, true)   // ← This line causes the failure                                                                                                                                                                                                                                             
    .limit(0, request.getTopK())
    .dialect(2);                                                                                                                                                                                                                                                                                                          
                                                                                                                                                                     
Exception thrown:                                                                                                                                                                                                                                                                                                         
 
redis.clients.jedis.exceptions.JedisDataException: Unexpected argument `SORTBY`                                                                                                                                                                                                                                           
    at redis.clients.jedis.Protocol.processError(Protocol.java:110)                                                                                                  
    at redis.clients.jedis.Protocol.process(Protocol.java:158)                                                                                                                                                                                                                                                            
    at redis.clients.jedis.Protocol.read(Protocol.java:221)
    at redis.clients.jedis.Connection.executeCommand(Connection.java:175)                                                                                                                                                                                                                                                 
    at redis.clients.jedis.UnifiedJedis.ftSearch(UnifiedJedis.java:3974)                                                                                                                                                                                                                                                  
    at org.springframework.ai.vectorstore.redis.RedisVectorStore.doSimilaritySearch(RedisVectorStore.java:361)
    at org.springframework.ai.vectorstore.observation.AbstractObservationVectorStore.lambda$similaritySearch$7(AbstractObservationVectorStore.java:140)                                                                                                                                                                   
                                                                                                                                                                                                                                                                                                                          
The fix in commit https://github.com/spring-projects/spring-ai/commit/c160b9c53f simply removes the redundant .setSortBy() call:                                                                                                                                                                                          
                                                                                                                                                                                                                                                                                                                          
Query query = new Query(queryString)                                                                                                                                                                                                                                                                                     
     .addParam(EMBEDDING_PARAM_NAME, RediSearchUtil.toByteArray(embedding))                                                                                          
     .returnFields(returnFields.toArray(new String[0]))                                                                                                                                                                                                                                                                   
-    .setSortBy(DISTANCE_FIELD_NAME, true)
     .limit(0, request.getTopK())                                                                                                                                                                                                                                                                                         
     .dialect(2);                                                                                                                                                    
                                                                                                                                                                                                                                                                                                                          
Workaround: bypass RedisVectorStore.similaritySearch() and execute KNN query directly via Jedis ftSearch() without the SORTBY clause.                                                                                                                                                                                     
                                                                                                                                                                                                                                                                                                                          
Related issues: #5215, #1846

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in vector-stores/spring-ai-redis-store/src/main/java/org/springframework/ai/vectorstore/redis/RedisVectorStore.java around line 361 and compare the 1.1.x code with commit c160b9c. Backport the removal of the redundant SORTBY clause, then verify that similaritySearch works against AWS ElastiCache Valkey 8.x using the reproduction steps and no longer raises the reported JedisDataException.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, redis
Domain
backend, databases
Issue type
Bug
Difficulty
1/5
Estimated time
Under an hour
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.