Search in sources :

Example 1 with GetFieldMappingsResponse

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

the class SimpleGetFieldMappingsIT method testSimpleGetFieldMappings.

public void testSimpleGetFieldMappings() throws Exception {
    assertAcked(prepareCreate("indexa").addMapping("typeA", getMappingForType("typeA")).addMapping("typeB", getMappingForType("typeB")));
    assertAcked(client().admin().indices().prepareCreate("indexb").addMapping("typeA", getMappingForType("typeA")).addMapping("typeB", getMappingForType("typeB")));
    // Get mappings by full name
    GetFieldMappingsResponse response = client().admin().indices().prepareGetFieldMappings("indexa").setTypes("typeA").setFields("field1", "obj.subfield").get();
    assertThat(response.fieldMappings("indexa", "typeA", "field1").fullName(), equalTo("field1"));
    assertThat(response.fieldMappings("indexa", "typeA", "field1").sourceAsMap(), hasKey("field1"));
    assertThat(response.fieldMappings("indexa", "typeA", "obj.subfield").fullName(), equalTo("obj.subfield"));
    assertThat(response.fieldMappings("indexa", "typeA", "obj.subfield").sourceAsMap(), hasKey("subfield"));
    assertThat(response.mappings().get("indexa"), not(hasKey("typeB")));
    assertThat(response.fieldMappings("indexa", "typeB", "field1"), nullValue());
    assertThat(response.mappings(), not(hasKey("indexb")));
    assertThat(response.fieldMappings("indexb", "typeB", "field1"), nullValue());
    // Get mappings by name
    response = client().admin().indices().prepareGetFieldMappings("indexa").setTypes("typeA").setFields("field1", "obj.subfield").get();
    assertThat(response.fieldMappings("indexa", "typeA", "field1").fullName(), equalTo("field1"));
    assertThat(response.fieldMappings("indexa", "typeA", "field1").sourceAsMap(), hasKey("field1"));
    assertThat(response.fieldMappings("indexa", "typeA", "obj.subfield").fullName(), equalTo("obj.subfield"));
    assertThat(response.fieldMappings("indexa", "typeA", "obj.subfield").sourceAsMap(), hasKey("subfield"));
    assertThat(response.fieldMappings("indexa", "typeB", "field1"), nullValue());
    assertThat(response.fieldMappings("indexb", "typeB", "field1"), nullValue());
    // get mappings by name across multiple indices
    response = client().admin().indices().prepareGetFieldMappings().setTypes("typeA").setFields("obj.subfield").get();
    assertThat(response.fieldMappings("indexa", "typeA", "obj.subfield").fullName(), equalTo("obj.subfield"));
    assertThat(response.fieldMappings("indexa", "typeA", "obj.subfield").sourceAsMap(), hasKey("subfield"));
    assertThat(response.fieldMappings("indexa", "typeB", "obj.subfield"), nullValue());
    assertThat(response.fieldMappings("indexb", "typeA", "obj.subfield").fullName(), equalTo("obj.subfield"));
    assertThat(response.fieldMappings("indexb", "typeA", "obj.subfield").sourceAsMap(), hasKey("subfield"));
    assertThat(response.fieldMappings("indexb", "typeB", "obj.subfield"), nullValue());
    // get mappings by name across multiple types
    response = client().admin().indices().prepareGetFieldMappings("indexa").setFields("obj.subfield").get();
    assertThat(response.fieldMappings("indexa", "typeA", "obj.subfield").fullName(), equalTo("obj.subfield"));
    assertThat(response.fieldMappings("indexa", "typeA", "obj.subfield").sourceAsMap(), hasKey("subfield"));
    assertThat(response.fieldMappings("indexa", "typeA", "field1"), nullValue());
    assertThat(response.fieldMappings("indexa", "typeB", "obj.subfield").fullName(), equalTo("obj.subfield"));
    assertThat(response.fieldMappings("indexa", "typeB", "obj.subfield").sourceAsMap(), hasKey("subfield"));
    assertThat(response.fieldMappings("indexa", "typeB", "field1"), nullValue());
    assertThat(response.fieldMappings("indexb", "typeA", "obj.subfield"), nullValue());
    assertThat(response.fieldMappings("indexb", "typeA", "field1"), nullValue());
    assertThat(response.fieldMappings("indexb", "typeB", "obj.subfield"), nullValue());
    assertThat(response.fieldMappings("indexb", "typeB", "field1"), nullValue());
    // get mappings by name across multiple types & indices
    response = client().admin().indices().prepareGetFieldMappings().setFields("obj.subfield").get();
    assertThat(response.fieldMappings("indexa", "typeA", "obj.subfield").fullName(), equalTo("obj.subfield"));
    assertThat(response.fieldMappings("indexa", "typeA", "obj.subfield").sourceAsMap(), hasKey("subfield"));
    assertThat(response.fieldMappings("indexa", "typeA", "field1"), nullValue());
    assertThat(response.fieldMappings("indexa", "typeB", "obj.subfield").fullName(), equalTo("obj.subfield"));
    assertThat(response.fieldMappings("indexa", "typeB", "obj.subfield").sourceAsMap(), hasKey("subfield"));
    assertThat(response.fieldMappings("indexa", "typeB", "field1"), nullValue());
    assertThat(response.fieldMappings("indexb", "typeA", "obj.subfield").fullName(), equalTo("obj.subfield"));
    assertThat(response.fieldMappings("indexb", "typeA", "obj.subfield").sourceAsMap(), hasKey("subfield"));
    assertThat(response.fieldMappings("indexb", "typeB", "field1"), nullValue());
    assertThat(response.fieldMappings("indexb", "typeB", "obj.subfield").fullName(), equalTo("obj.subfield"));
    assertThat(response.fieldMappings("indexb", "typeB", "obj.subfield").sourceAsMap(), hasKey("subfield"));
    assertThat(response.fieldMappings("indexb", "typeB", "field1"), nullValue());
}
Also used : GetFieldMappingsResponse(org.elasticsearch.action.admin.indices.mapping.get.GetFieldMappingsResponse)

Example 2 with GetFieldMappingsResponse

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

the class SimpleGetFieldMappingsIT method testGetFieldMappingsWithBlocks.

public void testGetFieldMappingsWithBlocks() throws Exception {
    assertAcked(prepareCreate("test").addMapping("typeA", getMappingForType("typeA")).addMapping("typeB", getMappingForType("typeB")));
    for (String block : Arrays.asList(SETTING_BLOCKS_READ, SETTING_BLOCKS_WRITE, SETTING_READ_ONLY)) {
        try {
            enableIndexBlock("test", block);
            GetFieldMappingsResponse response = client().admin().indices().prepareGetFieldMappings("test").setTypes("typeA").setFields("field1", "obj.subfield").get();
            assertThat(response.fieldMappings("test", "typeA", "field1").fullName(), equalTo("field1"));
        } finally {
            disableIndexBlock("test", block);
        }
    }
    try {
        enableIndexBlock("test", SETTING_BLOCKS_METADATA);
        assertBlocked(client().admin().indices().prepareGetMappings(), INDEX_METADATA_BLOCK);
    } finally {
        disableIndexBlock("test", SETTING_BLOCKS_METADATA);
    }
}
Also used : GetFieldMappingsResponse(org.elasticsearch.action.admin.indices.mapping.get.GetFieldMappingsResponse)

Example 3 with GetFieldMappingsResponse

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

the class SimpleGetFieldMappingsIT method testSimpleGetFieldMappingsWithDefaults.

@SuppressWarnings("unchecked")
public void testSimpleGetFieldMappingsWithDefaults() throws Exception {
    assertAcked(prepareCreate("test").addMapping("type", getMappingForType("type")));
    client().prepareIndex("test", "type", "1").setSource("num", 1).get();
    GetFieldMappingsResponse response = client().admin().indices().prepareGetFieldMappings().setFields("num", "field1", "obj.subfield").includeDefaults(true).get();
    assertThat((Map<String, Object>) response.fieldMappings("test", "type", "num").sourceAsMap().get("num"), hasEntry("index", Boolean.TRUE));
    assertThat((Map<String, Object>) response.fieldMappings("test", "type", "num").sourceAsMap().get("num"), hasEntry("type", (Object) "long"));
    assertThat((Map<String, Object>) response.fieldMappings("test", "type", "field1").sourceAsMap().get("field1"), hasEntry("index", Boolean.TRUE));
    assertThat((Map<String, Object>) response.fieldMappings("test", "type", "field1").sourceAsMap().get("field1"), hasEntry("type", (Object) "text"));
    assertThat((Map<String, Object>) response.fieldMappings("test", "type", "obj.subfield").sourceAsMap().get("subfield"), hasEntry("type", (Object) "keyword"));
}
Also used : GetFieldMappingsResponse(org.elasticsearch.action.admin.indices.mapping.get.GetFieldMappingsResponse)

Example 4 with GetFieldMappingsResponse

use of org.elasticsearch.action.admin.indices.mapping.get.GetFieldMappingsResponse 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 5 with GetFieldMappingsResponse

use of org.elasticsearch.action.admin.indices.mapping.get.GetFieldMappingsResponse 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

GetFieldMappingsResponse (org.elasticsearch.action.admin.indices.mapping.get.GetFieldMappingsResponse)8 HashMap (java.util.HashMap)2 Map (java.util.Map)2 FieldMappingMetaData (org.elasticsearch.action.admin.indices.mapping.get.GetFieldMappingsResponse.FieldMappingMetaData)2 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)2 IOException (java.io.IOException)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 Settings (org.elasticsearch.common.settings.Settings)1 ToXContent (org.elasticsearch.common.xcontent.ToXContent)1 BaseRestHandler (org.elasticsearch.rest.BaseRestHandler)1 BytesRestResponse (org.elasticsearch.rest.BytesRestResponse)1 RestController (org.elasticsearch.rest.RestController)1 RestRequest (org.elasticsearch.rest.RestRequest)1 GET (org.elasticsearch.rest.RestRequest.Method.GET)1 RestResponse (org.elasticsearch.rest.RestResponse)1 RestStatus (org.elasticsearch.rest.RestStatus)1