How to integrate knnsearch In java application

Hi Folks ,
I was tried knnsearch for vector similarity search in python it worked .But Now i need to integrate that with my spring application , I don't see much resource on it. But i tried with below code

Elastic Search : 8.0.1
 RestClient restClient = RestClient.builder(
                new HttpHost("localhost", 9200)).build();
        ElasticsearchTransport transport = new RestClientTransport(
                restClient, new JacksonJsonpMapper());
        ElasticsearchClient client = new ElasticsearchClient(transport);
 KnnSearchQuery query = new KnnSearchQuery.Builder().field("image-vector").k(1).numCandidates(100).queryVector(v).build();
        KnnSearchRequest request = new KnnSearchRequest.Builder().knn(query).index("image_similarity_1million").fields("humanid").build();
   client.knnSearch(request,RequestOptions.DEFAULT);

But i am getting exceptions

Exception in thread "main" jakarta.json.JsonException: Jackson exception
	at co.elastic.clients.json.jackson.JacksonUtils.convertException(JacksonUtils.java:39)
	at co.elastic.clients.json.jackson.JacksonJsonpMapper$JacksonValueParser.deserialize(JacksonJsonpMapper.java:113)
	at co.elastic.clients.json.JsonpDeserializer.deserialize(JsonpDeserializer.java:75)
	at co.elastic.clients.json.JsonpMapperBase.deserialize(JsonpMapperBase.java:41)
	at co.elastic.clients.json.AttributedJsonpMapper.deserialize(AttributedJsonpMapper.java:45)
	at co.elastic.clients.json.JsonpDeserializer$1.deserialize(JsonpDeserializer.java:98)
	at co.elastic.clients.json.NamedDeserializer.deserialize(NamedDeserializer.java:64)
	at co.elastic.clients.json.ObjectDeserializer$FieldObjectDeserializer.deserialize(ObjectDeserializer.java:72)
	at co.elastic.clients.json.ObjectDeserializer.deserialize(ObjectDeserializer.java:176)
	at co.elastic.clients.json.ObjectDeserializer.deserialize(ObjectDeserializer.java:137)
	at co.elastic.clients.json.ObjectBuilderDeserializer.deserialize(ObjectBuilderDeserializer.java:85)
	at co.elastic.clients.json.JsonpDeserializerBase$ArrayDeserializer.deserialize(JsonpDeserializerBase.java:320)
	at co.elastic.clients.json.JsonpDeserializerBase$ArrayDeserializer.deserialize(JsonpDeserializerBase.java:285)
	at co.elastic.clients.json.JsonpDeserializer.deserialize(JsonpDeserializer.java:75)
	at co.elastic.clients.json.ObjectDeserializer$FieldObjectDeserializer.deserialize(ObjectDeserializer.java:72)
	at co.elastic.clients.json.ObjectDeserializer.deserialize(ObjectDeserializer.java:176)
	at co.elastic.clients.json.ObjectDeserializer.deserialize(ObjectDeserializer.java:137)
	at co.elastic.clients.json.JsonpDeserializer.deserialize(JsonpDeserializer.java:75)
	at co.elastic.clients.json.ObjectBuilderDeserializer.deserialize(ObjectBuilderDeserializer.java:79)
	at co.elastic.clients.json.ObjectDeserializer$FieldObjectDeserializer.deserialize(ObjectDeserializer.java:72)
	at co.elastic.clients.json.ObjectDeserializer.deserialize(ObjectDeserializer.java:176)
	at co.elastic.clients.json.ObjectDeserializer.deserialize(ObjectDeserializer.java:137)
	at co.elastic.clients.json.JsonpDeserializer.deserialize(JsonpDeserializer.java:75)
	at co.elastic.clients.json.ObjectBuilderDeserializer.deserialize(ObjectBuilderDeserializer.java:79)
	at co.elastic.clients.json.DelegatingDeserializer$SameType.deserialize(DelegatingDeserializer.java:43)
	at co.elastic.clients.transport.endpoints.EndpointWithResponseMapperAttr$1.deserialize(EndpointWithResponseMapperAttr.java:56)
	at co.elastic.clients.transport.rest_client.RestClientTransport.decodeResponse(RestClientTransport.java:328)
	at co.elastic.clients.transport.rest_client.RestClientTransport.getHighLevelResponse(RestClientTransport.java:294)
	at co.elastic.clients.transport.rest_client.RestClientTransport.performRequest(RestClientTransport.java:147)
	at co.elastic.clients.elasticsearch.ElasticsearchClient.knnSearch(ElasticsearchClient.java:1010)
	at Elastic.main(Elastic.java:37)
Caused by: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `HUMAN` (no Creators, like default constructor, exist): cannot deserialize from Object value (no delegate- or property-based Creator)
 at [Source: (org.apache.http.nio.entity.ContentInputStream); line: 1, column: 250]
	at com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:67)
	at com.fasterxml.jackson.databind.DeserializationContext.reportBadDefinition(DeserializationContext.java:1764)
	at com.fasterxml.jackson.databind.DatabindContext.reportBadDefinition(DatabindContext.java:400)
	at com.fasterxml.jackson.databind.DeserializationContext.handleMissingInstantiator(DeserializationContext.java:1209)
	at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:1400)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:362)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:195)
	at com.fasterxml.jackson.databind.deser.DefaultDeserializationContext.readRootValue(DefaultDeserializationContext.java:322)
	at com.fasterxml.jackson.databind.ObjectMapper._readValue(ObjectMapper.java:4569)
	at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2798)
	at co.elastic.clients.json.jackson.JacksonJsonpMapper$JacksonValueParser.deserialize(JacksonJsonpMapper.java:111)
	... 29 more

please help me out.

Hi @dileepkotte4529 , great that you are trying out the kNN search API. I am not a Spring expert, so apologies if this is not helpful, but it looks like one of your classes called HUMAN is missing some methods required to construct it:

Caused by: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `HUMAN` (no Creators, like default constructor, exist): cannot deserialize from Object value (no delegate- or property-based Creator)

I don't see any Elasticsearch-specific issues in the stack trace.

Julie

1 Like

This line is a High Level Rest Client call: client.knnSearch(request,RequestOptions.DEFAULT);

I guess your actual code is more like client.knnSearch(request, SomeDocumentClass.class); where SomeApplicationClass is the type to use to deserialize documents returned by the search request.

The error indicates that Jackson is unable to create an instance of HUMAN. What does the definition of your SomeDocumentClass look like?

1 Like

Yeah ! Actually I fixed that day itself . Thank you for response @swallez @Julie_Tibshirani

1 Like

Awesome! Thanks for the update @dileepkotte4529!

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.