From 9949c8b4e4808a7013d0e16efa54f740c05e9be6 Mon Sep 17 00:00:00 2001 From: stroller Date: Fri, 10 Oct 2025 17:28:14 +0800 Subject: [PATCH 1/2] Optimization: remove redundant check on each vector operation Signed-off-by: stroller --- .../ElasticsearchVectorStore.java | 22 +++++-------------- 1 file changed, 6 insertions(+), 16 deletions(-) diff --git a/vector-stores/spring-ai-elasticsearch-store/src/main/java/org/springframework/ai/vectorstore/elasticsearch/ElasticsearchVectorStore.java b/vector-stores/spring-ai-elasticsearch-store/src/main/java/org/springframework/ai/vectorstore/elasticsearch/ElasticsearchVectorStore.java index 6f2c6e6c768..f0262a6a4d0 100644 --- a/vector-stores/spring-ai-elasticsearch-store/src/main/java/org/springframework/ai/vectorstore/elasticsearch/ElasticsearchVectorStore.java +++ b/vector-stores/spring-ai-elasticsearch-store/src/main/java/org/springframework/ai/vectorstore/elasticsearch/ElasticsearchVectorStore.java @@ -176,11 +176,6 @@ protected ElasticsearchVectorStore(Builder builder) { @Override public void doAdd(List documents) { - // For the index to be present, either it must be pre-created or set the - // initializeSchema to true. - if (!indexExists()) { - throw new IllegalArgumentException("Index not found"); - } BulkRequest.Builder bulkRequestBuilder = new BulkRequest.Builder(); List embeddings = this.embeddingModel.embed(documents, EmbeddingOptionsBuilder.builder().build(), @@ -214,11 +209,6 @@ private Object getDocument(Document document, float[] embedding, String embeddin @Override public void doDelete(List idList) { BulkRequest.Builder bulkRequestBuilder = new BulkRequest.Builder(); - // For the index to be present, either it must be pre-created or set the - // initializeSchema to true. - if (!indexExists()) { - throw new IllegalArgumentException("Index not found"); - } for (String id : idList) { bulkRequestBuilder.operations(op -> op.delete(idx -> idx.index(this.options.getIndexName()).id(id))); } @@ -229,12 +219,6 @@ public void doDelete(List idList) { @Override public void doDelete(Filter.Expression filterExpression) { - // For the index to be present, either it must be pre-created or set the - // initializeSchema to true. - if (!indexExists()) { - throw new IllegalArgumentException("Index not found"); - } - try { this.elasticsearchClient.deleteByQuery(d -> d.index(this.options.getIndexName()) .query(q -> q.queryString(qs -> qs.query(getElasticsearchQueryString(filterExpression))))); @@ -349,7 +333,13 @@ private DenseVectorSimilarity parseSimilarity(String similarity) { @Override public void afterPropertiesSet() { + // For the index to be present, either it must be pre-created or set the + // initializeSchema to true. if (!this.initializeSchema) { + boolean exists = indexExists(); + if(!exists){ + throw new IllegalArgumentException("Index not found"); + } return; } if (!indexExists()) { From 3d73a51d71750b67a8fa2306f9a011894be969bc Mon Sep 17 00:00:00 2001 From: stroller Date: Fri, 10 Oct 2025 21:22:50 +0800 Subject: [PATCH 2/2] Tiny refactor for code cleanup Signed-off-by: stroller --- .../elasticsearch/ElasticsearchVectorStore.java | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/vector-stores/spring-ai-elasticsearch-store/src/main/java/org/springframework/ai/vectorstore/elasticsearch/ElasticsearchVectorStore.java b/vector-stores/spring-ai-elasticsearch-store/src/main/java/org/springframework/ai/vectorstore/elasticsearch/ElasticsearchVectorStore.java index f0262a6a4d0..a7fff198bbf 100644 --- a/vector-stores/spring-ai-elasticsearch-store/src/main/java/org/springframework/ai/vectorstore/elasticsearch/ElasticsearchVectorStore.java +++ b/vector-stores/spring-ai-elasticsearch-store/src/main/java/org/springframework/ai/vectorstore/elasticsearch/ElasticsearchVectorStore.java @@ -335,16 +335,13 @@ private DenseVectorSimilarity parseSimilarity(String similarity) { public void afterPropertiesSet() { // For the index to be present, either it must be pre-created or set the // initializeSchema to true. - if (!this.initializeSchema) { - boolean exists = indexExists(); - if(!exists){ - throw new IllegalArgumentException("Index not found"); - } + if (indexExists()) { return; } - if (!indexExists()) { - createIndexMapping(); + if (!this.initializeSchema) { + throw new IllegalArgumentException("Index not found"); } + createIndexMapping(); } @Override