org.elasticsearch.action.admin.indices.mapping.get.GetFieldMappingsResponse Java Examples

The following examples show how to use org.elasticsearch.action.admin.indices.mapping.get.GetFieldMappingsResponse. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: UpgradeUtil.java    From fess with Apache License 2.0 6 votes vote down vote up
public static boolean addFieldMapping(final IndicesAdminClient indicesClient, final String index, final String type,
        final String field, final String source) {
    final GetFieldMappingsResponse gfmResponse =
            indicesClient.prepareGetFieldMappings(index).addTypes(type).setFields(field).execute().actionGet();
    final FieldMappingMetadata fieldMappings = gfmResponse.fieldMappings(index, type, field);
    if (fieldMappings == null || fieldMappings.isNull()) {
        try {
            final AcknowledgedResponse pmResponse =
                    indicesClient.preparePutMapping(index).setSource(source, XContentType.JSON).execute().actionGet();
            if (!pmResponse.isAcknowledged()) {
                logger.warn("Failed to add {} to {}/{}", field, index, type);
            } else {
                return true;
            }
        } catch (final Exception e) {
            logger.warn("Failed to add " + field + " to " + index + "/" + type, e);
        }
    }
    return false;
}
 
Example #2
Source File: RestGetFieldMappingAction.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public void handleRequest(final RestRequest request, final RestChannel channel, final Client client) {
    final String[] indices = Strings.splitStringByCommaToArray(request.param("index"));
    final String[] types = request.paramAsStringArrayOrEmptyIfAll("type");
    final String[] fields = Strings.splitStringByCommaToArray(request.param("fields"));
    GetFieldMappingsRequest getMappingsRequest = new GetFieldMappingsRequest();
    getMappingsRequest.indices(indices).types(types).fields(fields).includeDefaults(request.paramAsBoolean("include_defaults", false));
    getMappingsRequest.indicesOptions(IndicesOptions.fromRequest(request, getMappingsRequest.indicesOptions()));
    getMappingsRequest.local(request.paramAsBoolean("local", getMappingsRequest.local()));
    client.admin().indices().getFieldMappings(getMappingsRequest, new RestBuilderListener<GetFieldMappingsResponse>(channel) {

        @SuppressWarnings("unchecked")
        @Override
        public RestResponse buildResponse(GetFieldMappingsResponse response, XContentBuilder builder) throws Exception {
            ImmutableMap<String, ImmutableMap<String, ImmutableMap<String, FieldMappingMetaData>>> mappingsByIndex = response.mappings();

            boolean isPossibleSingleFieldRequest = indices.length == 1 && types.length == 1 && fields.length == 1;
            if (isPossibleSingleFieldRequest && isFieldMappingMissingField(mappingsByIndex)) {
                return new BytesRestResponse(OK, builder.startObject().endObject());
            }

            RestStatus status = OK;
            if (mappingsByIndex.isEmpty() && fields.length > 0) {
                status = NOT_FOUND;
            }
            builder.startObject();
            response.toXContent(builder, request);
            builder.endObject();
            return new BytesRestResponse(status, builder);
        }
    });
}
 
Example #3
Source File: ESIndex.java    From pyramid with Apache License 2.0 5 votes vote down vote up
public String getFieldType(String field) {
    GetFieldMappingsResponse response = this.client.admin().indices().
            prepareGetFieldMappings(this.indexName).setTypes(this.documentType).
            setFields(field).execute().actionGet();
    Map map = (Map)response.mappings().get(this.indexName).get(this.documentType).
            get(field).sourceAsMap().get(field);
    return map.get("type").toString();
}