Search in sources :

Example 56 with IndexNotFoundException

use of org.elasticsearch.index.IndexNotFoundException 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 57 with IndexNotFoundException

use of org.elasticsearch.index.IndexNotFoundException in project elasticsearch by elastic.

the class TransportPutMappingAction method masterOperation.

@Override
protected void masterOperation(final PutMappingRequest request, final ClusterState state, final ActionListener<PutMappingResponse> listener) {
    try {
        final Index[] concreteIndices = request.getConcreteIndex() == null ? indexNameExpressionResolver.concreteIndices(state, request) : new Index[] { request.getConcreteIndex() };
        PutMappingClusterStateUpdateRequest updateRequest = new PutMappingClusterStateUpdateRequest().ackTimeout(request.timeout()).masterNodeTimeout(request.masterNodeTimeout()).indices(concreteIndices).type(request.type()).updateAllTypes(request.updateAllTypes()).source(request.source());
        metaDataMappingService.putMapping(updateRequest, new ActionListener<ClusterStateUpdateResponse>() {

            @Override
            public void onResponse(ClusterStateUpdateResponse response) {
                listener.onResponse(new PutMappingResponse(response.isAcknowledged()));
            }

            @Override
            public void onFailure(Exception t) {
                logger.debug((Supplier<?>) () -> new ParameterizedMessage("failed to put mappings on indices [{}], type [{}]", concreteIndices, request.type()), t);
                listener.onFailure(t);
            }
        });
    } catch (IndexNotFoundException ex) {
        logger.debug((Supplier<?>) () -> new ParameterizedMessage("failed to put mappings on indices [{}], type [{}]", request.indices(), request.type()), ex);
        throw ex;
    }
}
Also used : IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) Index(org.elasticsearch.index.Index) Supplier(org.apache.logging.log4j.util.Supplier) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) ClusterStateUpdateResponse(org.elasticsearch.cluster.ack.ClusterStateUpdateResponse) ClusterBlockException(org.elasticsearch.cluster.block.ClusterBlockException) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException)

Example 58 with IndexNotFoundException

use of org.elasticsearch.index.IndexNotFoundException in project elasticsearch by elastic.

the class AutoCreateIndexTests method testAutoCreationDisabled.

public void testAutoCreationDisabled() {
    Settings settings = Settings.builder().put(AutoCreateIndex.AUTO_CREATE_INDEX_SETTING.getKey(), false).build();
    AutoCreateIndex autoCreateIndex = newAutoCreateIndex(settings);
    IndexNotFoundException e = expectThrows(IndexNotFoundException.class, () -> autoCreateIndex.shouldAutoCreate(randomAsciiOfLengthBetween(1, 10), buildClusterState()));
    assertEquals("no such index and [action.auto_create_index] is [false]", e.getMessage());
}
Also used : IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) ClusterSettings(org.elasticsearch.common.settings.ClusterSettings) Settings(org.elasticsearch.common.settings.Settings)

Example 59 with IndexNotFoundException

use of org.elasticsearch.index.IndexNotFoundException in project elasticsearch by elastic.

the class AutoCreateIndexTests method expectNotMatch.

private void expectNotMatch(ClusterState clusterState, AutoCreateIndex autoCreateIndex, String index) {
    IndexNotFoundException e = expectThrows(IndexNotFoundException.class, () -> autoCreateIndex.shouldAutoCreate(index, clusterState));
    assertEquals("no such index and [action.auto_create_index] ([" + autoCreateIndex.getAutoCreate() + "]) doesn't match", e.getMessage());
}
Also used : IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException)

Example 60 with IndexNotFoundException

use of org.elasticsearch.index.IndexNotFoundException in project elasticsearch by elastic.

the class AutoCreateIndexTests method expectForbidden.

private void expectForbidden(ClusterState clusterState, AutoCreateIndex autoCreateIndex, String index, String forbiddingPattern) {
    IndexNotFoundException e = expectThrows(IndexNotFoundException.class, () -> autoCreateIndex.shouldAutoCreate(index, clusterState));
    assertEquals("no such index and [action.auto_create_index] contains [" + forbiddingPattern + "] which forbids automatic creation of the index", e.getMessage());
}
Also used : IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException)

Aggregations

IndexNotFoundException (org.elasticsearch.index.IndexNotFoundException)92 ClusterState (org.elasticsearch.cluster.ClusterState)22 ShardNotFoundException (org.elasticsearch.index.shard.ShardNotFoundException)21 ShardId (org.elasticsearch.index.shard.ShardId)19 ShardRouting (org.elasticsearch.cluster.routing.ShardRouting)16 Index (org.elasticsearch.index.Index)16 Map (java.util.Map)15 ArrayList (java.util.ArrayList)14 IOException (java.io.IOException)13 IndexMetadata (org.elasticsearch.cluster.metadata.IndexMetadata)12 List (java.util.List)11 IndicesOptions (org.elasticsearch.action.support.IndicesOptions)11 ClusterName (org.elasticsearch.cluster.ClusterName)9 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)9 Settings (org.elasticsearch.common.settings.Settings)9 Matchers.containsString (org.hamcrest.Matchers.containsString)9 HashMap (java.util.HashMap)8 Nullable (javax.annotation.Nullable)8 RoutingNode (org.elasticsearch.cluster.routing.RoutingNode)8 RoutingNodes (org.elasticsearch.cluster.routing.RoutingNodes)8