Skip to content

Commit 9949c8b

Browse files
authored
Optimization: remove redundant check on each vector operation
Signed-off-by: stroller <[email protected]>
1 parent 133eb40 commit 9949c8b

File tree

1 file changed

+6
-16
lines changed

1 file changed

+6
-16
lines changed

vector-stores/spring-ai-elasticsearch-store/src/main/java/org/springframework/ai/vectorstore/elasticsearch/ElasticsearchVectorStore.java

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -176,11 +176,6 @@ protected ElasticsearchVectorStore(Builder builder) {
176176

177177
@Override
178178
public void doAdd(List<Document> documents) {
179-
// For the index to be present, either it must be pre-created or set the
180-
// initializeSchema to true.
181-
if (!indexExists()) {
182-
throw new IllegalArgumentException("Index not found");
183-
}
184179
BulkRequest.Builder bulkRequestBuilder = new BulkRequest.Builder();
185180

186181
List<float[]> embeddings = this.embeddingModel.embed(documents, EmbeddingOptionsBuilder.builder().build(),
@@ -214,11 +209,6 @@ private Object getDocument(Document document, float[] embedding, String embeddin
214209
@Override
215210
public void doDelete(List<String> idList) {
216211
BulkRequest.Builder bulkRequestBuilder = new BulkRequest.Builder();
217-
// For the index to be present, either it must be pre-created or set the
218-
// initializeSchema to true.
219-
if (!indexExists()) {
220-
throw new IllegalArgumentException("Index not found");
221-
}
222212
for (String id : idList) {
223213
bulkRequestBuilder.operations(op -> op.delete(idx -> idx.index(this.options.getIndexName()).id(id)));
224214
}
@@ -229,12 +219,6 @@ public void doDelete(List<String> idList) {
229219

230220
@Override
231221
public void doDelete(Filter.Expression filterExpression) {
232-
// For the index to be present, either it must be pre-created or set the
233-
// initializeSchema to true.
234-
if (!indexExists()) {
235-
throw new IllegalArgumentException("Index not found");
236-
}
237-
238222
try {
239223
this.elasticsearchClient.deleteByQuery(d -> d.index(this.options.getIndexName())
240224
.query(q -> q.queryString(qs -> qs.query(getElasticsearchQueryString(filterExpression)))));
@@ -349,7 +333,13 @@ private DenseVectorSimilarity parseSimilarity(String similarity) {
349333

350334
@Override
351335
public void afterPropertiesSet() {
336+
// For the index to be present, either it must be pre-created or set the
337+
// initializeSchema to true.
352338
if (!this.initializeSchema) {
339+
boolean exists = indexExists();
340+
if(!exists){
341+
throw new IllegalArgumentException("Index not found");
342+
}
353343
return;
354344
}
355345
if (!indexExists()) {

0 commit comments

Comments
 (0)