Search in sources :

Example 1 with FieldMappingMetaData

use of org.elasticsearch.action.admin.indices.mapping.get.GetFieldMappingsResponse.FieldMappingMetaData in project elasticsearch by elastic.

the class TransportGetFieldMappingsIndexAction method findFieldMappingsByType.

private Map<String, FieldMappingMetaData> findFieldMappingsByType(DocumentMapper documentMapper, GetFieldMappingsIndexRequest request) {
    MapBuilder<String, FieldMappingMetaData> fieldMappings = new MapBuilder<>();
    final DocumentFieldMappers allFieldMappers = documentMapper.mappers();
    for (String field : request.fields()) {
        if (Regex.isMatchAllPattern(field)) {
            for (FieldMapper fieldMapper : allFieldMappers) {
                addFieldMapper(fieldMapper.fieldType().name(), fieldMapper, fieldMappings, request.includeDefaults());
            }
        } else if (Regex.isSimpleMatchPattern(field)) {
            for (FieldMapper fieldMapper : allFieldMappers) {
                if (Regex.simpleMatch(field, fieldMapper.fieldType().name())) {
                    addFieldMapper(fieldMapper.fieldType().name(), fieldMapper, fieldMappings, request.includeDefaults());
                }
            }
        } else {
            // not a pattern
            FieldMapper fieldMapper = allFieldMappers.smartNameFieldMapper(field);
            if (fieldMapper != null) {
                addFieldMapper(field, fieldMapper, fieldMappings, request.includeDefaults());
            } else if (request.probablySingleFieldRequest()) {
                fieldMappings.put(field, FieldMappingMetaData.NULL);
            }
        }
    }
    return fieldMappings.immutableMap();
}
Also used : FieldMappingMetaData(org.elasticsearch.action.admin.indices.mapping.get.GetFieldMappingsResponse.FieldMappingMetaData) DocumentFieldMappers(org.elasticsearch.index.mapper.DocumentFieldMappers) MapBuilder(org.elasticsearch.common.collect.MapBuilder) FieldMapper(org.elasticsearch.index.mapper.FieldMapper)

Example 2 with FieldMappingMetaData

use of org.elasticsearch.action.admin.indices.mapping.get.GetFieldMappingsResponse.FieldMappingMetaData in project elasticsearch by elastic.

the class RestGetFieldMappingAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    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()));
    return channel -> client.admin().indices().getFieldMappings(getMappingsRequest, new RestBuilderListener<GetFieldMappingsResponse>(channel) {

        @Override
        public RestResponse buildResponse(GetFieldMappingsResponse response, XContentBuilder builder) throws Exception {
            Map<String, Map<String, Map<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);
        }
    });
}
Also used : BaseRestHandler(org.elasticsearch.rest.BaseRestHandler) FieldMappingMetaData(org.elasticsearch.action.admin.indices.mapping.get.GetFieldMappingsResponse.FieldMappingMetaData) GET(org.elasticsearch.rest.RestRequest.Method.GET) RestResponse(org.elasticsearch.rest.RestResponse) RestBuilderListener(org.elasticsearch.rest.action.RestBuilderListener) GetFieldMappingsResponse(org.elasticsearch.action.admin.indices.mapping.get.GetFieldMappingsResponse) IOException(java.io.IOException) NOT_FOUND(org.elasticsearch.rest.RestStatus.NOT_FOUND) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) RestController(org.elasticsearch.rest.RestController) Strings(org.elasticsearch.common.Strings) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) GetFieldMappingsRequest(org.elasticsearch.action.admin.indices.mapping.get.GetFieldMappingsRequest) Settings(org.elasticsearch.common.settings.Settings) RestStatus(org.elasticsearch.rest.RestStatus) Map(java.util.Map) IndicesOptions(org.elasticsearch.action.support.IndicesOptions) RestRequest(org.elasticsearch.rest.RestRequest) OK(org.elasticsearch.rest.RestStatus.OK) NodeClient(org.elasticsearch.client.node.NodeClient) RestResponse(org.elasticsearch.rest.RestResponse) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) GetFieldMappingsRequest(org.elasticsearch.action.admin.indices.mapping.get.GetFieldMappingsRequest) GetFieldMappingsResponse(org.elasticsearch.action.admin.indices.mapping.get.GetFieldMappingsResponse) IOException(java.io.IOException) RestStatus(org.elasticsearch.rest.RestStatus) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) Map(java.util.Map) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 3 with FieldMappingMetaData

use of org.elasticsearch.action.admin.indices.mapping.get.GetFieldMappingsResponse.FieldMappingMetaData in project elasticsearch by elastic.

the class TransportGetFieldMappingsIndexAction method addFieldMapper.

