UpdateRequest with JSON in JavaAPIClient getting parsing error

I am trying to update the existing record with Java API client but getting the below issue.

index record json

{
  
  "docId": "1",
  "id": 1,
  "name": "aegon",
  "details": {
    "id":1234
  }
}
// update json
   String json1 = "{\"details\": {\n" +
                "    \"ID\":123\n" +
                "  }}";
// api client call

        `client.update(i -> i.index("searchindex").id("348").withJson(new StringReader(json1)),JsonObject.class);`

error

Exception in thread "main" co.elastic.clients.json.JsonpMappingException: Error deserializing co.elastic.clients.elasticsearch.core.UpdateRequest: Unknown field 'details' (JSON path: details) (line no=1, column no=20, offset=19)
	at co.elastic.clients.json.ObjectDeserializer.parseUnknownField(ObjectDeserializer.java:233)
	at co.elastic.clients.json.ObjectDeserializer.deserialize(ObjectDeserializer.java:190)
	at co.elastic.clients.util.WithJsonObjectBuilderBase.withJson(WithJsonObjectBuilderBase.java:54)
	at co.elastic.clients.json.WithJson.withJson(WithJson.java:57)
	at org.example.Main.lambda$main$0(Main.java:39)
	at co.elastic.clients.elasticsearch.ElasticsearchClient.update(ElasticsearchClient.java:2218)
	at org.example.Main.main(Main.java:39)

can you please help me how to write the update request?

Hello Krishna,

When using .withJson() the client expects the same syntax as the Rest API, so in your case the json containing the update should be wrapped with "doc" as explained in the documentation, which results in json1 being:

    String json1 = "{\n" +
        "  \"doc\": {\n" +
        "  \"details\": {\n" +
        "    \"ID\":123\n" +
        "  }\n" +
        "  }\n" +
        "}";

This works, however since the java client has its own syntax I'd suggest switching from the json approach to the java client one, by creating a class for the Record and then using that for updates, for example:

    esClient.update(u -> u
        .index("test-update")
        .id("1")
        .doc(record)
        , Record.class);

with record being the updated object.

1 Like

Hello Laura

Thanks for the reply, i am thinking of implementing generic request, so i can't use dto object class in the request preparation a I have tried withJson u suggested with doc in json

    String json1 = "{\n" +
                "  \"doc\": {\n" +
                "  \"details\": {\n" +
                "    \"ID\":123\n" +
                "  }\n" +
                "  }\n" +
                "}";

      

        client.update(i -> i.index("searchresources").id("348").withJson(new StringReader(json1)),JsonObject.class);

I am getting below error

Exception in thread "main" co.elastic.clients.elasticsearch._types.ElasticsearchException: [es/update] failed: [document_missing_exception] [348]: document missing
	at co.elastic.clients.transport.ElasticsearchTransportBase.getApiResponse(ElasticsearchTransportBase.java:338)
	at co.elastic.clients.transport.ElasticsearchTransportBase.performRequest(ElasticsearchTransportBase.java:140)
	at co.elastic.clients.elasticsearch.ElasticsearchClient.update(ElasticsearchClient.java:2201)
	at co.elastic.clients.elasticsearch.ElasticsearchClient.update(ElasticsearchClient.java:2218)
	at org.example.Main.main(Main.java:49)

I also tried with .doc with and without doc in JSON, getting error as well.

String json1 = "{\n" +
                "  \"doc\": {\n" +
                "  \"details\": {\n" +
                "    \"ID\":123\n" +
                "  }\n" +
                "  }\n" +
                "}";

        JsonObject j = (JsonObject) JsonParser.parseString(json1);
      
        client.update(i -> i.index("searchresources").id("348").doc(j),JsonObject.class);

