Skip to content

Conversation

@yezhoujie
Copy link

Bug Fix

Correct Parameter Type for KnnSearch$Builder.k Method in ElasticsearchVectorStore.java

Description

This PR addresses a bug in ElasticsearchVectorStore.java where an incorrect parameter type was passed to the KnnSearch$Builder.k method of the ElasticSearchClient during the execution of similaritySearch. The issue caused the following error:

Handler dispatch failed: java.lang.NoSuchMethodError: 'co.elastic.clients.elasticsearch._types.KnnSearch$Builder co.elastic.clients.elasticsearch._types.KnnSearch$Builder.k(java.lang.Long)'

Root Cause

The KnnSearch$Builder.k method in elasticsearch-rest-client:8.15.4 requires an Integer parameter, but the code passed a long value, leading to the NoSuchMethodError.

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);

Fix

The fix involves changing the parameter type from long to Integer when calling the KnnSearch$Builder.k method.

Changes

Updated ElasticsearchVectorStore.java to pass an Integer parameter to the KnnSearch$Builder.k method.

…sticsearchVectorStore.java

Addresses a bug in ElasticsearchVectorStore.java where an incorrect parameter type was passed to the KnnSearch$Builder.k method of the ElasticSearchClient during the execution of similaritySearch. The issue caused the following error:

```
Handler dispatch failed: java.lang.NoSuchMethodError: 'co.elastic.clients.elasticsearch._types.KnnSearch$Builder co.elastic.clients.elasticsearch._types.KnnSearch$Builder.k(java.lang.Long)'
```

Root Cause
The KnnSearch$Builder.k method requires an Integer parameter, but the code passed a long value, leading to the NoSuchMethodError.

## Fix

The fix involves changing the parameter type from long to Integer when calling the KnnSearch$Builder.k method.
@markpollack markpollack added this to the 1.0.0-M5 milestone Dec 20, 2024
@sobychacko sobychacko modified the milestones: 1.0.0-M5, 1.0.0-M6 Dec 20, 2024
@ilayaperumalg ilayaperumalg self-assigned this Feb 12, 2025
@ilayaperumalg
Copy link
Member

Hi @yezhoujie,

Prior to this update to use Spring Boot 3.4.2 change, Spring AI used elasticsearch-rest-client 8.13.3 which expected the KnnSearch$Builder.k to have long type as the parameter. But, with Spring Boot 3.4.2, it is 8.15.5 which should work fine for you now.

Also, the KnnSearch$Builder.numCandidates requires parameter type Integer and hence, the change was added here to convert the double value to int. I would be curious to find out how this logic can be improved, perhaps, we could use Math.round() before casting to int. Please share your feedback.

Closing this PR without merging as the underlying issue is already fixed with Spring Boot 3.4.2 update. Thank you for your contribution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants