Search in sources :

Example 1 with GetMappingsRequest

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

the class IndicesRequestIT method testGetMappings.

public void testGetMappings() {
    interceptTransportActions(GetMappingsAction.NAME);
    GetMappingsRequest getMappingsRequest = new GetMappingsRequest().indices(randomIndicesOrAliases());
    internalCluster().coordOnlyNodeClient().admin().indices().getMappings(getMappingsRequest).actionGet();
    clearInterceptedActions();
    assertSameIndices(getMappingsRequest, GetMappingsAction.NAME);
}
Also used : GetMappingsRequest(org.elasticsearch.action.admin.indices.mapping.get.GetMappingsRequest)

Example 2 with GetMappingsRequest

use of org.elasticsearch.action.admin.indices.mapping.get.GetMappingsRequest in project incubator-sdap-mudrod by apache.

the class ESDriver method getTypeListWithPrefix.

public List<String> getTypeListWithPrefix(Object object, Object object2) {
    ArrayList<String> typeList = new ArrayList<>();
    GetMappingsResponse res;
    try {
        res = getClient().admin().indices().getMappings(new GetMappingsRequest().indices(object.toString())).get();
        ImmutableOpenMap<String, MappingMetaData> mapping = res.mappings().get(object.toString());
        for (ObjectObjectCursor<String, MappingMetaData> c : mapping) {
            if (c.key.startsWith(object2.toString())) {
                typeList.add(c.key);
            }
        }
    } catch (InterruptedException | ExecutionException e) {
        LOG.error("Error whilst obtaining type list from Elasticsearch mappings.", e);
    }
    return typeList;
}
Also used : MappingMetaData(org.elasticsearch.cluster.metadata.MappingMetaData) ExecutionException(java.util.concurrent.ExecutionException) GetMappingsRequest(org.elasticsearch.action.admin.indices.mapping.get.GetMappingsRequest) GetMappingsResponse(org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse)

Example 3 with GetMappingsRequest

use of org.elasticsearch.action.admin.indices.mapping.get.GetMappingsRequest in project calcite by apache.

the class Elasticsearch2Schema method getTableMap.

@Override
protected Map<String, Table> getTableMap() {
    final ImmutableMap.Builder<String, Table> builder = ImmutableMap.builder();
    try {
        GetMappingsResponse response = client.admin().indices().getMappings(new GetMappingsRequest().indices(index)).get();
        ImmutableOpenMap<String, MappingMetaData> mapping = response.getMappings().get(index);
        for (ObjectObjectCursor<String, MappingMetaData> c : mapping) {
            builder.put(c.key, new Elasticsearch2Table(client, index, c.key));
        }
    } catch (RuntimeException e) {
        throw e;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return builder.build();
}
Also used : Table(org.apache.calcite.schema.Table) MappingMetaData(org.elasticsearch.cluster.metadata.MappingMetaData) GetMappingsRequest(org.elasticsearch.action.admin.indices.mapping.get.GetMappingsRequest) ImmutableMap(com.google.common.collect.ImmutableMap) GetMappingsResponse(org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse)

Example 4 with GetMappingsRequest

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

the class RestGetMappingAction 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");
    GetMappingsRequest getMappingsRequest = new GetMappingsRequest();
    getMappingsRequest.indices(indices).types(types);
    getMappingsRequest.indicesOptions(IndicesOptions.fromRequest(request, getMappingsRequest.indicesOptions()));
    getMappingsRequest.local(request.paramAsBoolean("local", getMappingsRequest.local()));
    return channel -> client.admin().indices().getMappings(getMappingsRequest, new RestBuilderListener<GetMappingsResponse>(channel) {

        @Override
        public RestResponse buildResponse(GetMappingsResponse response, XContentBuilder builder) throws Exception {
            ImmutableOpenMap<String, ImmutableOpenMap<String, MappingMetaData>> mappingsByIndex = response.getMappings();
            if (mappingsByIndex.isEmpty()) {
                if (indices.length != 0 && types.length != 0) {
                    return new BytesRestResponse(OK, builder.startObject().endObject());
                } else if (indices.length != 0) {
                    builder.close();
                    return new BytesRestResponse(channel, new IndexNotFoundException(indices[0]));
                } else if (types.length != 0) {
                    builder.close();
                    return new BytesRestResponse(channel, new TypeMissingException("_all", types[0]));
                } else {
                    return new BytesRestResponse(OK, builder.startObject().endObject());
                }
            }
            builder.startObject();
            for (ObjectObjectCursor<String, ImmutableOpenMap<String, MappingMetaData>> indexEntry : mappingsByIndex) {
                if (indexEntry.value.isEmpty()) {
                    continue;
                }
                builder.startObject(indexEntry.key);
                builder.startObject(Fields.MAPPINGS);
                for (ObjectObjectCursor<String, MappingMetaData> typeEntry : indexEntry.value) {
                    builder.field(typeEntry.key);
                    builder.map(typeEntry.value.sourceAsMap());
                }
                builder.endObject();
                builder.endObject();
            }
            builder.endObject();
            return new BytesRestResponse(OK, builder);
        }
    });
}
Also used : BaseRestHandler(org.elasticsearch.rest.BaseRestHandler) GET(org.elasticsearch.rest.RestRequest.Method.GET) ImmutableOpenMap(org.elasticsearch.common.collect.ImmutableOpenMap) MappingMetaData(org.elasticsearch.cluster.metadata.MappingMetaData) RestResponse(org.elasticsearch.rest.RestResponse) GetMappingsResponse(org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse) GetMappingsRequest(org.elasticsearch.action.admin.indices.mapping.get.GetMappingsRequest) RestBuilderListener(org.elasticsearch.rest.action.RestBuilderListener) IOException(java.io.IOException) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) RestController(org.elasticsearch.rest.RestController) Strings(org.elasticsearch.common.Strings) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) ObjectObjectCursor(com.carrotsearch.hppc.cursors.ObjectObjectCursor) Settings(org.elasticsearch.common.settings.Settings) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) IndicesOptions(org.elasticsearch.action.support.IndicesOptions) RestRequest(org.elasticsearch.rest.RestRequest) OK(org.elasticsearch.rest.RestStatus.OK) NodeClient(org.elasticsearch.client.node.NodeClient) TypeMissingException(org.elasticsearch.indices.TypeMissingException) RestResponse(org.elasticsearch.rest.RestResponse) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) TypeMissingException(org.elasticsearch.indices.TypeMissingException) ImmutableOpenMap(org.elasticsearch.common.collect.ImmutableOpenMap) MappingMetaData(org.elasticsearch.cluster.metadata.MappingMetaData) GetMappingsRequest(org.elasticsearch.action.admin.indices.mapping.get.GetMappingsRequest) IOException(java.io.IOException) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) TypeMissingException(org.elasticsearch.indices.TypeMissingException) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) ObjectObjectCursor(com.carrotsearch.hppc.cursors.ObjectObjectCursor) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) GetMappingsResponse(org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse)

