Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set ElasticSearch size to match requested topK used in KNN search #2025

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -271,16 +271,15 @@ public List<Document> doSimilaritySearch(SearchRequest searchRequest) {
final float finalThreshold = threshold;
float[] vectors = this.embeddingModel.embed(searchRequest.getQuery());

SearchResponse<Document> res = this.elasticsearchClient.search(
sr -> sr.index(this.options.getIndexName())
.knn(knn -> knn.queryVector(EmbeddingUtils.toList(vectors))
.similarity(finalThreshold)
.k((long) searchRequest.getTopK())
.field("embedding")
.numCandidates((long) (1.5 * searchRequest.getTopK()))
.filter(fl -> fl.queryString(
qs -> qs.query(getElasticsearchQueryString(searchRequest.getFilterExpression()))))),
Document.class);
SearchResponse<Document> res = this.elasticsearchClient.search(sr -> sr.index(this.options.getIndexName())
.knn(knn -> knn.queryVector(EmbeddingUtils.toList(vectors))
.similarity(finalThreshold)
.k((long) searchRequest.getTopK())
.field("embedding")
.numCandidates((long) (1.5 * searchRequest.getTopK()))
.filter(fl -> fl
.queryString(qs -> qs.query(getElasticsearchQueryString(searchRequest.getFilterExpression())))))
.size(searchRequest.getTopK()), Document.class);

return res.hits().hits().stream().map(this::toDocument).collect(Collectors.toList());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.time.ZonedDateTime;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.*;
import java.util.concurrent.TimeUnit;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
Expand Down Expand Up @@ -400,6 +397,45 @@ public void searchThresholdTest(String similarityFunction) {
});
}

@Test
public void overDefaultSizeTest() {

var overDefaultSize = 12;

getContextRunner().run(context -> {

ElasticsearchVectorStore vectorStore = context.getBean("vectorStore_cosine",
ElasticsearchVectorStore.class);

var testDocs = new ArrayList<Document>();
for (int i = 0; i < overDefaultSize; i++) {
testDocs.add(new Document(String.valueOf(i), "Great Depression " + i, Map.of()));
}
vectorStore.add(testDocs);

Awaitility.await()
.until(() -> vectorStore.similaritySearch(
SearchRequest.builder().query("Great Depression").topK(1).similarityThresholdAll().build()),
hasSize(1));

List<Document> results = vectorStore.similaritySearch(SearchRequest.builder()
.query("Great Depression")
.topK(overDefaultSize)
.similarityThresholdAll()
.build());

assertThat(results).hasSize(overDefaultSize);

// Remove all documents from the store
vectorStore.delete(testDocs.stream().map(Document::getId).toList());

Awaitility.await()
.until(() -> vectorStore.similaritySearch(
SearchRequest.builder().query("Great Depression").topK(1).similarityThresholdAll().build()),
hasSize(0));
});
}

@SpringBootConfiguration
@EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class })
public static class TestApplication {
Expand Down
Loading