Get Field Mapping request failing with UnexpectedJsonEventException using the Java API Client

Hello,

during the process of migrating our code base from the Java High Level REST Client to the Java API Client, we are hitting an issue when trying to retrieve fields mappings.

When running the following code (with an Elasticsearch 7.17.3 server running, and using version 7.16.3 of the Java API Client):

RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200)).build();
ObjectMapper om = new ObjectMapper();
JsonpMapper mapper = new JacksonJsonpMapper(om);
ElasticsearchTransport transport = new RestClientTransport(restClient, mapper);
ElasticsearchClient client = new ElasticsearchClient(transport);
GetFieldMappingRequest gfmRequest =  new GetFieldMappingRequest.Builder()
		.index("*")
		.fields("*")
		.build();
GetFieldMappingResponse gfmResponse = client.indices().getFieldMapping(gfmRequest);

the following exception is thrown:

Exception in thread "main" co.elastic.clients.json.UnexpectedJsonEventException: Unexpected JSON event 'KEY_NAME' instead of 'END_OBJECT'
	at co.elastic.clients.json.JsonpUtils.expectEvent(JsonpUtils.java:57)
	at co.elastic.clients.json.JsonpUtils.expectNextEvent(JsonpUtils.java:51)
	at co.elastic.clients.json.ObjectDeserializer.deserialize(ObjectDeserializer.java:197)
	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.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.DictionaryResponse.lambda$setupDictionaryResponseDeserializer$0(DictionaryResponse.java:148)
	at co.elastic.clients.json.ObjectDeserializer.parseUnknownField(ObjectDeserializer.java:205)
	at co.elastic.clients.json.ObjectDeserializer.deserialize(ObjectDeserializer.java:174)
	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.rest_client.RestClientTransport.decodeResponse(RestClientTransport.java:325)
	at co.elastic.clients.transport.rest_client.RestClientTransport.getHighLevelResponse(RestClientTransport.java:291)
	at co.elastic.clients.transport.rest_client.RestClientTransport.performRequest(RestClientTransport.java:144)
	at co.elastic.clients.elasticsearch.indices.ElasticsearchIndicesClient.getFieldMapping(ElasticsearchIndicesClient.java:1062)

When calling the REST API directly (GET http://localhost:9200//_mapping/field/), it returns valid JSON with an HTTP 200 status.

Is it an issue with the Java API Client or an issue with our code ? How can we fix this ?

Thanks for your help.

An update on this: when providing an actual field name to the Get Mapping Resquest rather than a wildcard in order to get all the fields, there is no exception.

When providing two fields names, there is the exception... So it looks like the client API is expecting a single field mapping in the response, whatever the request.

So the question might be: how to proceed with the Java API Client to request mapping for all fields ?

Hey,

I suppose you are hitting getFieldMapping error when several fields · Issue #166 · elastic/elasticsearch-java · GitHub - feel free to add a comment there or subscribe to the issue to get notified when it is fixed.

As a workaround until this is fixed, you could go with the Low Level Rest Client and execute that request using that one.

Hope that helps!

Thank you Alexander, that is definitely helping. I will look into using the Low Level Rest Client, as getFieldMapping error when several fields · Issue #166 · elastic/elasticsearch-java · GitHub doesn't look as if it's going to be solved anytime soon.

I guess that the issue comes from elasticsearch-specification/types.ts at 4cbdef1d11586b7d6b27d3744dd15a6f0ce96afb · elastic/elasticsearch-specification · GitHub and the definition of a "single key dictionary" for the mappings ?

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