Example 5 with GetMappingsRequest

use of org.elasticsearch.action.admin.indices.mapping.get.GetMappingsRequest in project canal by alibaba.

the class ESConnection method getMapping.

public MappingMetaData getMapping(String index, String type) {
    MappingMetaData mappingMetaData = null;
    if (mode == ESClientMode.TRANSPORT) {
        ImmutableOpenMap<String, MappingMetaData> mappings;
        try {
            mappings = transportClient.admin().cluster().prepareState().execute().actionGet().getState().getMetaData().getIndices().get(index).getMappings();
        } catch (NullPointerException e) {
            throw new IllegalArgumentException("Not found the mapping info of index: " + index);
        }
        mappingMetaData = mappings.get(type);
    } else {
        ImmutableOpenMap<String, ImmutableOpenMap<String, MappingMetaData>> mappings;
        try {
            GetMappingsRequest request = new GetMappingsRequest();
            request.indices(index);
            GetMappingsResponse response;
            // try {
            // response = restHighLevelClient
            // .indices()
            // .getMapping(request, RequestOptions.DEFAULT);
            // // 6.4以下版本直接使用该接口会报错
            // } catch (Exception e) {
            // logger.warn("Low ElasticSearch version for getMapping");
            response = RestHighLevelClientExt.getMapping(restHighLevelClient, request, RequestOptions.DEFAULT);
            // }
            mappings = response.mappings();
        } catch (NullPointerException e) {
            throw new IllegalArgumentException("Not found the mapping info of index: " + index);
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
            return null;
        }
        mappingMetaData = mappings.get(index).get(type);
    }
    return mappingMetaData;
}
Also used : IOException(java.io.IOException) MappingMetaData(org.elasticsearch.cluster.metadata.MappingMetaData) ImmutableOpenMap(org.elasticsearch.common.collect.ImmutableOpenMap) GetMappingsRequest(org.elasticsearch.action.admin.indices.mapping.get.GetMappingsRequest) GetMappingsResponse(org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse)

Aggregations

GetMappingsRequest (org.elasticsearch.action.admin.indices.mapping.get.GetMappingsRequest)7 GetMappingsResponse (org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse)5 MappingMetaData (org.elasticsearch.cluster.metadata.MappingMetaData)5 ImmutableMap (com.google.common.collect.ImmutableMap)2 IOException (java.io.IOException)2 Table (org.apache.calcite.schema.Table)2 ImmutableOpenMap (org.elasticsearch.common.collect.ImmutableOpenMap)2 ObjectObjectCursor (com.carrotsearch.hppc.cursors.ObjectObjectCursor)1 ArrayList (java.util.ArrayList)1 ExecutionException (java.util.concurrent.ExecutionException)1 DocWriteRequest (org.elasticsearch.action.DocWriteRequest)1 ClusterSearchShardsRequest (org.elasticsearch.action.admin.cluster.shards.ClusterSearchShardsRequest)1 IndicesAliasesRequest (org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest)1 GetAliasesRequest (org.elasticsearch.action.admin.indices.alias.get.GetAliasesRequest)1 ClearIndicesCacheRequest (org.elasticsearch.action.admin.indices.cache.clear.ClearIndicesCacheRequest)1 CloseIndexRequest (org.elasticsearch.action.admin.indices.close.CloseIndexRequest)1 CreateIndexRequest (org.elasticsearch.action.admin.indices.create.CreateIndexRequest)1 DeleteIndexRequest (org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest)1 IndicesExistsRequest (org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequest)1 TypesExistsRequest (org.elasticsearch.action.admin.indices.exists.types.TypesExistsRequest)1