Get Field Mappings API

edit

Get Field Mappings Request

edit

A GetFieldMappingsRequest can have an optional list of indices, optional list of types and the list of fields:

GetFieldMappingsRequest request = new GetFieldMappingsRequest(); 
request.indices("twitter"); 
request.types("_doc"); 
request.fields("message", "timestamp"); 

An empty request

Setting the indices to fetch mapping for

The types to be returned

The fields to be returned

Optional arguments

edit

The following arguments can also optionally be provided:

request.indicesOptions(IndicesOptions.lenientExpandOpen()); 

Setting IndicesOptions controls how unavailable indices are resolved and how wildcard expressions are expanded

request.local(true); 

The local flag (defaults to false) controls whether the aliases need to be looked up in the local cluster state or in the cluster state held by the elected master node

Synchronous Execution

edit
GetFieldMappingsResponse response =
    client.indices().getFieldMapping(request, RequestOptions.DEFAULT);

Asynchronous Execution

edit

The asynchronous execution of a get mappings request requires both the GetFieldMappingsRequest instance and an ActionListener instance to be passed to the asynchronous method:

client.indices().getFieldMappingAsync(request, RequestOptions.DEFAULT, listener); 

The GetFieldMappingsRequest to execute and the ActionListener to use when the execution completes

The asynchronous method does not block and returns immediately. Once it is completed the ActionListener is called back using the onResponse method if the execution successfully completed or using the onFailure method if it failed.

A typical listener for GetMappingsResponse looks like:

ActionListener<GetFieldMappingsResponse> listener =
    new ActionListener<GetFieldMappingsResponse>() {
        @Override
        public void onResponse(GetFieldMappingsResponse putMappingResponse) {
            
        }

        @Override
        public void onFailure(Exception e) {
            
        }
    };

Called when the execution is successfully completed. The response is provided as an argument

Called in case of failure. The raised exception is provided as an argument

Get Field Mappings Response

edit

The returned GetFieldMappingsResponse allows to retrieve information about the executed operation as follows:

final Map<String, Map<String, Map<String, GetFieldMappingsResponse.FieldMappingMetaData>>> mappings =
    response.mappings();
final Map<String, GetFieldMappingsResponse.FieldMappingMetaData> typeMappings =
    mappings.get("twitter").get("_doc"); 
final GetFieldMappingsResponse.FieldMappingMetaData metaData =
    typeMappings.get("message");

final String fullName = metaData.fullName();
final Map<String, Object> source = metaData.sourceAsMap(); 

Returning all requested indices fields' mappings

Retrieving the mappings for a particular index and type

Getting the mappings metadata for the message field

Getting the full name of the field

Getting the mapping source of the field