{"record":{"id":"8de18d5888d73cdb","repo":"spring-projects/spring-ai","slug":"failed-to-delete-documents-by-filter-8de18d","errorCode":null,"errorMessage":"Failed to delete documents by filter","messagePattern":"Failed to delete documents by filter","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"vector-stores/spring-ai-opensearch-store/src/main/java/org/springframework/ai/vectorstore/opensearch/OpenSearchVectorStore.java","lineNumber":300,"sourceCode":"\t\t\t// Create delete by query request\n\t\t\tDeleteByQueryRequest request = new DeleteByQueryRequest.Builder().index(this.index)\n\t\t\t\t.query(q -> q.queryString(qs -> qs.query(filterStr)))\n\t\t\t\t.build();\n\n\t\t\tDeleteByQueryResponse response = this.openSearchClient.deleteByQuery(request);\n\t\t\tif (logger.isDebugEnabled()) {\n\t\t\t\tlogger.debug(\"Deleted \" + response.deleted() + \" documents matching filter expression\");\n\t\t\t}\n\n\t\t\tif (!response.failures().isEmpty()) {\n\t\t\t\tthrow new IllegalStateException(\"Failed to delete some documents: \" + response.failures());\n\t\t\t}\n\t\t}\n\t\tcatch (Exception e) {\n\t\t\tif (logger.isErrorEnabled()) {\n\t\t\t\tlogger.error(\"Failed to delete documents by filter: \" + e.getMessage());\n\t\t\t}\n\t\t\tthrow new IllegalStateException(\"Failed to delete documents by filter\", e);\n\t\t}\n\t}\n\n\t@Override\n\tpublic List<Document> doSimilaritySearch(SearchRequest searchRequest) {\n\t\tAssert.notNull(searchRequest, \"The search request must not be null.\");\n\t\treturn similaritySearch(this.embeddingModel.embed(searchRequest.getQuery()), searchRequest.getTopK(),\n\t\t\t\tsearchRequest.getSimilarityThreshold(), searchRequest.getFilterExpression());\n\t}\n\n\tpublic List<Document> similaritySearch(float[] embedding, int topK, double similarityThreshold,\n\t\t\tFilter.@Nullable Expression filterExpression) {\n\t\treturn similaritySearch(\n\t\t\t\tthis.useApproximateKnn ? buildApproximateQuery(embedding, topK, similarityThreshold, filterExpression)\n\t\t\t\t\t\t: buildExactQuery(embedding, topK, similarityThreshold, filterExpression));\n\t}\n\n\tprivate org.opensearch.client.opensearch.core.SearchRequest buildApproximateQuery(float[] embedding, int topK,","sourceCodeStart":282,"sourceCodeEnd":318,"githubUrl":"https://github.com/spring-projects/spring-ai/blob/98a7beda4f29d80a71c5837eb4053b03a93a46f7/vector-stores/spring-ai-opensearch-store/src/main/java/org/springframework/ai/vectorstore/opensearch/OpenSearchVectorStore.java#L282-L318","documentation":"OpenSearchVectorStore.doDelete catches any exception thrown while performing the filter-based delete (building the query, executing delete_by_query) and rethrows it as IllegalStateException('Failed to delete documents by filter') with the original as cause. It is a wrapper ensuring a uniform exception type for filter deletes.","triggerScenarios":"Calling vectorStore.delete(Filter.Expression) or delete(String) when the OpenSearch client throws — connectivity loss, malformed filter converting to invalid query DSL, index missing, or authentication errors.","commonSituations":"OpenSearch endpoint down or wrong host/port in config; invalid filter syntax that the converter renders into broken query DSL; security plugin rejecting the request; index deleted between existence check and query.","solutions":["Inspect the cause exception and the logged 'Failed to delete documents by filter' message for the root error.","Verify OpenSearch connection settings (url, credentials) and cluster availability.","Simplify/validate the filter expression and confirm the metadata field names exist in the index mapping.","Retry after transient network errors; wrap calls in retry logic with backoff."],"exampleFix":"// before\nvectorStore.delete(\"counrty == 'IN'\"); // typo yields broken query DSL\n// after\nvectorStore.delete(\"country == 'IN'\"); // valid field, delete succeeds","handlingStrategy":"try-catch","validationCode":"// validate connectivity and index existence first\nboolean exists = client.indices().exists(i -> i.index(index)).value();\nboolean reachable = pingClient();","typeGuard":null,"tryCatchPattern":"try {\n    vectorStore.delete(filterExpression);\n} catch (IllegalStateException e) {\n    Throwable cause = e.getCause();\n    logger.error(\"Filter delete failed: {}\", cause == null ? e.getMessage() : cause.getMessage(), cause);\n}","preventionTips":["Verify OpenSearch host/credentials in configuration at startup","Validate filter expression field names against index mappings","Always inspect getCause() — this is a wrapper exception","Add retry with backoff for transient client errors"],"tags":["opensearch","vector-store","delete","filter"],"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-14T11:17:12.474Z"}