error 2

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.serialize(JacksonJsonpMapper.java:100)
	at co.elastic.clients.json.JsonpUtils.serialize(JsonpUtils.java:219)
	at co.elastic.clients.elasticsearch.core.UpdateRequest.serializeInternal(UpdateRequest.java:370)
	at co.elastic.clients.elasticsearch.core.UpdateRequest.serialize(UpdateRequest.java:352)
	at co.elastic.clients.json.JsonpMapperBase$JsonpSerializableSerializer.serialize(JsonpMapperBase.java:122)
	at co.elastic.clients.json.JsonpMapperBase$JsonpSerializableSerializer.serialize(JsonpMapperBase.java:119)
	at co.elastic.clients.json.jackson.JacksonJsonpMapper.serialize(JacksonJsonpMapper.java:92)
	at co.elastic.clients.transport.ElasticsearchTransportBase.prepareTransportRequest(ElasticsearchTransportBase.java:258)
	at co.elastic.clients.transport.ElasticsearchTransportBase.performRequest(ElasticsearchTransportBase.java:134)
	at co.elastic.clients.elasticsearch.ElasticsearchClient.update(ElasticsearchClient.java:2201)
	at co.elastic.clients.elasticsearch.ElasticsearchClient.update(ElasticsearchClient.java:2218)
	at org.example.Main.main(Main.java:49)
Caused by: com.fasterxml.jackson.databind.JsonMappingException: JsonObject (through reference chain: com.google.gson.JsonObject["asString"])
	at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:402)
	at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:361)
	at com.fasterxml.jackson.databind.ser.std.StdSerializer.wrapAndThrow(StdSerializer.java:323)
	at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:778)
	at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:183)
	at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider._serialize(DefaultSerializerProvider.java:502)
	at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:341)
	at com.fasterxml.jackson.databind.ObjectMapper.writeValue(ObjectMapper.java:3362)
	at co.elastic.clients.json.jackson.JacksonJsonpMapper.serialize(JacksonJsonpMapper.java:98)
	... 11 more
Caused by: java.lang.UnsupportedOperationException: JsonObject
	at com.google.gson.JsonElement.getAsString(JsonElement.java:179)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:568)
	at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:688)
	at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:770)
	... 16 more

request 3

        client.update(i -> i.index("searchresources").id("348").doc(new StringReader(json1)),JsonObject.class);

error 3

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.serialize(JacksonJsonpMapper.java:100)
	at co.elastic.clients.json.JsonpUtils.serialize(JsonpUtils.java:219)
	at co.elastic.clients.elasticsearch.core.UpdateRequest.serializeInternal(UpdateRequest.java:370)
	at co.elastic.clients.elasticsearch.core.UpdateRequest.serialize(UpdateRequest.java:352)
	at co.elastic.clients.json.JsonpMapperBase$JsonpSerializableSerializer.serialize(JsonpMapperBase.java:122)
	at co.elastic.clients.json.JsonpMapperBase$JsonpSerializableSerializer.serialize(JsonpMapperBase.java:119)
	at co.elastic.clients.json.jackson.JacksonJsonpMapper.serialize(JacksonJsonpMapper.java:92)
	at co.elastic.clients.transport.ElasticsearchTransportBase.prepareTransportRequest(ElasticsearchTransportBase.java:258)
	at co.elastic.clients.transport.ElasticsearchTransportBase.performRequest(ElasticsearchTransportBase.java:134)
	at co.elastic.clients.elasticsearch.ElasticsearchClient.update(ElasticsearchClient.java:2201)
	at co.elastic.clients.elasticsearch.ElasticsearchClient.update(ElasticsearchClient.java:2218)
	at org.example.Main.main(Main.java:49)
Caused by: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class java.io.StringReader and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS)
	at com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:77)
	at com.fasterxml.jackson.databind.SerializerProvider.reportBadDefinition(SerializerProvider.java:1330)
	at com.fasterxml.jackson.databind.DatabindContext.reportBadDefinition(DatabindContext.java:414)
	at com.fasterxml.jackson.databind.ser.impl.UnknownSerializer.failForEmpty(UnknownSerializer.java:53)
	at com.fasterxml.jackson.databind.ser.impl.UnknownSerializer.serialize(UnknownSerializer.java:30)
	at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider._serialize(DefaultSerializerProvider.java:502)
	at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:341)
	at com.fasterxml.jackson.databind.ObjectMapper.writeValue(ObjectMapper.java:3362)
	at co.elastic.clients.json.jackson.JacksonJsonpMapper.serialize(JacksonJsonpMapper.java:98)
	... 11 more

Thanks
Krishna Jalla

Hey again!

The json structure I sent you should be fine, the error [document_missing_exception] [348]: document missing simply means that no document with id 348 exists in index searchresources. The id field in the update request must be the same as it was in the original document, so it was either specified at creation, or autogenerated by elasticsearch.

@ltrotta

thanks it's working fine now with the new id. can i do it without adding doc in json?

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