private void addFieldMapper(String field, FieldMapper fieldMapper, MapBuilder<String, FieldMappingMetaData> fieldMappings, boolean includeDefaults) {
    if (fieldMappings.containsKey(field)) {
        return;
    }
    try {
        XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON);
        builder.startObject();
        fieldMapper.toXContent(builder, includeDefaults ? includeDefaultsParams : ToXContent.EMPTY_PARAMS);
        builder.endObject();
        fieldMappings.put(field, new FieldMappingMetaData(fieldMapper.fieldType().name(), builder.bytes()));
    } catch (IOException e) {
        throw new ElasticsearchException("failed to serialize XContent of field [" + field + "]", e);
    }
}
Also used : FieldMappingMetaData(org.elasticsearch.action.admin.indices.mapping.get.GetFieldMappingsResponse.FieldMappingMetaData) IOException(java.io.IOException) ElasticsearchException(org.elasticsearch.ElasticsearchException) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 4 with FieldMappingMetaData

use of org.elasticsearch.action.admin.indices.mapping.get.GetFieldMappingsResponse.FieldMappingMetaData in project elasticsearch by elastic.

the class GetFieldMappingsResponseTests method testSerialization.

public void testSerialization() throws IOException {
    Map<String, Map<String, Map<String, FieldMappingMetaData>>> mappings = new HashMap<>();
    FieldMappingMetaData fieldMappingMetaData = new FieldMappingMetaData("my field", new BytesArray("{}"));
    mappings.put("index", Collections.singletonMap("type", Collections.singletonMap("field", fieldMappingMetaData)));
    GetFieldMappingsResponse response = new GetFieldMappingsResponse(mappings);
    try (BytesStreamOutput out = new BytesStreamOutput()) {
        response.writeTo(out);
        GetFieldMappingsResponse serialized = new GetFieldMappingsResponse();
        try (StreamInput in = StreamInput.wrap(out.bytes().toBytesRef().bytes)) {
            serialized.readFrom(in);
            FieldMappingMetaData metaData = serialized.fieldMappings("index", "type", "field");
            assertNotNull(metaData);
            assertEquals(new BytesArray("{}"), metaData.getSource());
        }
    }
}
Also used : BytesArray(org.elasticsearch.common.bytes.BytesArray) HashMap(java.util.HashMap) FieldMappingMetaData(org.elasticsearch.action.admin.indices.mapping.get.GetFieldMappingsResponse.FieldMappingMetaData) StreamInput(org.elasticsearch.common.io.stream.StreamInput) Map(java.util.Map) HashMap(java.util.HashMap) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput)

Example 5 with FieldMappingMetaData

use of org.elasticsearch.action.admin.indices.mapping.get.GetFieldMappingsResponse.FieldMappingMetaData in project fess by codelibs.

the class AdminUpgradeAction method addFieldMapping.

private void 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 PutMappingResponse pmResponse = indicesClient.preparePutMapping(index).setType(type).setSource(source).execute().actionGet();
            if (!pmResponse.isAcknowledged()) {
                logger.warn("Failed to add " + field + " to " + index + "/" + type);
            }
        } catch (final Exception e) {
            logger.warn("Failed to add " + field + " to " + index + "/" + type, e);
        }
    }
}
Also used : FieldMappingMetaData(org.elasticsearch.action.admin.indices.mapping.get.GetFieldMappingsResponse.FieldMappingMetaData) PutMappingResponse(org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse) GetFieldMappingsResponse(org.elasticsearch.action.admin.indices.mapping.get.GetFieldMappingsResponse)

Aggregations

FieldMappingMetaData (org.elasticsearch.action.admin.indices.mapping.get.GetFieldMappingsResponse.FieldMappingMetaData)6 Map (java.util.Map)3 IOException (java.io.IOException)2 GetFieldMappingsResponse (org.elasticsearch.action.admin.indices.mapping.get.GetFieldMappingsResponse)2 MapBuilder (org.elasticsearch.common.collect.MapBuilder)2 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)2 ArrayList (java.util.ArrayList)1 Collections.singletonMap (java.util.Collections.singletonMap)1 HashMap (java.util.HashMap)1 ElasticsearchException (org.elasticsearch.ElasticsearchException)1 GetFieldMappingsRequest (org.elasticsearch.action.admin.indices.mapping.get.GetFieldMappingsRequest)1 PutMappingResponse (org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse)1 IndicesOptions (org.elasticsearch.action.support.IndicesOptions)1 NodeClient (org.elasticsearch.client.node.NodeClient)1 Strings (org.elasticsearch.common.Strings)1 BytesArray (org.elasticsearch.common.bytes.BytesArray)1 BytesStreamOutput (org.elasticsearch.common.io.stream.BytesStreamOutput)1 StreamInput (org.elasticsearch.common.io.stream.StreamInput)1 Settings (org.elasticsearch.common.settings.Settings)1 IndexService (org.elasticsearch.index.IndexService)1