spring-projects/spring-ai · error · RuntimeException
Search failed!
Error message
Search failed!
What it means
doSimilaritySearch checks the R<SearchResults> response from the Milvus client and throws a plain RuntimeException("Search failed!") when respSearch.getException() is non-null. The Milvus server rejected or failed the similarity search RPC; the original exception is the cause.
Solutions
- Read the cause exception to get the exact Milvus status/error.
- Confirm the query embedding dimension matches the collection's embedding field dimension.
- Load the collection (milvusClient.loadCollection) before searching.
- Check the search params JSON (e.g. nprobe) is valid for the configured index type.
- Verify Milvus server connectivity and collection existence.
Example fix
// before
SearchRequest request = SearchRequest.builder().query("...").topK(10000).build();
// after
SearchRequest request = SearchRequest.builder().query("...").topK(100).build(); // keep topK within server limits Defensive patterns
Strategy: retry
Validate before calling
// verify collection is loaded and dimensions match before searching String dims = embeddingModel.dimensions(); // must equal the collection's embedding field dimension // also confirm collection exists: milvusClient.hasCollection(HasCollectionParam.newBuilder().withCollectionName(collectionName).build());
Try / catch
try {
List<Document> docs = vectorStore.similaritySearch(SearchRequest.builder().query(q).topK(5).build());
} catch (RuntimeException e) {
logger.error("Milvus search failed: {}", e.getCause() != null ? e.getCause().getMessage() : e.getMessage(), e);
// optionally retry once after verifying collection load state
} Prevention
- Always load the collection before searching after server restarts.
- Match embedding model dimension to the collection index dimension.
- Keep topK and search params within server limits for your index type.
When it happens
Trigger: Calling similaritySearch(SearchRequest) when the Milvus search RPC returns an error status: collection not loaded, invalid search params (nprobe/ef), wrong embedding dimension, or connectivity failure.
Common situations: Query embedding dimension differs from the collection's index dimension; topK too large; search params JSON invalid; Milvus collection dropped or not loaded after restart.
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
- Deleted only entries from requested
- Failed to delete documents by filter
- Failed to delete documents by filter:
- Failed to insert:
- Not supported expression type
AI-assisted analysis of spring-projects/spring-ai@98a7beda4f (2026-09-11).
Data as JSON: /api/errors/986c89b7455247cf.
Report an issue: GitHub.
Appendix: source
Thrown at vector-stores/spring-ai-milvus-store/src/main/java/org/springframework/ai/vectorstore/milvus/MilvusVectorStore.java:405
.withFloatVectors(List.of(EmbeddingUtils.toList(embedding)))
.withVectorFieldName(this.embeddingFieldName);
if (StringUtils.hasText(nativeFilterExpressions)) {
searchParamBuilder.withExpr(nativeFilterExpressions);
}
if (StringUtils.hasText(this.partitionName)) {
searchParamBuilder.addPartitionName(this.partitionName);
}
if (StringUtils.hasText(searchParamsJson)) {
searchParamBuilder.withParams(searchParamsJson);
}
R<SearchResults> respSearch = this.milvusClient.search(searchParamBuilder.build());
if (respSearch.getException() != null) {
throw new RuntimeException("Search failed!", respSearch.getException());
}
SearchResultsWrapper wrapperSearch = new SearchResultsWrapper(respSearch.getData().getResults());
return wrapperSearch.getRowRecords(0)
.stream()
.filter(rowRecord -> getResultSimilarity(rowRecord) >= request.getSimilarityThreshold())
.map(rowRecord -> {
String docId = String.valueOf(rowRecord.get(this.idFieldName));
String content = (String) rowRecord.get(this.contentFieldName);
JsonObject metadata = new JsonObject();
try {
metadata = (JsonObject) rowRecord.get(this.metadataFieldName);
if (metadata != null) {
// inject the distance into the metadata.
metadata.addProperty(DocumentMetadata.DISTANCE.value(), 1 - getResultSimilarity(rowRecord));
}
}View on GitHub (pinned to 98a7beda4f)