From 5d987ad5e29cdd00bb710fad0a10e95c3611130e Mon Sep 17 00:00:00 2001 From: javanna Date: Wed, 23 Jul 2014 21:36:24 +0200 Subject: [PATCH] Internal: changed every single index operation to not replace the index within the original request An anti-pattern that we have in our code, noticeable for java API users, is that we modify incoming requests by replacing the index or alias with the concrete index. This way not only the request has changed, but all following communications that use that request will lose the information on whether the original request was performed against an alias or an index. Refactored the following base classes: `TransportShardReplicationOperationAction`, `TransportShardSingleOperationAction`, `TransportSingleCustomOperationAction`, `TransportInstanceSingleOperationAction` and all subclasses by introduced an InternalRequest object that contains the original request plus additional info (e.g. the concrete index). This internal request doesn't get sent over the transport but rebuilt on each node on demand (not different to what currently happens anyway, as concrete index gets set on each node). When the request becomes a shard level request, instead of using the only int shardId we serialize the ShardId that contains both concrete index name (which might then differ ffrom the original one within the request) and shard id. Using this pattern we can move get, multi_get, explain, analyze, term_vector, multi_term_vector, index, delete, update, bulk to not replace the index name with the concrete one within the request. The index name within the original request will stay the same. Made it also clearer within the different transport actions when the index needs to be resolved and when that's not needed (e.g. shard level request), by exposing `resolveIndex` method. Moved check block methods to parent classes as their content was always the same on every subclass. Improved existing tests by randomly introducing the use of an alias, and verifying that the responses always contain the concrete index name and not the original one, as that's the expected behaviour. Added backwards compatibility tests to make sure that the change is applied in a backwards compatible manner. Closes #7223 --- .../analyze/TransportAnalyzeAction.java | 23 +- .../TransportGetFieldMappingsIndexAction.java | 23 +- .../action/bulk/TransportBulkAction.java | 72 ++- .../action/bulk/TransportShardBulkAction.java | 70 ++- .../action/delete/IndexDeleteRequest.java | 4 +- .../action/delete/TransportDeleteAction.java | 49 +- .../delete/TransportShardDeleteAction.java | 23 +- .../TransportShardDeleteByQueryAction.java | 29 +- .../explain/TransportExplainAction.java | 43 +- .../action/get/TransportGetAction.java | 36 +- .../action/get/TransportMultiGetAction.java | 11 +- .../get/TransportShardMultiGetAction.java | 31 +- .../action/index/IndexRequest.java | 6 +- .../action/index/TransportIndexAction.java | 48 +- .../TransportMultiPercolateAction.java | 5 +- .../TransportShardMultiPercolateAction.java | 20 +- ...nsportShardReplicationOperationAction.java | 167 ++++--- .../TransportSingleCustomOperationAction.java | 112 +++-- ...ransportInstanceSingleOperationAction.java | 89 ++-- .../TransportShardSingleOperationAction.java | 109 +++-- .../TransportMultiTermVectorsAction.java | 14 +- ...portSingleShardMultiTermsVectorAction.java | 33 +- .../TransportSingleShardTermVectorAction.java | 37 +- .../action/update/TransportUpdateAction.java | 62 +-- .../action/update/UpdateHelper.java | 25 +- .../termvectors/ShardTermVectorService.java | 4 +- .../termvector/AbstractTermVectorTests.java | 26 +- .../action/termvector/GetTermVectorTests.java | 56 ++- .../termvector/MultiTermVectorsTests.java | 2 +- .../BasicBackwardsCompatibilityTest.java | 149 +++++- .../deleteByQuery/DeleteByQueryTests.java | 35 +- .../org/elasticsearch/document/BulkTests.java | 38 +- .../explain/ExplainActionTests.java | 138 +++--- .../org/elasticsearch/get/GetActionTests.java | 460 +++++++++--------- .../timestamp/TimestampMappingTests.java | 17 +- .../indices/analyze/AnalyzeActionTests.java | 70 ++- .../elasticsearch/mget/SimpleMgetTests.java | 50 +- .../org/elasticsearch/update/UpdateTests.java | 124 +++-- 38 files changed, 1284 insertions(+), 1026 deletions(-) diff --git a/src/main/java/org/elasticsearch/action/admin/indices/analyze/TransportAnalyzeAction.java b/src/main/java/org/elasticsearch/action/admin/indices/analyze/TransportAnalyzeAction.java index 92f911e2205..566537cabed 100644 --- a/src/main/java/org/elasticsearch/action/admin/indices/analyze/TransportAnalyzeAction.java +++ b/src/main/java/org/elasticsearch/action/admin/indices/analyze/TransportAnalyzeAction.java @@ -41,6 +41,7 @@ import org.elasticsearch.index.analysis.*; import org.elasticsearch.index.mapper.FieldMapper; import org.elasticsearch.index.mapper.internal.AllFieldMapper; import org.elasticsearch.index.service.IndexService; +import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.indices.IndicesService; import org.elasticsearch.indices.analysis.IndicesAnalysisService; import org.elasticsearch.threadpool.ThreadPool; @@ -85,28 +86,32 @@ public class TransportAnalyzeAction extends TransportSingleCustomOperationAction } @Override - protected ClusterBlockException checkRequestBlock(ClusterState state, AnalyzeRequest request) { - if (request.index() != null) { - request.index(state.metaData().concreteSingleIndex(request.index(), request.indicesOptions())); + protected boolean resolveIndex(AnalyzeRequest request) { + return request.index() != null; + } + + @Override + protected ClusterBlockException checkRequestBlock(ClusterState state, InternalRequest request) { + if (request.concreteIndex() != null) { return super.checkRequestBlock(state, request); } return null; } @Override - protected ShardsIterator shards(ClusterState state, AnalyzeRequest request) { - if (request.index() == null) { + protected ShardsIterator shards(ClusterState state, InternalRequest request) { + if (request.concreteIndex() == null) { // just execute locally.... return null; } - return state.routingTable().index(request.index()).randomAllActiveShardsIt(); + return state.routingTable().index(request.concreteIndex()).randomAllActiveShardsIt(); } @Override - protected AnalyzeResponse shardOperation(AnalyzeRequest request, int shardId) throws ElasticsearchException { + protected AnalyzeResponse shardOperation(AnalyzeRequest request, ShardId shardId) throws ElasticsearchException { IndexService indexService = null; - if (request.index() != null) { - indexService = indicesService.indexServiceSafe(request.index()); + if (shardId != null) { + indexService = indicesService.indexServiceSafe(shardId.getIndex()); } Analyzer analyzer = null; boolean closeAnalyzer = false; diff --git a/src/main/java/org/elasticsearch/action/admin/indices/mapping/get/TransportGetFieldMappingsIndexAction.java b/src/main/java/org/elasticsearch/action/admin/indices/mapping/get/TransportGetFieldMappingsIndexAction.java index 2fbac48bcf5..cc974535ad2 100644 --- a/src/main/java/org/elasticsearch/action/admin/indices/mapping/get/TransportGetFieldMappingsIndexAction.java +++ b/src/main/java/org/elasticsearch/action/admin/indices/mapping/get/TransportGetFieldMappingsIndexAction.java @@ -37,10 +37,10 @@ import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentType; -import org.elasticsearch.index.Index; import org.elasticsearch.index.mapper.DocumentMapper; import org.elasticsearch.index.mapper.FieldMapper; import org.elasticsearch.index.service.IndexService; +import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.indices.IndicesService; import org.elasticsearch.indices.TypeMissingException; import org.elasticsearch.threadpool.ThreadPool; @@ -76,14 +76,21 @@ public class TransportGetFieldMappingsIndexAction extends TransportSingleCustomO } @Override - protected ShardsIterator shards(ClusterState state, GetFieldMappingsIndexRequest request) { - // Will balance requests between shards - return state.routingTable().index(request.index()).randomAllActiveShardsIt(); + protected boolean resolveIndex(GetFieldMappingsIndexRequest request) { + //internal action, index already resolved + return false; } @Override - protected GetFieldMappingsResponse shardOperation(final GetFieldMappingsIndexRequest request, int shardId) throws ElasticsearchException { - IndexService indexService = indicesService.indexServiceSafe(request.index()); + protected ShardsIterator shards(ClusterState state, InternalRequest request) { + // Will balance requests between shards + return state.routingTable().index(request.concreteIndex()).randomAllActiveShardsIt(); + } + + @Override + protected GetFieldMappingsResponse shardOperation(final GetFieldMappingsIndexRequest request, ShardId shardId) throws ElasticsearchException { + assert shardId != null; + IndexService indexService = indicesService.indexServiceSafe(shardId.getIndex()); Collection typeIntersection; if (request.types().length == 0) { typeIntersection = indexService.mapperService().types(); @@ -98,7 +105,7 @@ public class TransportGetFieldMappingsIndexAction extends TransportSingleCustomO }); if (typeIntersection.isEmpty()) { - throw new TypeMissingException(new Index(request.index()), request.types()); + throw new TypeMissingException(shardId.index(), request.types()); } } @@ -111,7 +118,7 @@ public class TransportGetFieldMappingsIndexAction extends TransportSingleCustomO } } - return new GetFieldMappingsResponse(ImmutableMap.of(request.index(), typeMappings.immutableMap())); + return new GetFieldMappingsResponse(ImmutableMap.of(shardId.getIndex(), typeMappings.immutableMap())); } @Override diff --git a/src/main/java/org/elasticsearch/action/bulk/TransportBulkAction.java b/src/main/java/org/elasticsearch/action/bulk/TransportBulkAction.java index 1289fa352e5..a6a0ae31063 100644 --- a/src/main/java/org/elasticsearch/action/bulk/TransportBulkAction.java +++ b/src/main/java/org/elasticsearch/action/bulk/TransportBulkAction.java @@ -35,7 +35,7 @@ import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.action.support.ActionFilters; import org.elasticsearch.action.support.AutoCreateIndex; import org.elasticsearch.action.support.HandledTransportAction; -import org.elasticsearch.action.support.TransportAction; +import org.elasticsearch.action.support.IndicesOptions; import org.elasticsearch.action.update.UpdateRequest; import org.elasticsearch.cluster.ClusterService; import org.elasticsearch.cluster.ClusterState; @@ -51,14 +51,9 @@ import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.indices.IndexAlreadyExistsException; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.threadpool.ThreadPool; -import org.elasticsearch.transport.BaseTransportRequestHandler; -import org.elasticsearch.transport.TransportChannel; import org.elasticsearch.transport.TransportService; -import java.util.List; -import java.util.Locale; -import java.util.Map; -import java.util.Set; +import java.util.*; import java.util.concurrent.atomic.AtomicInteger; /** @@ -201,26 +196,25 @@ public class TransportBulkAction extends HandledTransportAction listener, final AtomicArray responses ) { - ClusterState clusterState = clusterService.state(); + final ClusterState clusterState = clusterService.state(); // TODO use timeout to wait here if its blocked... clusterState.blocks().globalBlockedRaiseException(ClusterBlockLevel.WRITE); + final ConcreteIndices concreteIndices = new ConcreteIndices(clusterState.metaData()); MetaData metaData = clusterState.metaData(); for (int i = 0; i < bulkRequest.requests.size(); i++) { ActionRequest request = bulkRequest.requests.get(i); if (request instanceof IndexRequest) { IndexRequest indexRequest = (IndexRequest) request; - String aliasOrIndex = indexRequest.index(); - indexRequest.index(clusterState.metaData().concreteSingleIndex(indexRequest.index(), indexRequest.indicesOptions())); - + String concreteIndex = concreteIndices.resolveIfAbsent(indexRequest.index(), indexRequest.indicesOptions()); MappingMetaData mappingMd = null; - if (metaData.hasIndex(indexRequest.index())) { - mappingMd = metaData.index(indexRequest.index()).mappingOrDefault(indexRequest.type()); + if (metaData.hasIndex(concreteIndex)) { + mappingMd = metaData.index(concreteIndex).mappingOrDefault(indexRequest.type()); } try { - indexRequest.process(metaData, aliasOrIndex, mappingMd, allowIdGeneration); + indexRequest.process(metaData, mappingMd, allowIdGeneration, concreteIndex); } catch (ElasticsearchParseException e) { - BulkItemResponse.Failure failure = new BulkItemResponse.Failure(indexRequest.index(), indexRequest.type(), indexRequest.id(), e); + BulkItemResponse.Failure failure = new BulkItemResponse.Failure(concreteIndex, indexRequest.type(), indexRequest.id(), e); BulkItemResponse bulkItemResponse = new BulkItemResponse(i, "index", failure); responses.set(i, bulkItemResponse); // make sure the request gets never processed again @@ -228,12 +222,12 @@ public class TransportBulkAction extends HandledTransportAction list = requestsByShard.get(shardId); if (list == null) { list = Lists.newArrayList(); @@ -253,10 +248,11 @@ public class TransportBulkAction extends HandledTransportAction list = requestsByShard.get(shardIt.shardId()); if (list == null) { @@ -266,7 +262,7 @@ public class TransportBulkAction extends HandledTransportAction list = requestsByShard.get(shardId); if (list == null) { list = Lists.newArrayList(); @@ -276,11 +272,12 @@ public class TransportBulkAction extends HandledTransportAction list = requestsByShard.get(shardId); if (list == null) { list = Lists.newArrayList(); @@ -323,15 +320,15 @@ public class TransportBulkAction extends HandledTransportAction indices = new HashMap<>(); + private final MetaData metaData; + + ConcreteIndices(MetaData metaData) { + this.metaData = metaData; + } + + String getConcreteIndex(String indexOrAlias) { + return indices.get(indexOrAlias); + } + + String resolveIfAbsent(String indexOrAlias, IndicesOptions indicesOptions) { + String concreteIndex = indices.get(indexOrAlias); + if (concreteIndex == null) { + concreteIndex = metaData.concreteSingleIndex(indexOrAlias, indicesOptions); + indices.put(indexOrAlias, concreteIndex); + } + return concreteIndex; + } + } } diff --git a/src/main/java/org/elasticsearch/action/bulk/TransportShardBulkAction.java b/src/main/java/org/elasticsearch/action/bulk/TransportShardBulkAction.java index 24fcd827711..7a60416241f 100644 --- a/src/main/java/org/elasticsearch/action/bulk/TransportShardBulkAction.java +++ b/src/main/java/org/elasticsearch/action/bulk/TransportShardBulkAction.java @@ -39,8 +39,6 @@ import org.elasticsearch.cluster.ClusterService; import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.action.index.MappingUpdatedAction; import org.elasticsearch.cluster.action.shard.ShardStateAction; -import org.elasticsearch.cluster.block.ClusterBlockException; -import org.elasticsearch.cluster.block.ClusterBlockLevel; import org.elasticsearch.cluster.metadata.MappingMetaData; import org.elasticsearch.cluster.routing.ShardIterator; import org.elasticsearch.common.Nullable; @@ -57,7 +55,6 @@ import org.elasticsearch.index.engine.VersionConflictEngineException; import org.elasticsearch.index.mapper.DocumentMapper; import org.elasticsearch.index.mapper.SourceToParse; import org.elasticsearch.index.service.IndexService; -import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.index.shard.service.IndexShard; import org.elasticsearch.indices.IndicesService; import org.elasticsearch.rest.RestStatus; @@ -123,25 +120,20 @@ public class TransportShardBulkAction extends TransportShardReplicationOperation } @Override - protected ClusterBlockException checkGlobalBlock(ClusterState state, BulkShardRequest request) { - return state.blocks().globalBlockedException(ClusterBlockLevel.WRITE); + protected boolean resolveIndex() { + return false; } @Override - protected ClusterBlockException checkRequestBlock(ClusterState state, BulkShardRequest request) { - return state.blocks().indexBlockedException(ClusterBlockLevel.WRITE, request.index()); - } - - @Override - protected ShardIterator shards(ClusterState clusterState, BulkShardRequest request) { - return clusterState.routingTable().index(request.index()).shard(request.shardId()).shardsIt(); + protected ShardIterator shards(ClusterState clusterState, InternalRequest request) { + return clusterState.routingTable().index(request.concreteIndex()).shard(request.request().shardId()).shardsIt(); } @Override protected PrimaryResponse shardOperationOnPrimary(ClusterState clusterState, PrimaryOperationRequest shardRequest) { final BulkShardRequest request = shardRequest.request; - IndexService indexService = indicesService.indexServiceSafe(shardRequest.request.index()); - IndexShard indexShard = indexService.shardSafe(shardRequest.shardId); + IndexService indexService = indicesService.indexServiceSafe(request.index()); + IndexShard indexShard = indexService.shardSafe(shardRequest.shardId.id()); Engine.IndexingOperation[] ops = null; final Set mappingTypesToUpdate = Sets.newHashSet(); @@ -191,12 +183,12 @@ public class TransportShardBulkAction extends TransportShardReplicationOperation throw (ElasticsearchException) e; } if (e instanceof ElasticsearchException && ((ElasticsearchException) e).status() == RestStatus.CONFLICT) { - logger.trace("[{}][{}] failed to execute bulk item (index) {}", e, shardRequest.request.index(), shardRequest.shardId, indexRequest); + logger.trace("{} failed to execute bulk item (index) {}", e, shardRequest.shardId, indexRequest); } else { - logger.debug("[{}][{}] failed to execute bulk item (index) {}", e, shardRequest.request.index(), shardRequest.shardId, indexRequest); + logger.debug("{} failed to execute bulk item (index) {}", e, shardRequest.shardId, indexRequest); } responses[requestIndex] = new BulkItemResponse(item.id(), indexRequest.opType().lowercase(), - new BulkItemResponse.Failure(indexRequest.index(), indexRequest.type(), indexRequest.id(), e)); + new BulkItemResponse.Failure(request.index(), indexRequest.type(), indexRequest.id(), e)); // nullify the request so it won't execute on the replicas request.items()[requestIndex] = null; } @@ -207,7 +199,7 @@ public class TransportShardBulkAction extends TransportShardReplicationOperation try { // add the response - DeleteResponse deleteResponse = shardDeleteOperation(deleteRequest, indexShard).response(); + DeleteResponse deleteResponse = shardDeleteOperation(request, deleteRequest, indexShard).response(); responses[requestIndex] = new BulkItemResponse(item.id(), OP_TYPE_DELETE, deleteResponse); } catch (Throwable e) { // rethrow the failure if we are going to retry on primary and let parent failure to handle it @@ -219,12 +211,12 @@ public class TransportShardBulkAction extends TransportShardReplicationOperation throw (ElasticsearchException) e; } if (e instanceof ElasticsearchException && ((ElasticsearchException) e).status() == RestStatus.CONFLICT) { - logger.trace("[{}][{}] failed to execute bulk item (delete) {}", e, shardRequest.request.index(), shardRequest.shardId, deleteRequest); + logger.trace("{} failed to execute bulk item (delete) {}", e, shardRequest.shardId, deleteRequest); } else { - logger.debug("[{}][{}] failed to execute bulk item (delete) {}", e, shardRequest.request.index(), shardRequest.shardId, deleteRequest); + logger.debug("{} failed to execute bulk item (delete) {}", e, shardRequest.shardId, deleteRequest); } responses[requestIndex] = new BulkItemResponse(item.id(), OP_TYPE_DELETE, - new BulkItemResponse.Failure(deleteRequest.index(), deleteRequest.type(), deleteRequest.id(), e)); + new BulkItemResponse.Failure(request.index(), deleteRequest.type(), deleteRequest.id(), e)); // nullify the request so it won't execute on the replicas request.items()[requestIndex] = null; } @@ -253,7 +245,7 @@ public class TransportShardBulkAction extends TransportShardReplicationOperation UpdateResponse updateResponse = new UpdateResponse(indexResponse.getIndex(), indexResponse.getType(), indexResponse.getId(), indexResponse.getVersion(), indexResponse.isCreated()); if (updateRequest.fields() != null && updateRequest.fields().length > 0) { Tuple> sourceAndContent = XContentHelper.convertToMap(indexSourceAsBytes, true); - updateResponse.setGetResult(updateHelper.extractGetResult(updateRequest, indexResponse.getVersion(), sourceAndContent.v2(), sourceAndContent.v1(), indexSourceAsBytes)); + updateResponse.setGetResult(updateHelper.extractGetResult(updateRequest, shardRequest.request.index(), indexResponse.getVersion(), sourceAndContent.v2(), sourceAndContent.v1(), indexSourceAsBytes)); } responses[requestIndex] = new BulkItemResponse(item.id(), OP_TYPE_UPDATE, updateResponse); if (result.mappingTypeToUpdate != null) { @@ -272,7 +264,7 @@ public class TransportShardBulkAction extends TransportShardReplicationOperation DeleteResponse response = updateResult.writeResult.response(); DeleteRequest deleteRequest = updateResult.request(); updateResponse = new UpdateResponse(response.getIndex(), response.getType(), response.getId(), response.getVersion(), false); - updateResponse.setGetResult(updateHelper.extractGetResult(updateRequest, response.getVersion(), updateResult.result.updatedSourceAsMap(), updateResult.result.updateSourceContentType(), null)); + updateResponse.setGetResult(updateHelper.extractGetResult(updateRequest, shardRequest.request.index(), response.getVersion(), updateResult.result.updatedSourceAsMap(), updateResult.result.updateSourceContentType(), null)); responses[requestIndex] = new BulkItemResponse(item.id(), OP_TYPE_UPDATE, updateResponse); // Replace the update request to the translated delete request to execute on the replica. request.items()[requestIndex] = new BulkItemRequest(request.items()[requestIndex].id(), deleteRequest); @@ -291,7 +283,7 @@ public class TransportShardBulkAction extends TransportShardReplicationOperation if (updateAttemptsCount >= updateRequest.retryOnConflict()) { // we can't try any more responses[requestIndex] = new BulkItemResponse(item.id(), OP_TYPE_UPDATE, - new BulkItemResponse.Failure(updateRequest.index(), updateRequest.type(), updateRequest.id(), t)); + new BulkItemResponse.Failure(request.index(), updateRequest.type(), updateRequest.id(), t)); request.items()[requestIndex] = null; // do not send to replicas } } else { @@ -304,29 +296,29 @@ public class TransportShardBulkAction extends TransportShardReplicationOperation throw (ElasticsearchException) t; } if (updateResult.result == null) { - responses[requestIndex] = new BulkItemResponse(item.id(), OP_TYPE_UPDATE, new BulkItemResponse.Failure(updateRequest.index(), updateRequest.type(), updateRequest.id(), t)); + responses[requestIndex] = new BulkItemResponse(item.id(), OP_TYPE_UPDATE, new BulkItemResponse.Failure(shardRequest.request.index(), updateRequest.type(), updateRequest.id(), t)); } else { switch (updateResult.result.operation()) { case UPSERT: case INDEX: IndexRequest indexRequest = updateResult.request(); if (t instanceof ElasticsearchException && ((ElasticsearchException) t).status() == RestStatus.CONFLICT) { - logger.trace("[{}][{}] failed to execute bulk item (index) {}", t, shardRequest.request.index(), shardRequest.shardId, indexRequest); + logger.trace("{} failed to execute bulk item (index) {}", t, shardRequest.shardId, indexRequest); } else { - logger.debug("[{}][{}] failed to execute bulk item (index) {}", t, shardRequest.request.index(), shardRequest.shardId, indexRequest); + logger.debug("{} failed to execute bulk item (index) {}", t, shardRequest.shardId, indexRequest); } responses[requestIndex] = new BulkItemResponse(item.id(), OP_TYPE_UPDATE, - new BulkItemResponse.Failure(indexRequest.index(), indexRequest.type(), indexRequest.id(), t)); + new BulkItemResponse.Failure(request.index(), indexRequest.type(), indexRequest.id(), t)); break; case DELETE: DeleteRequest deleteRequest = updateResult.request(); if (t instanceof ElasticsearchException && ((ElasticsearchException) t).status() == RestStatus.CONFLICT) { - logger.trace("[{}][{}] failed to execute bulk item (delete) {}", t, shardRequest.request.index(), shardRequest.shardId, deleteRequest); + logger.trace("{} failed to execute bulk item (delete) {}", t, shardRequest.shardId, deleteRequest); } else { - logger.debug("[{}][{}] failed to execute bulk item (delete) {}", t, shardRequest.request.index(), shardRequest.shardId, deleteRequest); + logger.debug("{} failed to execute bulk item (delete) {}", t, shardRequest.shardId, deleteRequest); } responses[requestIndex] = new BulkItemResponse(item.id(), OP_TYPE_DELETE, - new BulkItemResponse.Failure(deleteRequest.index(), deleteRequest.type(), deleteRequest.id(), t)); + new BulkItemResponse.Failure(request.index(), deleteRequest.type(), deleteRequest.id(), t)); break; } } @@ -359,7 +351,7 @@ public class TransportShardBulkAction extends TransportShardReplicationOperation // ignore } } - BulkShardResponse response = new BulkShardResponse(new ShardId(request.index(), request.shardId()), responses); + BulkShardResponse response = new BulkShardResponse(shardRequest.shardId, responses); return new PrimaryResponse<>(shardRequest.request, response, ops); } @@ -400,12 +392,12 @@ public class TransportShardBulkAction extends TransportShardReplicationOperation MappingMetaData mappingMd = clusterState.metaData().index(request.index()).mappingOrDefault(indexRequest.type()); if (mappingMd != null && mappingMd.routing().required()) { if (indexRequest.routing() == null) { - throw new RoutingMissingException(indexRequest.index(), indexRequest.type(), indexRequest.id()); + throw new RoutingMissingException(request.index(), indexRequest.type(), indexRequest.id()); } } if (!processed) { - indexRequest.process(clusterState.metaData(), indexRequest.index(), mappingMd, allowIdGeneration); + indexRequest.process(clusterState.metaData(), mappingMd, allowIdGeneration, request.index()); } SourceToParse sourceToParse = SourceToParse.source(SourceToParse.Origin.PRIMARY, indexRequest.source()).type(indexRequest.type()).id(indexRequest.id()) @@ -448,11 +440,11 @@ public class TransportShardBulkAction extends TransportShardReplicationOperation assert indexRequest.versionType().validateVersionForWrites(indexRequest.version()); - IndexResponse indexResponse = new IndexResponse(indexRequest.index(), indexRequest.type(), indexRequest.id(), version, created); + IndexResponse indexResponse = new IndexResponse(request.index(), indexRequest.type(), indexRequest.id(), version, created); return new WriteResult(indexResponse, mappingTypeToUpdate, op); } - private WriteResult shardDeleteOperation(DeleteRequest deleteRequest, IndexShard indexShard) { + private WriteResult shardDeleteOperation(BulkShardRequest request, DeleteRequest deleteRequest, IndexShard indexShard) { Engine.Delete delete = indexShard.prepareDelete(deleteRequest.type(), deleteRequest.id(), deleteRequest.version(), deleteRequest.versionType(), Engine.Operation.Origin.PRIMARY); indexShard.delete(delete); // update the request with the version so it will go to the replicas @@ -461,7 +453,7 @@ public class TransportShardBulkAction extends TransportShardReplicationOperation assert deleteRequest.versionType().validateVersionForWrites(deleteRequest.version()); - DeleteResponse deleteResponse = new DeleteResponse(deleteRequest.index(), deleteRequest.type(), deleteRequest.id(), delete.version(), delete.found()); + DeleteResponse deleteResponse = new DeleteResponse(request.index(), deleteRequest.type(), deleteRequest.id(), delete.version(), delete.found()); return new WriteResult(deleteResponse, null, null); } @@ -538,7 +530,7 @@ public class TransportShardBulkAction extends TransportShardReplicationOperation case DELETE: DeleteRequest deleteRequest = translate.action(); try { - WriteResult result = shardDeleteOperation(deleteRequest, indexShard); + WriteResult result = shardDeleteOperation(bulkShardRequest, deleteRequest, indexShard); return new UpdateResult(translate, deleteRequest, result); } catch (Throwable t) { t = ExceptionsHelper.unwrapCause(t); @@ -559,7 +551,7 @@ public class TransportShardBulkAction extends TransportShardReplicationOperation protected void shardOperationOnReplica(ReplicaOperationRequest shardRequest) { - IndexShard indexShard = indicesService.indexServiceSafe(shardRequest.request.index()).shardSafe(shardRequest.shardId); + IndexShard indexShard = indicesService.indexServiceSafe(shardRequest.shardId.getIndex()).shardSafe(shardRequest.shardId.id()); final BulkShardRequest request = shardRequest.request; for (int i = 0; i < request.items().length; i++) { BulkItemRequest item = request.items()[i]; diff --git a/src/main/java/org/elasticsearch/action/delete/IndexDeleteRequest.java b/src/main/java/org/elasticsearch/action/delete/IndexDeleteRequest.java index a7e19f8afae..494ddb69e45 100644 --- a/src/main/java/org/elasticsearch/action/delete/IndexDeleteRequest.java +++ b/src/main/java/org/elasticsearch/action/delete/IndexDeleteRequest.java @@ -32,8 +32,8 @@ class IndexDeleteRequest extends IndexReplicationOperationRequest listener) { - request.routing(state.metaData().resolveIndexRouting(request.routing(), request.index())); - request.index(state.metaData().concreteSingleIndex(request.index(), request.indicesOptions())); - if (state.metaData().hasIndex(request.index())) { + protected boolean resolveIndex() { + return true; + } + + @Override + protected boolean resolveRequest(final ClusterState state, final InternalRequest request, final ActionListener listener) { + request.request().routing(state.metaData().resolveIndexRouting(request.request().routing(), request.request().index())); + if (state.metaData().hasIndex(request.concreteIndex())) { // check if routing is required, if so, do a broadcast delete - MappingMetaData mappingMd = state.metaData().index(request.index()).mappingOrDefault(request.type()); + MappingMetaData mappingMd = state.metaData().index(request.concreteIndex()).mappingOrDefault(request.request().type()); if (mappingMd != null && mappingMd.routing().required()) { - if (request.routing() == null) { - if (request.versionType() != VersionType.INTERNAL) { + if (request.request().routing() == null) { + if (request.request().versionType() != VersionType.INTERNAL) { // TODO: implement this feature - throw new ElasticsearchIllegalArgumentException("routing value is required for deleting documents of type [" + request.type() - + "] while using version_type [" + request.versionType() + "]"); + throw new ElasticsearchIllegalArgumentException("routing value is required for deleting documents of type [" + request.request().type() + + "] while using version_type [" + request.request().versionType() + "]"); } - indexDeleteAction.execute(new IndexDeleteRequest(request), new ActionListener() { + indexDeleteAction.execute(new IndexDeleteRequest(request.request(), request.concreteIndex()), new ActionListener() { @Override public void onResponse(IndexDeleteResponse indexDeleteResponse) { // go over the response, see if we have found one, and the version if found @@ -124,7 +126,7 @@ public class TransportDeleteAction extends TransportShardReplicationOperationAct break; } } - listener.onResponse(new DeleteResponse(request.index(), request.type(), request.id(), version, found)); + listener.onResponse(new DeleteResponse(request.concreteIndex(), request.request().type(), request.request().id(), version, found)); } @Override @@ -163,20 +165,10 @@ public class TransportDeleteAction extends TransportShardReplicationOperationAct return new DeleteResponse(); } - @Override - protected ClusterBlockException checkGlobalBlock(ClusterState state, DeleteRequest request) { - return state.blocks().globalBlockedException(ClusterBlockLevel.WRITE); - } - - @Override - protected ClusterBlockException checkRequestBlock(ClusterState state, DeleteRequest request) { - return state.blocks().indexBlockedException(ClusterBlockLevel.WRITE, request.index()); - } - @Override protected PrimaryResponse shardOperationOnPrimary(ClusterState clusterState, PrimaryOperationRequest shardRequest) { DeleteRequest request = shardRequest.request; - IndexShard indexShard = indicesService.indexServiceSafe(shardRequest.request.index()).shardSafe(shardRequest.shardId); + IndexShard indexShard = indicesService.indexServiceSafe(shardRequest.shardId.getIndex()).shardSafe(shardRequest.shardId.id()); Engine.Delete delete = indexShard.prepareDelete(request.type(), request.id(), request.version(), request.versionType(), Engine.Operation.Origin.PRIMARY); indexShard.delete(delete); // update the request with teh version so it will go to the replicas @@ -193,14 +185,14 @@ public class TransportDeleteAction extends TransportShardReplicationOperationAct } } - DeleteResponse response = new DeleteResponse(request.index(), request.type(), request.id(), delete.version(), delete.found()); + DeleteResponse response = new DeleteResponse(shardRequest.shardId.getIndex(), request.type(), request.id(), delete.version(), delete.found()); return new PrimaryResponse<>(shardRequest.request, response, null); } @Override protected void shardOperationOnReplica(ReplicaOperationRequest shardRequest) { DeleteRequest request = shardRequest.request; - IndexShard indexShard = indicesService.indexServiceSafe(shardRequest.request.index()).shardSafe(shardRequest.shardId); + IndexShard indexShard = indicesService.indexServiceSafe(shardRequest.shardId.getIndex()).shardSafe(shardRequest.shardId.id()); Engine.Delete delete = indexShard.prepareDelete(request.type(), request.id(), request.version(), request.versionType(), Engine.Operation.Origin.REPLICA); indexShard.delete(delete); @@ -212,12 +204,11 @@ public class TransportDeleteAction extends TransportShardReplicationOperationAct // ignore } } - } @Override - protected ShardIterator shards(ClusterState clusterState, DeleteRequest request) { + protected ShardIterator shards(ClusterState clusterState, InternalRequest request) { return clusterService.operationRouting() - .deleteShards(clusterService.state(), request.index(), request.type(), request.id(), request.routing()); + .deleteShards(clusterService.state(), request.concreteIndex(), request.request().type(), request.request().id(), request.request().routing()); } } diff --git a/src/main/java/org/elasticsearch/action/delete/TransportShardDeleteAction.java b/src/main/java/org/elasticsearch/action/delete/TransportShardDeleteAction.java index b738388523d..6db345f1504 100644 --- a/src/main/java/org/elasticsearch/action/delete/TransportShardDeleteAction.java +++ b/src/main/java/org/elasticsearch/action/delete/TransportShardDeleteAction.java @@ -25,8 +25,6 @@ import org.elasticsearch.action.support.replication.TransportShardReplicationOpe import org.elasticsearch.cluster.ClusterService; import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.action.shard.ShardStateAction; -import org.elasticsearch.cluster.block.ClusterBlockException; -import org.elasticsearch.cluster.block.ClusterBlockLevel; import org.elasticsearch.cluster.routing.GroupShardsIterator; import org.elasticsearch.cluster.routing.ShardIterator; import org.elasticsearch.common.inject.Inject; @@ -78,19 +76,14 @@ public class TransportShardDeleteAction extends TransportShardReplicationOperati } @Override - protected ClusterBlockException checkGlobalBlock(ClusterState state, ShardDeleteRequest request) { - return state.blocks().globalBlockedException(ClusterBlockLevel.WRITE); - } - - @Override - protected ClusterBlockException checkRequestBlock(ClusterState state, ShardDeleteRequest request) { - return state.blocks().indexBlockedException(ClusterBlockLevel.WRITE, request.index()); + protected boolean resolveIndex() { + return false; } @Override protected PrimaryResponse shardOperationOnPrimary(ClusterState clusterState, PrimaryOperationRequest shardRequest) { ShardDeleteRequest request = shardRequest.request; - IndexShard indexShard = indicesService.indexServiceSafe(shardRequest.request.index()).shardSafe(shardRequest.shardId); + IndexShard indexShard = indicesService.indexServiceSafe(shardRequest.shardId.getIndex()).shardSafe(shardRequest.shardId.id()); Engine.Delete delete = indexShard.prepareDelete(request.type(), request.id(), request.version(), VersionType.INTERNAL, Engine.Operation.Origin.PRIMARY); indexShard.delete(delete); // update the version to happen on the replicas @@ -112,7 +105,7 @@ public class TransportShardDeleteAction extends TransportShardReplicationOperati @Override protected void shardOperationOnReplica(ReplicaOperationRequest shardRequest) { ShardDeleteRequest request = shardRequest.request; - IndexShard indexShard = indicesService.indexServiceSafe(shardRequest.request.index()).shardSafe(shardRequest.shardId); + IndexShard indexShard = indicesService.indexServiceSafe(shardRequest.shardId.getIndex()).shardSafe(shardRequest.shardId.id()); Engine.Delete delete = indexShard.prepareDelete(request.type(), request.id(), request.version(), VersionType.INTERNAL, Engine.Operation.Origin.REPLICA); // IndexDeleteAction doesn't support version type at the moment. Hard coded for the INTERNAL version @@ -133,13 +126,13 @@ public class TransportShardDeleteAction extends TransportShardReplicationOperati } @Override - protected ShardIterator shards(ClusterState clusterState, ShardDeleteRequest request) { - GroupShardsIterator group = clusterService.operationRouting().broadcastDeleteShards(clusterService.state(), request.index()); + protected ShardIterator shards(ClusterState clusterState, InternalRequest request) { + GroupShardsIterator group = clusterService.operationRouting().broadcastDeleteShards(clusterService.state(), request.concreteIndex()); for (ShardIterator shardIt : group) { - if (shardIt.shardId().id() == request.shardId()) { + if (shardIt.shardId().id() == request.request().shardId()) { return shardIt; } } - throw new ElasticsearchIllegalStateException("No shards iterator found for shard [" + request.shardId() + "]"); + throw new ElasticsearchIllegalStateException("No shards iterator found for shard [" + request.request().shardId() + "]"); } } diff --git a/src/main/java/org/elasticsearch/action/deletebyquery/TransportShardDeleteByQueryAction.java b/src/main/java/org/elasticsearch/action/deletebyquery/TransportShardDeleteByQueryAction.java index a4e651a0bda..408e722524b 100644 --- a/src/main/java/org/elasticsearch/action/deletebyquery/TransportShardDeleteByQueryAction.java +++ b/src/main/java/org/elasticsearch/action/deletebyquery/TransportShardDeleteByQueryAction.java @@ -29,8 +29,6 @@ import org.elasticsearch.cache.recycler.PageCacheRecycler; import org.elasticsearch.cluster.ClusterService; import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.action.shard.ShardStateAction; -import org.elasticsearch.cluster.block.ClusterBlockException; -import org.elasticsearch.cluster.block.ClusterBlockLevel; import org.elasticsearch.cluster.routing.GroupShardsIterator; import org.elasticsearch.cluster.routing.ShardIterator; import org.elasticsearch.common.inject.Inject; @@ -100,20 +98,15 @@ public class TransportShardDeleteByQueryAction extends TransportShardReplication } @Override - protected ClusterBlockException checkGlobalBlock(ClusterState state, ShardDeleteByQueryRequest request) { - return state.blocks().globalBlockedException(ClusterBlockLevel.WRITE); - } - - @Override - protected ClusterBlockException checkRequestBlock(ClusterState state, ShardDeleteByQueryRequest request) { - return state.blocks().indexBlockedException(ClusterBlockLevel.WRITE, request.index()); + protected boolean resolveIndex() { + return false; } @Override protected PrimaryResponse shardOperationOnPrimary(ClusterState clusterState, PrimaryOperationRequest shardRequest) { ShardDeleteByQueryRequest request = shardRequest.request; - IndexService indexService = indicesService.indexServiceSafe(shardRequest.request.index()); - IndexShard indexShard = indexService.shardSafe(shardRequest.shardId); + IndexService indexService = indicesService.indexServiceSafe(shardRequest.shardId.getIndex()); + IndexShard indexShard = indexService.shardSafe(shardRequest.shardId.id()); SearchContext.setCurrent(new DefaultSearchContext(0, new ShardSearchRequest().types(request.types()).nowInMillis(request.nowInMillis()), null, indexShard.acquireSearcher(DELETE_BY_QUERY_API), indexService, indexShard, scriptService, cacheRecycler, @@ -134,8 +127,8 @@ public class TransportShardDeleteByQueryAction extends TransportShardReplication @Override protected void shardOperationOnReplica(ReplicaOperationRequest shardRequest) { ShardDeleteByQueryRequest request = shardRequest.request; - IndexService indexService = indicesService.indexServiceSafe(shardRequest.request.index()); - IndexShard indexShard = indexService.shardSafe(shardRequest.shardId); + IndexService indexService = indicesService.indexServiceSafe(shardRequest.shardId.getIndex()); + IndexShard indexShard = indexService.shardSafe(shardRequest.shardId.id()); SearchContext.setCurrent(new DefaultSearchContext(0, new ShardSearchRequest().types(request.types()).nowInMillis(request.nowInMillis()), null, indexShard.acquireSearcher(DELETE_BY_QUERY_API, IndexShard.Mode.WRITE), indexService, indexShard, scriptService, @@ -145,20 +138,20 @@ public class TransportShardDeleteByQueryAction extends TransportShardReplication SearchContext.current().parsedQuery(new ParsedQuery(deleteByQuery.query(), ImmutableMap.of())); indexShard.deleteByQuery(deleteByQuery); } finally { - try (SearchContext searchContext = SearchContext.current();) { + try (SearchContext searchContext = SearchContext.current()) { SearchContext.removeCurrent(); } } } @Override - protected ShardIterator shards(ClusterState clusterState, ShardDeleteByQueryRequest request) { - GroupShardsIterator group = clusterService.operationRouting().deleteByQueryShards(clusterService.state(), request.index(), request.routing()); + protected ShardIterator shards(ClusterState clusterState, InternalRequest request) { + GroupShardsIterator group = clusterService.operationRouting().deleteByQueryShards(clusterService.state(), request.concreteIndex(), request.request().routing()); for (ShardIterator shardIt : group) { - if (shardIt.shardId().id() == request.shardId()) { + if (shardIt.shardId().id() == request.request().shardId()) { return shardIt; } } - throw new ElasticsearchIllegalStateException("No shards iterator found for shard [" + request.shardId() + "]"); + throw new ElasticsearchIllegalStateException("No shards iterator found for shard [" + request.request().shardId() + "]"); } } diff --git a/src/main/java/org/elasticsearch/action/explain/TransportExplainAction.java b/src/main/java/org/elasticsearch/action/explain/TransportExplainAction.java index 2663ab1b985..c6c539d5c3b 100644 --- a/src/main/java/org/elasticsearch/action/explain/TransportExplainAction.java +++ b/src/main/java/org/elasticsearch/action/explain/TransportExplainAction.java @@ -30,8 +30,6 @@ import org.elasticsearch.cache.recycler.CacheRecycler; import org.elasticsearch.cache.recycler.PageCacheRecycler; import org.elasticsearch.cluster.ClusterService; import org.elasticsearch.cluster.ClusterState; -import org.elasticsearch.cluster.block.ClusterBlockException; -import org.elasticsearch.cluster.block.ClusterBlockLevel; import org.elasticsearch.cluster.routing.ShardIterator; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.settings.Settings; @@ -41,6 +39,7 @@ import org.elasticsearch.index.get.GetResult; import org.elasticsearch.index.mapper.Uid; import org.elasticsearch.index.mapper.internal.UidFieldMapper; import org.elasticsearch.index.service.IndexService; +import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.index.shard.service.IndexShard; import org.elasticsearch.indices.IndicesService; import org.elasticsearch.script.ScriptService; @@ -94,24 +93,27 @@ public class TransportExplainAction extends TransportShardSingleOperationAction< } @Override - protected void resolveRequest(ClusterState state, ExplainRequest request) { - String concreteIndex = state.metaData().concreteSingleIndex(request.index(), request.indicesOptions()); - request.filteringAlias(state.metaData().filteringAliases(concreteIndex, request.index())); - request.index(state.metaData().concreteSingleIndex(request.index(), request.indicesOptions())); + protected boolean resolveIndex() { + return true; + } + @Override + protected void resolveRequest(ClusterState state, InternalRequest request) { + request.request().filteringAlias(state.metaData().filteringAliases(request.concreteIndex(), request.request().index())); // Fail fast on the node that received the request. - if (request.routing() == null && state.getMetaData().routingRequired(request.index(), request.type())) { - throw new RoutingMissingException(request.index(), request.type(), request.id()); + if (request.request().routing() == null && state.getMetaData().routingRequired(request.concreteIndex(), request.request().type())) { + throw new RoutingMissingException(request.concreteIndex(), request.request().type(), request.request().id()); } } - protected ExplainResponse shardOperation(ExplainRequest request, int shardId) throws ElasticsearchException { - IndexService indexService = indicesService.indexServiceSafe(request.index()); - IndexShard indexShard = indexService.shardSafe(shardId); + @Override + protected ExplainResponse shardOperation(ExplainRequest request, ShardId shardId) throws ElasticsearchException { + IndexService indexService = indicesService.indexService(shardId.getIndex()); + IndexShard indexShard = indexService.shardSafe(shardId.id()); Term uidTerm = new Term(UidFieldMapper.NAME, Uid.createUidAsBytes(request.type(), request.id())); Engine.GetResult result = indexShard.get(new Engine.Get(false, uidTerm)); if (!result.exists()) { - return new ExplainResponse(request.index(), request.type(), request.id(), false); + return new ExplainResponse(shardId.getIndex(), request.type(), request.id(), false); } SearchContext context = new DefaultSearchContext( @@ -139,9 +141,9 @@ public class TransportExplainAction extends TransportShardSingleOperationAction< // because we are working in the same searcher in engineGetResult we can be sure that a // doc isn't deleted between the initial get and this call. GetResult getResult = indexShard.getService().get(result, request.id(), request.type(), request.fields(), request.fetchSourceContext(), false); - return new ExplainResponse(request.index(), request.type(), request.id(), true, explanation, getResult); + return new ExplainResponse(shardId.getIndex(), request.type(), request.id(), true, explanation, getResult); } else { - return new ExplainResponse(request.index(), request.type(), request.id(), true, explanation); + return new ExplainResponse(shardId.getIndex(), request.type(), request.id(), true, explanation); } } catch (IOException e) { throw new ElasticsearchException("Could not explain", e); @@ -159,17 +161,10 @@ public class TransportExplainAction extends TransportShardSingleOperationAction< return new ExplainResponse(); } - protected ClusterBlockException checkGlobalBlock(ClusterState state, ExplainRequest request) { - return state.blocks().globalBlockedException(ClusterBlockLevel.READ); - } - - protected ClusterBlockException checkRequestBlock(ClusterState state, ExplainRequest request) { - return state.blocks().indexBlockedException(ClusterBlockLevel.READ, request.index()); - } - - protected ShardIterator shards(ClusterState state, ExplainRequest request) throws ElasticsearchException { + @Override + protected ShardIterator shards(ClusterState state, InternalRequest request) throws ElasticsearchException { return clusterService.operationRouting().getShards( - clusterService.state(), request.index(), request.type(), request.id(), request.routing(), request.preference() + clusterService.state(), request.concreteIndex(), request.request().type(), request.request().id(), request.request().routing(), request.request().preference() ); } } diff --git a/src/main/java/org/elasticsearch/action/get/TransportGetAction.java b/src/main/java/org/elasticsearch/action/get/TransportGetAction.java index f92a0efd6eb..b4dd167c6a8 100644 --- a/src/main/java/org/elasticsearch/action/get/TransportGetAction.java +++ b/src/main/java/org/elasticsearch/action/get/TransportGetAction.java @@ -25,14 +25,13 @@ import org.elasticsearch.action.support.ActionFilters; import org.elasticsearch.action.support.single.shard.TransportShardSingleOperationAction; import org.elasticsearch.cluster.ClusterService; import org.elasticsearch.cluster.ClusterState; -import org.elasticsearch.cluster.block.ClusterBlockException; -import org.elasticsearch.cluster.block.ClusterBlockLevel; import org.elasticsearch.cluster.routing.ShardIterator; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.index.engine.Engine; import org.elasticsearch.index.get.GetResult; import org.elasticsearch.index.service.IndexService; +import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.index.shard.service.IndexShard; import org.elasticsearch.indices.IndicesService; import org.elasticsearch.threadpool.ThreadPool; @@ -63,40 +62,33 @@ public class TransportGetAction extends TransportShardSingleOperationAction return this.autoGeneratedId; } - public void process(MetaData metaData, String aliasOrIndex, @Nullable MappingMetaData mappingMd, boolean allowIdGeneration) throws ElasticsearchException { + public void process(MetaData metaData, @Nullable MappingMetaData mappingMd, boolean allowIdGeneration, String concreteIndex) throws ElasticsearchException { // resolve the routing if needed - routing(metaData.resolveIndexRouting(routing, aliasOrIndex)); + routing(metaData.resolveIndexRouting(routing, index)); // resolve timestamp if provided externally if (timestamp != null) { timestamp = MappingMetaData.Timestamp.parseStringTimestamp(timestamp, @@ -592,7 +592,7 @@ public class IndexRequest extends ShardReplicationOperationRequest // might as well check for routing here if (mappingMd.routing().required() && routing == null) { - throw new RoutingMissingException(index, type, id); + throw new RoutingMissingException(concreteIndex, type, id); } if (parent != null && !mappingMd.hasParentField()) { diff --git a/src/main/java/org/elasticsearch/action/index/TransportIndexAction.java b/src/main/java/org/elasticsearch/action/index/TransportIndexAction.java index 25bf9db58d1..c9aa5d8259d 100644 --- a/src/main/java/org/elasticsearch/action/index/TransportIndexAction.java +++ b/src/main/java/org/elasticsearch/action/index/TransportIndexAction.java @@ -32,8 +32,6 @@ import org.elasticsearch.cluster.ClusterService; import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.action.index.MappingUpdatedAction; import org.elasticsearch.cluster.action.shard.ShardStateAction; -import org.elasticsearch.cluster.block.ClusterBlockException; -import org.elasticsearch.cluster.block.ClusterBlockLevel; import org.elasticsearch.cluster.metadata.IndexMetaData; import org.elasticsearch.cluster.metadata.MappingMetaData; import org.elasticsearch.cluster.metadata.MetaData; @@ -111,15 +109,19 @@ public class TransportIndexAction extends TransportShardReplicationOperationActi } @Override - protected boolean resolveRequest(ClusterState state, IndexRequest request, ActionListener indexResponseActionListener) { + protected boolean resolveIndex() { + return true; + } + + @Override + protected boolean resolveRequest(ClusterState state, InternalRequest request, ActionListener indexResponseActionListener) { MetaData metaData = clusterService.state().metaData(); - String aliasOrIndex = request.index(); - request.index(metaData.concreteSingleIndex(request.index(), request.indicesOptions())); + MappingMetaData mappingMd = null; - if (metaData.hasIndex(request.index())) { - mappingMd = metaData.index(request.index()).mappingOrDefault(request.type()); + if (metaData.hasIndex(request.concreteIndex())) { + mappingMd = metaData.index(request.concreteIndex()).mappingOrDefault(request.request().type()); } - request.process(metaData, aliasOrIndex, mappingMd, allowIdGeneration); + request.request().process(metaData, mappingMd, allowIdGeneration, request.concreteIndex()); return true; } @@ -153,19 +155,9 @@ public class TransportIndexAction extends TransportShardReplicationOperationActi } @Override - protected ClusterBlockException checkGlobalBlock(ClusterState state, IndexRequest request) { - return state.blocks().globalBlockedException(ClusterBlockLevel.WRITE); - } - - @Override - protected ClusterBlockException checkRequestBlock(ClusterState state, IndexRequest request) { - return state.blocks().indexBlockedException(ClusterBlockLevel.WRITE, request.index()); - } - - @Override - protected ShardIterator shards(ClusterState clusterState, IndexRequest request) { + protected ShardIterator shards(ClusterState clusterState, InternalRequest request) { return clusterService.operationRouting() - .indexShards(clusterService.state(), request.index(), request.type(), request.id(), request.routing()); + .indexShards(clusterService.state(), request.concreteIndex(), request.request().type(), request.request().id(), request.request().routing()); } @Override @@ -173,16 +165,16 @@ public class TransportIndexAction extends TransportShardReplicationOperationActi final IndexRequest request = shardRequest.request; // validate, if routing is required, that we got routing - IndexMetaData indexMetaData = clusterState.metaData().index(request.index()); + IndexMetaData indexMetaData = clusterState.metaData().index(shardRequest.shardId.getIndex()); MappingMetaData mappingMd = indexMetaData.mappingOrDefault(request.type()); if (mappingMd != null && mappingMd.routing().required()) { if (request.routing() == null) { - throw new RoutingMissingException(request.index(), request.type(), request.id()); + throw new RoutingMissingException(shardRequest.shardId.getIndex(), request.type(), request.id()); } } - IndexService indexService = indicesService.indexServiceSafe(shardRequest.request.index()); - IndexShard indexShard = indexService.shardSafe(shardRequest.shardId); + IndexService indexService = indicesService.indexServiceSafe(shardRequest.shardId.getIndex()); + IndexShard indexShard = indexService.shardSafe(shardRequest.shardId.id()); SourceToParse sourceToParse = SourceToParse.source(SourceToParse.Origin.PRIMARY, request.source()).type(request.type()).id(request.id()) .routing(request.routing()).parent(request.parent()).timestamp(request.timestamp()).ttl(request.ttl()); long version; @@ -191,7 +183,7 @@ public class TransportIndexAction extends TransportShardReplicationOperationActi if (request.opType() == IndexRequest.OpType.INDEX) { Engine.Index index = indexShard.prepareIndex(sourceToParse, request.version(), request.versionType(), Engine.Operation.Origin.PRIMARY, request.canHaveDuplicates()); if (index.parsedDoc().mappingsModified()) { - mappingUpdatedAction.updateMappingOnMaster(request.index(), index.docMapper(), indexService.indexUUID()); + mappingUpdatedAction.updateMappingOnMaster(shardRequest.shardId.getIndex(), index.docMapper(), indexService.indexUUID()); } indexShard.index(index); version = index.version(); @@ -201,7 +193,7 @@ public class TransportIndexAction extends TransportShardReplicationOperationActi Engine.Create create = indexShard.prepareCreate(sourceToParse, request.version(), request.versionType(), Engine.Operation.Origin.PRIMARY, request.canHaveDuplicates(), request.autoGeneratedId()); if (create.parsedDoc().mappingsModified()) { - mappingUpdatedAction.updateMappingOnMaster(request.index(), create.docMapper(), indexService.indexUUID()); + mappingUpdatedAction.updateMappingOnMaster(shardRequest.shardId.getIndex(), create.docMapper(), indexService.indexUUID()); } indexShard.create(create); version = create.version(); @@ -222,13 +214,13 @@ public class TransportIndexAction extends TransportShardReplicationOperationActi assert request.versionType().validateVersionForWrites(request.version()); - IndexResponse response = new IndexResponse(request.index(), request.type(), request.id(), version, created); + IndexResponse response = new IndexResponse(shardRequest.shardId.getIndex(), request.type(), request.id(), version, created); return new PrimaryResponse<>(shardRequest.request, response, op); } @Override protected void shardOperationOnReplica(ReplicaOperationRequest shardRequest) { - IndexShard indexShard = indicesService.indexServiceSafe(shardRequest.request.index()).shardSafe(shardRequest.shardId); + IndexShard indexShard = indicesService.indexServiceSafe(shardRequest.shardId.getIndex()).shardSafe(shardRequest.shardId.id()); IndexRequest request = shardRequest.request; SourceToParse sourceToParse = SourceToParse.source(SourceToParse.Origin.REPLICA, request.source()).type(request.type()).id(request.id()) .routing(request.routing()).parent(request.parent()).timestamp(request.timestamp()).ttl(request.ttl()); diff --git a/src/main/java/org/elasticsearch/action/percolate/TransportMultiPercolateAction.java b/src/main/java/org/elasticsearch/action/percolate/TransportMultiPercolateAction.java index c22ca31bf9e..13b7cde1b9b 100644 --- a/src/main/java/org/elasticsearch/action/percolate/TransportMultiPercolateAction.java +++ b/src/main/java/org/elasticsearch/action/percolate/TransportMultiPercolateAction.java @@ -26,7 +26,6 @@ import org.elasticsearch.action.UnavailableShardsException; import org.elasticsearch.action.get.*; import org.elasticsearch.action.support.ActionFilters; import org.elasticsearch.action.support.HandledTransportAction; -import org.elasticsearch.action.support.TransportAction; import org.elasticsearch.action.support.broadcast.BroadcastShardOperationFailedException; import org.elasticsearch.cluster.ClusterService; import org.elasticsearch.cluster.ClusterState; @@ -41,8 +40,6 @@ import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.indices.IndexMissingException; import org.elasticsearch.percolator.PercolatorService; import org.elasticsearch.threadpool.ThreadPool; -import org.elasticsearch.transport.BaseTransportRequestHandler; -import org.elasticsearch.transport.TransportChannel; import org.elasticsearch.transport.TransportService; import java.util.*; @@ -195,7 +192,7 @@ public class TransportMultiPercolateAction extends HandledTransportAction(request.items.size()); @@ -106,7 +100,7 @@ public class TransportShardMultiPercolateAction extends TransportShardSingleOper if (TransportActions.isShardNotAvailableException(t)) { throw (ElasticsearchException) t; } else { - logger.debug("[{}][{}] failed to multi percolate", t, request.index(), request.shardId()); + logger.debug("{} failed to multi percolate", t, request.shardId()); responseItem = new Response.Item(slot, new StringText(ExceptionsHelper.detailedMessage(t))); } } diff --git a/src/main/java/org/elasticsearch/action/support/replication/TransportShardReplicationOperationAction.java b/src/main/java/org/elasticsearch/action/support/replication/TransportShardReplicationOperationAction.java index 5ebc665709f..68131ecbe9c 100644 --- a/src/main/java/org/elasticsearch/action/support/replication/TransportShardReplicationOperationAction.java +++ b/src/main/java/org/elasticsearch/action/support/replication/TransportShardReplicationOperationAction.java @@ -22,6 +22,7 @@ package org.elasticsearch.action.support.replication; import org.elasticsearch.ElasticsearchException; import org.elasticsearch.ElasticsearchIllegalStateException; import org.elasticsearch.ExceptionsHelper; +import org.elasticsearch.Version; import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.ActionResponse; import org.elasticsearch.action.UnavailableShardsException; @@ -34,6 +35,7 @@ import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.ClusterStateObserver; import org.elasticsearch.cluster.action.shard.ShardStateAction; import org.elasticsearch.cluster.block.ClusterBlockException; +import org.elasticsearch.cluster.block.ClusterBlockLevel; import org.elasticsearch.cluster.metadata.IndexMetaData; import org.elasticsearch.cluster.node.DiscoveryNode; import org.elasticsearch.cluster.routing.ShardIterator; @@ -42,12 +44,12 @@ import org.elasticsearch.cluster.routing.ShardRoutingState; import org.elasticsearch.common.Nullable; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; -import org.elasticsearch.common.io.stream.Streamable; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.util.concurrent.AbstractRunnable; import org.elasticsearch.index.engine.VersionConflictEngineException; import org.elasticsearch.index.service.IndexService; +import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.index.shard.service.IndexShard; import org.elasticsearch.indices.IndicesService; import org.elasticsearch.node.NodeClosedException; @@ -117,24 +119,29 @@ public abstract class TransportShardReplicationOperationAction response) { + protected void postPrimaryOperation(InternalRequest request, PrimaryResponse response) { } - protected abstract ShardIterator shards(ClusterState clusterState, Request request) throws ElasticsearchException; + protected abstract ShardIterator shards(ClusterState clusterState, InternalRequest request) throws ElasticsearchException; protected abstract boolean checkWriteConsistency(); - protected abstract ClusterBlockException checkGlobalBlock(ClusterState state, Request request); + protected ClusterBlockException checkGlobalBlock(ClusterState state) { + return state.blocks().globalBlockedException(ClusterBlockLevel.WRITE); + } - protected abstract ClusterBlockException checkRequestBlock(ClusterState state, Request request); + protected ClusterBlockException checkRequestBlock(ClusterState state, InternalRequest request) { + return state.blocks().indexBlockedException(ClusterBlockLevel.WRITE, request.concreteIndex()); + } + + protected abstract boolean resolveIndex(); /** - * Resolves the request, by default, simply setting the concrete index (if its aliased one). If the resolve + * Resolves the request, by default doing nothing. If the resolve * means a different execution, then return false here to indicate not to continue and execute this request. */ - protected boolean resolveRequest(ClusterState state, Request request, ActionListener listener) { - request.index(state.metaData().concreteSingleIndex(request.index(), request.indicesOptions())); + protected boolean resolveRequest(ClusterState state, InternalRequest request, ActionListener listener) { return true; } @@ -237,50 +244,32 @@ public abstract class TransportShardReplicationOperationAction= 0; + //older nodes will send the concrete index as part of the request + shardId = new ShardId(request.index(), shard); + } } @Override public void writeTo(StreamOutput out) throws IOException { super.writeTo(out); - out.writeVInt(shardId); + if (out.getVersion().onOrAfter(Version.V_1_4_0)) { + shardId.writeTo(out); + } else { + out.writeVInt(shardId.id()); + //older nodes expect the concrete index as part of the request + request.index(shardId.getIndex()); + } request.writeTo(out); } } protected class AsyncShardOperationAction { - private final ActionListener listener; - private final Request request; + private final InternalRequest internalRequest; private volatile ShardIterator shardIt; private final AtomicBoolean primaryOperationStarted = new AtomicBoolean(); private final ReplicationType replicationType; private volatile ClusterStateObserver observer; AsyncShardOperationAction(Request request, ActionListener listener) { - this.request = request; + this.internalRequest = new InternalRequest(request); this.listener = listener; if (request.replicationType() != ReplicationType.DEFAULT) { @@ -323,7 +327,7 @@ public abstract class TransportShardReplicationOperationAction 2) { @@ -413,8 +422,8 @@ public abstract class TransportShardReplicationOperationAction() { + transportService.sendRequest(node, actionName, internalRequest.request(), transportOptions, new BaseTransportResponseHandler() { @Override public Response newInstance() { @@ -483,8 +492,8 @@ public abstract class TransportShardReplicationOperationAction response = shardOperationOnPrimary(clusterState, new PrimaryOperationRequest(primaryShardId, request)); + PrimaryResponse response = shardOperationOnPrimary(clusterState, new PrimaryOperationRequest(primaryShardId, internalRequest.concreteIndex(), internalRequest.request())); performReplicas(response); } catch (Throwable e) { // shard has not been allocated yet, retry it here @@ -532,11 +541,11 @@ public abstract class TransportShardReplicationOperationAction response) { if (ignoreReplicas()) { - postPrimaryOperation(request, response); + postPrimaryOperation(internalRequest, response); listener.onResponse(response.response()); return; } @@ -573,7 +582,7 @@ public abstract class TransportShardReplicationOperationAction listener; private final ShardsIterator shardsIt; - private final Request request; + private final InternalRequest internalRequest; private final DiscoveryNodes nodes; private AsyncSingleAction(Request request, ActionListener listener) { - this.request = request; this.listener = listener; ClusterState clusterState = clusterService.state(); @@ -110,11 +117,20 @@ public abstract class TransportSingleCustomOperationAction() { + transportService.sendRequest(node, transportShardAction, new ShardSingleOperationRequest(internalRequest.request(), shard.shardId()), new BaseTransportResponseHandler() { @Override public Response newInstance() { return newResponse(); @@ -294,12 +310,12 @@ public abstract class TransportSingleCustomOperationAction listener) throws ElasticsearchException; + protected abstract void shardOperation(InternalRequest request, ActionListener listener) throws ElasticsearchException; protected abstract Request newRequest(); protected abstract Response newResponse(); - protected abstract ClusterBlockException checkGlobalBlock(ClusterState state, Request request); - - protected abstract ClusterBlockException checkRequestBlock(ClusterState state, Request request); - - /** - * Resolves the request, by default, simply setting the concrete index (if its aliased one). If the resolve - * means a different execution, then return false here to indicate not to continue and execute this request. - */ - protected boolean resolveRequest(ClusterState state, Request request, ActionListener listener) { - request.index(state.metaData().concreteSingleIndex(request.index(), request.indicesOptions())); - return true; + protected ClusterBlockException checkGlobalBlock(ClusterState state) { + return state.blocks().globalBlockedException(ClusterBlockLevel.WRITE); } + protected ClusterBlockException checkRequestBlock(ClusterState state, InternalRequest request) { + return state.blocks().indexBlockedException(ClusterBlockLevel.WRITE, request.concreteIndex()); + } + /** + * Resolves the request. If the resolve means a different execution, then return false + * here to indicate not to continue and execute this request. + */ + protected abstract boolean resolveRequest(ClusterState state, InternalRequest request, ActionListener listener); + protected boolean retryOnFailure(Throwable e) { return false; } @@ -101,36 +102,31 @@ public abstract class TransportInstanceSingleOperationAction listener; - - private final Request request; - + private final InternalRequest internalRequest; + private volatile ClusterStateObserver observer; private ShardIterator shardIt; - private DiscoveryNodes nodes; - private final AtomicBoolean operationStarted = new AtomicBoolean(); - private volatile ClusterStateObserver observer; - private AsyncSingleAction(Request request, ActionListener listener) { - this.request = request; + this.internalRequest = new InternalRequest(request); this.listener = listener; } public void start() { - observer = new ClusterStateObserver(clusterService, request.timeout(), logger); + this.observer = new ClusterStateObserver(clusterService, internalRequest.request().timeout(), logger); doStart(); } protected boolean doStart() throws ElasticsearchException { nodes = observer.observedState().nodes(); try { - ClusterBlockException blockException = checkGlobalBlock(observer.observedState(), request); + ClusterBlockException blockException = checkGlobalBlock(observer.observedState()); if (blockException != null) { if (blockException.retryable()) { retry(blockException); @@ -139,11 +135,12 @@ public abstract class TransportInstanceSingleOperationAction() { + transportService.sendRequest(node, actionName, internalRequest.request(), transportOptions(), new BaseTransportResponseHandler() { @Override public Response newInstance() { @@ -251,7 +248,7 @@ public abstract class TransportInstanceSingleOperationAction listener; private final ShardIterator shardIt; - private final Request request; + private final InternalRequest internalRequest; private final DiscoveryNodes nodes; private volatile Throwable lastFailure; private AsyncSingleAction(Request request, ActionListener listener) { - this.request = request; this.listener = listener; ClusterState clusterState = clusterService.state(); @@ -108,17 +116,26 @@ public abstract class TransportShardSingleOperationAction() { + transportService.sendRequest(node, transportShardAction, new ShardSingleOperationRequest(internalRequest.request(), shardRouting.shardId()), new BaseTransportResponseHandler() { @Override public Response newInstance() { @@ -274,12 +291,12 @@ public abstract class TransportShardSingleOperationAction listener) { - MetaData metaData = clusterService.state().metaData(); - String aliasOrIndex = request.index(); - request.routing((metaData.resolveIndexRouting(request.routing(), aliasOrIndex))); - request.index(metaData.concreteSingleIndex(request.index(), request.indicesOptions())); - + protected boolean resolveRequest(ClusterState state, InternalRequest request, ActionListener listener) { + request.request().routing((state.metaData().resolveIndexRouting(request.request().routing(), request.request().index()))); // Fail fast on the node that received the request, rather than failing when translating on the index or delete request. - if (request.routing() == null && state.getMetaData().routingRequired(request.index(), request.type())) { - throw new RoutingMissingException(request.index(), request.type(), request.id()); + if (request.request().routing() == null && state.getMetaData().routingRequired(request.concreteIndex(), request.request().type())) { + throw new RoutingMissingException(request.concreteIndex(), request.request().type(), request.request().id()); } return true; } @@ -167,12 +150,12 @@ public class TransportUpdateAction extends TransportInstanceSingleOperationActio } @Override - protected ShardIterator shards(ClusterState clusterState, UpdateRequest request) throws ElasticsearchException { - if (request.shardId() != -1) { - return clusterState.routingTable().index(request.index()).shard(request.shardId()).primaryShardIt(); + protected ShardIterator shards(ClusterState clusterState, InternalRequest request) throws ElasticsearchException { + if (request.request().shardId() != -1) { + return clusterState.routingTable().index(request.concreteIndex()).shard(request.request().shardId()).primaryShardIt(); } ShardIterator shardIterator = clusterService.operationRouting() - .indexShards(clusterState, request.index(), request.type(), request.id(), request.routing()); + .indexShards(clusterState, request.concreteIndex(), request.request().type(), request.request().id(), request.request().routing()); ShardRouting shard; while ((shard = shardIterator.nextOrNull()) != null) { if (shard.primary()) { @@ -183,12 +166,14 @@ public class TransportUpdateAction extends TransportInstanceSingleOperationActio } @Override - protected void shardOperation(final UpdateRequest request, final ActionListener listener) throws ElasticsearchException { + protected void shardOperation(final InternalRequest request, final ActionListener listener) throws ElasticsearchException { shardOperation(request, listener, 0); } - protected void shardOperation(final UpdateRequest request, final ActionListener listener, final int retryCount) throws ElasticsearchException { - final UpdateHelper.Result result = updateHelper.prepare(request); + protected void shardOperation(final InternalRequest request, final ActionListener listener, final int retryCount) throws ElasticsearchException { + IndexService indexService = indicesService.indexServiceSafe(request.concreteIndex()); + IndexShard indexShard = indexService.shardSafe(request.request().shardId()); + final UpdateHelper.Result result = updateHelper.prepare(request.request(), indexShard); switch (result.operation()) { case UPSERT: IndexRequest upsertRequest = result.action(); @@ -198,9 +183,9 @@ public class TransportUpdateAction extends TransportInstanceSingleOperationActio @Override public void onResponse(IndexResponse response) { UpdateResponse update = new UpdateResponse(response.getIndex(), response.getType(), response.getId(), response.getVersion(), response.isCreated()); - if (request.fields() != null && request.fields().length > 0) { + if (request.request().fields() != null && request.request().fields().length > 0) { Tuple> sourceAndContent = XContentHelper.convertToMap(upsertSourceBytes, true); - update.setGetResult(updateHelper.extractGetResult(request, response.getVersion(), sourceAndContent.v2(), sourceAndContent.v1(), upsertSourceBytes)); + update.setGetResult(updateHelper.extractGetResult(request.request(), request.concreteIndex(), response.getVersion(), sourceAndContent.v2(), sourceAndContent.v1(), upsertSourceBytes)); } else { update.setGetResult(null); } @@ -211,7 +196,7 @@ public class TransportUpdateAction extends TransportInstanceSingleOperationActio public void onFailure(Throwable e) { e = ExceptionsHelper.unwrapCause(e); if (e instanceof VersionConflictEngineException || e instanceof DocumentAlreadyExistsException) { - if (retryCount < request.retryOnConflict()) { + if (retryCount < request.request().retryOnConflict()) { threadPool.executor(executor()).execute(new ActionRunnable(listener) { @Override protected void doRun() { @@ -233,7 +218,7 @@ public class TransportUpdateAction extends TransportInstanceSingleOperationActio @Override public void onResponse(IndexResponse response) { UpdateResponse update = new UpdateResponse(response.getIndex(), response.getType(), response.getId(), response.getVersion(), response.isCreated()); - update.setGetResult(updateHelper.extractGetResult(request, response.getVersion(), result.updatedSourceAsMap(), result.updateSourceContentType(), indexSourceBytes)); + update.setGetResult(updateHelper.extractGetResult(request.request(), request.concreteIndex(), response.getVersion(), result.updatedSourceAsMap(), result.updateSourceContentType(), indexSourceBytes)); listener.onResponse(update); } @@ -241,7 +226,7 @@ public class TransportUpdateAction extends TransportInstanceSingleOperationActio public void onFailure(Throwable e) { e = ExceptionsHelper.unwrapCause(e); if (e instanceof VersionConflictEngineException) { - if (retryCount < request.retryOnConflict()) { + if (retryCount < request.request().retryOnConflict()) { threadPool.executor(executor()).execute(new ActionRunnable(listener) { @Override protected void doRun() { @@ -261,7 +246,7 @@ public class TransportUpdateAction extends TransportInstanceSingleOperationActio @Override public void onResponse(DeleteResponse response) { UpdateResponse update = new UpdateResponse(response.getIndex(), response.getType(), response.getId(), response.getVersion(), false); - update.setGetResult(updateHelper.extractGetResult(request, response.getVersion(), result.updatedSourceAsMap(), result.updateSourceContentType(), null)); + update.setGetResult(updateHelper.extractGetResult(request.request(), request.concreteIndex(), response.getVersion(), result.updatedSourceAsMap(), result.updateSourceContentType(), null)); listener.onResponse(update); } @@ -269,7 +254,7 @@ public class TransportUpdateAction extends TransportInstanceSingleOperationActio public void onFailure(Throwable e) { e = ExceptionsHelper.unwrapCause(e); if (e instanceof VersionConflictEngineException) { - if (retryCount < request.retryOnConflict()) { + if (retryCount < request.request().retryOnConflict()) { threadPool.executor(executor()).execute(new ActionRunnable(listener) { @Override protected void doRun() { @@ -286,10 +271,9 @@ public class TransportUpdateAction extends TransportInstanceSingleOperationActio case NONE: UpdateResponse update = result.action(); listener.onResponse(update); - IndexService indexService = indicesService.indexService(request.index()); - if (indexService != null) { - IndexShard indexShard = indexService.shard(request.shardId()); - indexShard.indexingService().noopUpdate(request.type()); + IndexService indexServiceOrNull = indicesService.indexService(request.concreteIndex()); + if (indexServiceOrNull != null) { + indexService.shard(request.request().shardId()).indexingService().noopUpdate(request.request().type()); } break; default: diff --git a/src/main/java/org/elasticsearch/action/update/UpdateHelper.java b/src/main/java/org/elasticsearch/action/update/UpdateHelper.java index a1bad51557d..a38285d9f14 100644 --- a/src/main/java/org/elasticsearch/action/update/UpdateHelper.java +++ b/src/main/java/org/elasticsearch/action/update/UpdateHelper.java @@ -41,10 +41,8 @@ import org.elasticsearch.index.get.GetResult; import org.elasticsearch.index.mapper.internal.ParentFieldMapper; import org.elasticsearch.index.mapper.internal.RoutingFieldMapper; import org.elasticsearch.index.mapper.internal.TTLFieldMapper; -import org.elasticsearch.index.service.IndexService; import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.index.shard.service.IndexShard; -import org.elasticsearch.indices.IndicesService; import org.elasticsearch.script.ExecutableScript; import org.elasticsearch.script.ScriptService; import org.elasticsearch.search.fetch.source.FetchSourceContext; @@ -61,25 +59,18 @@ import static com.google.common.collect.Maps.newHashMapWithExpectedSize; */ public class UpdateHelper extends AbstractComponent { - private final IndicesService indicesService; private final ScriptService scriptService; @Inject - public UpdateHelper(Settings settings, IndicesService indicesService, ScriptService scriptService) { + public UpdateHelper(Settings settings, ScriptService scriptService) { super(settings); - this.indicesService = indicesService; this.scriptService = scriptService; } /** * Prepares an update request by converting it into an index or delete request or an update response (no action). */ - public Result prepare(UpdateRequest request) { - IndexService indexService = indicesService.indexServiceSafe(request.index()); - IndexShard indexShard = indexService.shardSafe(request.shardId()); - return prepare(request, indexShard); - } - + @SuppressWarnings("unchecked") public Result prepare(UpdateRequest request, IndexShard indexShard) { long getDate = System.currentTimeMillis(); final GetResult getResult = indexShard.getService().get(request.type(), request.id(), @@ -88,7 +79,7 @@ public class UpdateHelper extends AbstractComponent { if (!getResult.isExists()) { if (request.upsertRequest() == null && !request.docAsUpsert()) { - throw new DocumentMissingException(new ShardId(request.index(), request.shardId()), request.type(), request.id()); + throw new DocumentMissingException(new ShardId(indexShard.indexService().index().name(), request.shardId()), request.type(), request.id()); } Long ttl = null; IndexRequest indexRequest = request.docAsUpsert() ? request.doc() : request.upsertRequest(); @@ -152,12 +143,12 @@ public class UpdateHelper extends AbstractComponent { if (getResult.internalSourceRef() == null) { // no source, we can't do nothing, through a failure... - throw new DocumentSourceMissingException(new ShardId(request.index(), request.shardId()), request.type(), request.id()); + throw new DocumentSourceMissingException(new ShardId(indexShard.indexService().index().name(), request.shardId()), request.type(), request.id()); } Tuple> sourceAndContent = XContentHelper.convertToMap(getResult.internalSourceRef(), true); String operation = null; - String timestamp = null; + String timestamp; Long ttl = null; final Map updatedSourceAsMap; final XContentType updateSourceContentType = sourceAndContent.v1(); @@ -232,7 +223,7 @@ public class UpdateHelper extends AbstractComponent { return new Result(deleteRequest, Operation.DELETE, updatedSourceAsMap, updateSourceContentType); } else if ("none".equals(operation)) { UpdateResponse update = new UpdateResponse(getResult.getIndex(), getResult.getType(), getResult.getId(), getResult.getVersion(), false); - update.setGetResult(extractGetResult(request, getResult.getVersion(), updatedSourceAsMap, updateSourceContentType, getResult.internalSourceRef())); + update.setGetResult(extractGetResult(request, indexShard.indexService().index().name(), getResult.getVersion(), updatedSourceAsMap, updateSourceContentType, getResult.internalSourceRef())); return new Result(update, Operation.NONE, updatedSourceAsMap, updateSourceContentType); } else { logger.warn("Used update operation [{}] for script [{}], doing nothing...", operation, request.script); @@ -257,7 +248,7 @@ public class UpdateHelper extends AbstractComponent { /** * Extracts the fields from the updated document to be returned in a update response */ - public GetResult extractGetResult(final UpdateRequest request, long version, final Map source, XContentType sourceContentType, @Nullable final BytesReference sourceAsBytes) { + public GetResult extractGetResult(final UpdateRequest request, String concreteIndex, long version, final Map source, XContentType sourceContentType, @Nullable final BytesReference sourceAsBytes) { if (request.fields() == null || request.fields().length == 0) { return null; } @@ -287,7 +278,7 @@ public class UpdateHelper extends AbstractComponent { } // TODO when using delete/none, we can still return the source as bytes by generating it (using the sourceContentType) - return new GetResult(request.index(), request.type(), request.id(), version, true, sourceRequested ? sourceAsBytes : null, fields); + return new GetResult(concreteIndex, request.type(), request.id(), version, true, sourceRequested ? sourceAsBytes : null, fields); } public static class Result { diff --git a/src/main/java/org/elasticsearch/index/termvectors/ShardTermVectorService.java b/src/main/java/org/elasticsearch/index/termvectors/ShardTermVectorService.java index c225bfcbeab..a05fdc88d30 100644 --- a/src/main/java/org/elasticsearch/index/termvectors/ShardTermVectorService.java +++ b/src/main/java/org/elasticsearch/index/termvectors/ShardTermVectorService.java @@ -63,10 +63,10 @@ public class ShardTermVectorService extends AbstractIndexShardComponent { return this; } - public TermVectorResponse getTermVector(TermVectorRequest request) { + public TermVectorResponse getTermVector(TermVectorRequest request, String concreteIndex) { final Engine.Searcher searcher = indexShard.acquireSearcher("term_vector"); IndexReader topLevelReader = searcher.reader(); - final TermVectorResponse termVectorResponse = new TermVectorResponse(request.index(), request.type(), request.id()); + final TermVectorResponse termVectorResponse = new TermVectorResponse(concreteIndex, request.type(), request.id()); final Term uidTerm = new Term(UidFieldMapper.NAME, Uid.createUidAsBytes(request.type(), request.id())); try { Fields topLevelFields = MultiFields.getFields(topLevelReader); diff --git a/src/test/java/org/elasticsearch/action/termvector/AbstractTermVectorTests.java b/src/test/java/org/elasticsearch/action/termvector/AbstractTermVectorTests.java index 77c0ff963ed..bf42e0c0bb7 100644 --- a/src/test/java/org/elasticsearch/action/termvector/AbstractTermVectorTests.java +++ b/src/test/java/org/elasticsearch/action/termvector/AbstractTermVectorTests.java @@ -37,6 +37,7 @@ import org.apache.lucene.search.TopDocs; import org.apache.lucene.store.Directory; import org.apache.lucene.store.RAMDirectory; import org.elasticsearch.Version; +import org.elasticsearch.action.admin.indices.alias.Alias; import org.elasticsearch.common.inject.internal.Join; import org.elasticsearch.common.settings.ImmutableSettings; import org.elasticsearch.common.xcontent.XContentBuilder; @@ -114,6 +115,7 @@ public abstract class AbstractTermVectorTests extends ElasticsearchIntegrationTe final public TestFieldSetting[] fieldSettings; final public String[] fieldContent; public String index = "test"; + public String alias = "alias"; public String type = "type1"; public TestDoc(String id, TestFieldSetting[] fieldSettings, String[] fieldContent) { @@ -128,6 +130,11 @@ public abstract class AbstractTermVectorTests extends ElasticsearchIntegrationTe return this; } + public TestDoc alias(String alias) { + this.alias = alias; + return this; + } + @Override public String toString() { @@ -181,7 +188,7 @@ public abstract class AbstractTermVectorTests extends ElasticsearchIntegrationTe } } - protected void createIndexBasedOnFieldSettings(String index, TestFieldSetting[] fieldSettings) throws IOException { + protected void createIndexBasedOnFieldSettings(String index, String alias, TestFieldSetting[] fieldSettings) throws IOException { XContentBuilder mappingBuilder = jsonBuilder(); mappingBuilder.startObject().startObject("type1").startObject("properties"); for (TestFieldSetting field : fieldSettings) { @@ -192,7 +199,7 @@ public abstract class AbstractTermVectorTests extends ElasticsearchIntegrationTe .put(indexSettings()) .put("index.analysis.analyzer.tv_test.tokenizer", "standard") .putArray("index.analysis.analyzer.tv_test.filter", "type_as_payload", "lowercase"); - assertAcked(prepareCreate(index).addMapping("type1", mappingBuilder).setSettings(settings)); + assertAcked(prepareCreate(index).addMapping("type1", mappingBuilder).setSettings(settings).addAlias(new Alias(alias))); ensureYellow(); } @@ -253,12 +260,12 @@ public abstract class AbstractTermVectorTests extends ElasticsearchIntegrationTe configs.add(config); } // always adds a test that fails - configs.add(new TestConfig(new TestDoc("doesnt_exist", new TestFieldSetting[]{}, new String[]{}).index("doesn't_exist"), + configs.add(new TestConfig(new TestDoc("doesnt_exist", new TestFieldSetting[]{}, new String[]{}).index("doesn't_exist").alias("doesn't_exist"), new String[]{"doesnt_exist"}, true, true, true).expectedException(IndexMissingException.class)); refresh(); - return configs.toArray(new TestConfig[]{}); + return configs.toArray(new TestConfig[configs.size()]); } protected TestFieldSetting[] getFieldSettings() { @@ -267,12 +274,10 @@ public abstract class AbstractTermVectorTests extends ElasticsearchIntegrationTe new TestFieldSetting("field_with_only_tv", false, false, false), new TestFieldSetting("field_with_positions_offsets", false, false, true), new TestFieldSetting("field_with_positions_payloads", false, true, true) - }; } protected DirectoryReader indexDocsWithLucene(TestDoc[] testDocs) throws IOException { - Map mapping = new HashMap<>(); for (TestFieldSetting field : testDocs[0].fieldSettings) { if (field.storedPayloads) { @@ -319,6 +324,7 @@ public abstract class AbstractTermVectorTests extends ElasticsearchIntegrationTe } protected void validateResponse(TermVectorResponse esResponse, Fields luceneFields, TestConfig testConfig) throws IOException { + assertThat(esResponse.getIndex(), equalTo(testConfig.doc.index)); TestDoc testDoc = testConfig.doc; HashSet selectedFields = testConfig.selectedFields == null ? null : new HashSet<>( Arrays.asList(testConfig.selectedFields)); @@ -380,21 +386,16 @@ public abstract class AbstractTermVectorTests extends ElasticsearchIntegrationTe } else { assertThat("Missing payload test failed" + failDesc, esDocsPosEnum.getPayload(), equalTo(null)); } - } } - assertNull("Es returned terms are done but lucene isn't", luceneTermEnum.next()); - } - } protected TermVectorRequestBuilder getRequestForConfig(TestConfig config) { - return client().prepareTermVector(config.doc.index, config.doc.type, config.doc.id).setPayloads(config.requestPayloads) + return client().prepareTermVector(randomBoolean() ? config.doc.index : config.doc.alias, config.doc.type, config.doc.id).setPayloads(config.requestPayloads) .setOffsets(config.requestOffsets).setPositions(config.requestPositions).setFieldStatistics(true).setTermStatistics(true) .setSelectedFields(config.selectedFields); - } protected Fields getTermVectorsFromLucene(DirectoryReader directoryReader, TestDoc doc) throws IOException { @@ -405,5 +406,4 @@ public abstract class AbstractTermVectorTests extends ElasticsearchIntegrationTe assertEquals(1, scoreDocs.length); return directoryReader.getTermVectors(scoreDocs[0].doc); } - } diff --git a/src/test/java/org/elasticsearch/action/termvector/GetTermVectorTests.java b/src/test/java/org/elasticsearch/action/termvector/GetTermVectorTests.java index fb4437d46c3..df7d5321c1d 100644 --- a/src/test/java/org/elasticsearch/action/termvector/GetTermVectorTests.java +++ b/src/test/java/org/elasticsearch/action/termvector/GetTermVectorTests.java @@ -26,6 +26,7 @@ import org.apache.lucene.index.*; import org.apache.lucene.util.BytesRef; import org.elasticsearch.ElasticsearchException; import org.elasticsearch.action.ActionFuture; +import org.elasticsearch.action.admin.indices.alias.Alias; import org.elasticsearch.action.index.IndexRequestBuilder; import org.elasticsearch.common.settings.ImmutableSettings; import org.elasticsearch.common.xcontent.XContentBuilder; @@ -57,16 +58,17 @@ public class GetTermVectorTests extends AbstractTermVectorTests { .endObject() .endObject() .endObject().endObject(); - assertAcked(prepareCreate("test").addMapping("type1", mapping)); + assertAcked(prepareCreate("test").addAlias(new Alias("alias")).addMapping("type1", mapping)); ensureYellow(); client().prepareIndex("test", "type1", "666").setSource("field", "foo bar").execute().actionGet(); refresh(); for (int i = 0; i < 20; i++) { - ActionFuture termVector = client().termVector(new TermVectorRequest("test", "type1", "" + i)); + ActionFuture termVector = client().termVector(new TermVectorRequest(indexOrAlias(), "type1", "" + i)); TermVectorResponse actionGet = termVector.actionGet(); assertThat(actionGet, notNullValue()); + assertThat(actionGet.getIndex(), equalTo("test")); assertThat(actionGet.isExists(), equalTo(false)); } } @@ -81,7 +83,7 @@ public class GetTermVectorTests extends AbstractTermVectorTests { .endObject() .endObject() .endObject().endObject(); - assertAcked(prepareCreate("test").addMapping("type1", mapping)); + assertAcked(prepareCreate("test").addAlias(new Alias("alias")).addMapping("type1", mapping)); ensureYellow(); @@ -89,13 +91,14 @@ public class GetTermVectorTests extends AbstractTermVectorTests { // vectors will be null client().prepareIndex("test", "type1", "0").setSource("existingfield", "?").execute().actionGet(); refresh(); - ActionFuture termVector = client().termVector(new TermVectorRequest("test", "type1", "0") + ActionFuture termVector = client().termVector(new TermVectorRequest(indexOrAlias(), "type1", "0") .selectedFields(new String[]{"existingfield"})); // lets see if the null term vectors are caught... TermVectorResponse actionGet = termVector.actionGet(); assertThat(actionGet, notNullValue()); assertThat(actionGet.isExists(), equalTo(true)); + assertThat(actionGet.getIndex(), equalTo("test")); assertThat(actionGet.getFields().terms("existingfield"), nullValue()); } @@ -109,7 +112,7 @@ public class GetTermVectorTests extends AbstractTermVectorTests { .endObject() .endObject() .endObject().endObject(); - assertAcked(prepareCreate("test").addMapping("type1", mapping)); + assertAcked(prepareCreate("test").addAlias(new Alias("alias")).addMapping("type1", mapping)); ensureYellow(); @@ -117,26 +120,29 @@ public class GetTermVectorTests extends AbstractTermVectorTests { // vectors will be null client().prepareIndex("test", "type1", "0").setSource("anotherexistingfield", 1).execute().actionGet(); refresh(); - ActionFuture termVector = client().termVector(new TermVectorRequest("test", "type1", "0") + ActionFuture termVector = client().termVector(new TermVectorRequest(indexOrAlias(), "type1", "0") .selectedFields(new String[]{"existingfield"})); // lets see if the null term vectors are caught... TermVectorResponse actionGet = termVector.actionGet(); assertThat(actionGet, notNullValue()); assertThat(actionGet.isExists(), equalTo(true)); + assertThat(actionGet.getIndex(), equalTo("test")); assertThat(actionGet.getFields().terms("existingfield"), nullValue()); } @Test public void testNotIndexedField() throws Exception { // must be of type string and indexed. - assertAcked(prepareCreate("test").addMapping("type1", - "field0", "type=integer,", // no tvs - "field1", "type=string,index=no", // no tvs - "field2", "type=string,index=no,store=yes", // no tvs - "field3", "type=string,index=no,term_vector=yes", // no tvs - "field4", "type=string,index=not_analyzed", // yes tvs - "field5", "type=string,index=analyzed")); // yes tvs + assertAcked(prepareCreate("test") + .addAlias(new Alias("alias")) + .addMapping("type1", + "field0", "type=integer,", // no tvs + "field1", "type=string,index=no", // no tvs + "field2", "type=string,index=no,store=yes", // no tvs + "field3", "type=string,index=no,term_vector=yes", // no tvs + "field4", "type=string,index=not_analyzed", // yes tvs + "field5", "type=string,index=analyzed")); // yes tvs ensureYellow(); @@ -151,18 +157,19 @@ public class GetTermVectorTests extends AbstractTermVectorTests { indexRandom(true, indexBuilders); for (int i = 0; i < 4; i++) { - TermVectorResponse resp = client().prepareTermVector("test", "type1", String.valueOf(i)) + TermVectorResponse resp = client().prepareTermVector(indexOrAlias(), "type1", String.valueOf(i)) .setSelectedFields("field" + i) .get(); assertThat(resp, notNullValue()); assertThat(resp.isExists(), equalTo(true)); + assertThat(resp.getIndex(), equalTo("test")); assertThat("field" + i + " :", resp.getFields().terms("field" + i), nullValue()); } for (int i = 4; i < 6; i++) { - TermVectorResponse resp = client().prepareTermVector("test", "type1", String.valueOf(i)) - .setSelectedFields("field" + i) - .get(); + TermVectorResponse resp = client().prepareTermVector(indexOrAlias(), "type1", String.valueOf(i)) + .setSelectedFields("field" + i).get(); + assertThat(resp.getIndex(), equalTo("test")); assertThat("field" + i + " :", resp.getFields().terms("field" + i), notNullValue()); } } @@ -179,6 +186,7 @@ public class GetTermVectorTests extends AbstractTermVectorTests { .endObject() .endObject().endObject(); assertAcked(prepareCreate("test").addMapping("type1", mapping) + .addAlias(new Alias("alias")) .setSettings(settingsBuilder() .put(indexSettings()) .put("index.analysis.analyzer.tv_test.tokenizer", "whitespace") @@ -193,9 +201,10 @@ public class GetTermVectorTests extends AbstractTermVectorTests { refresh(); } for (int i = 0; i < 10; i++) { - TermVectorRequestBuilder resp = client().prepareTermVector("test", "type1", Integer.toString(i)).setPayloads(true) + TermVectorRequestBuilder resp = client().prepareTermVector(indexOrAlias(), "type1", Integer.toString(i)).setPayloads(true) .setOffsets(true).setPositions(true).setSelectedFields(); TermVectorResponse response = resp.execute().actionGet(); + assertThat(response.getIndex(), equalTo("test")); assertThat("doc id: " + i + " doesn't exists but should", response.isExists(), equalTo(true)); Fields fields = response.getFields(); assertThat(fields.size(), equalTo(1)); @@ -367,7 +376,7 @@ public class GetTermVectorTests extends AbstractTermVectorTests { @Test public void testDuelESLucene() throws Exception { TestFieldSetting[] testFieldSettings = getFieldSettings(); - createIndexBasedOnFieldSettings("test", testFieldSettings); + createIndexBasedOnFieldSettings("test", "alias", testFieldSettings); //we generate as many docs as many shards we have TestDoc[] testDocs = generateTestDocs(getNumShards("test").numPrimaries, testFieldSettings); @@ -764,14 +773,19 @@ public class GetTermVectorTests extends AbstractTermVectorTests { source.endObject(); mapping.endObject().endObject().endObject(); - assertAcked(prepareCreate("test").addMapping("type1", mapping)); + assertAcked(prepareCreate("test").addAlias(new Alias("alias")).addMapping("type1", mapping)); ensureGreen(); client().prepareIndex("test", "type1", "0").setSource(source).get(); refresh(); - TermVectorResponse response = client().prepareTermVector("test", "type1", "0").setSelectedFields("field*").get(); + TermVectorResponse response = client().prepareTermVector(indexOrAlias(), "type1", "0").setSelectedFields("field*").get(); assertThat("Doc doesn't exists but should", response.isExists(), equalTo(true)); + assertThat(response.getIndex(), equalTo("test")); assertThat("All term vectors should have been generated", response.getFields().size(), equalTo(numFields)); } + + private static String indexOrAlias() { + return randomBoolean() ? "test" : "alias"; + } } diff --git a/src/test/java/org/elasticsearch/action/termvector/MultiTermVectorsTests.java b/src/test/java/org/elasticsearch/action/termvector/MultiTermVectorsTests.java index cef544b65dd..8db97e9f7ae 100644 --- a/src/test/java/org/elasticsearch/action/termvector/MultiTermVectorsTests.java +++ b/src/test/java/org/elasticsearch/action/termvector/MultiTermVectorsTests.java @@ -30,7 +30,7 @@ public class MultiTermVectorsTests extends AbstractTermVectorTests { @Test public void testDuelESLucene() throws Exception { AbstractTermVectorTests.TestFieldSetting[] testFieldSettings = getFieldSettings(); - createIndexBasedOnFieldSettings("test", testFieldSettings); + createIndexBasedOnFieldSettings("test", "alias", testFieldSettings); //we generate as many docs as many shards we have TestDoc[] testDocs = generateTestDocs(getNumShards("test").numPrimaries, testFieldSettings); diff --git a/src/test/java/org/elasticsearch/bwcompat/BasicBackwardsCompatibilityTest.java b/src/test/java/org/elasticsearch/bwcompat/BasicBackwardsCompatibilityTest.java index 7dae37f98bb..fdb36e3fec0 100644 --- a/src/test/java/org/elasticsearch/bwcompat/BasicBackwardsCompatibilityTest.java +++ b/src/test/java/org/elasticsearch/bwcompat/BasicBackwardsCompatibilityTest.java @@ -20,21 +20,29 @@ package org.elasticsearch.bwcompat; import com.carrotsearch.randomizedtesting.LifecycleScope; import com.carrotsearch.randomizedtesting.generators.RandomPicks; +import org.apache.lucene.index.Fields; import org.apache.lucene.util.English; import org.elasticsearch.ElasticsearchIllegalArgumentException; import org.elasticsearch.Version; import org.elasticsearch.action.admin.cluster.health.ClusterHealthStatus; import org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse; import org.elasticsearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse; +import org.elasticsearch.action.admin.indices.alias.Alias; +import org.elasticsearch.action.admin.indices.analyze.AnalyzeResponse; import org.elasticsearch.action.admin.indices.settings.get.GetSettingsResponse; import org.elasticsearch.action.count.CountResponse; import org.elasticsearch.action.delete.DeleteResponse; import org.elasticsearch.action.deletebyquery.DeleteByQueryResponse; import org.elasticsearch.action.deletebyquery.IndexDeleteByQueryResponse; +import org.elasticsearch.action.explain.ExplainResponse; import org.elasticsearch.action.get.GetResponse; import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.action.index.IndexRequestBuilder; +import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.action.search.SearchResponse; +import org.elasticsearch.action.termvector.TermVectorResponse; +import org.elasticsearch.action.update.UpdateRequestBuilder; +import org.elasticsearch.action.update.UpdateResponse; import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.metadata.IndexMetaData; import org.elasticsearch.cluster.routing.IndexRoutingTable; @@ -128,12 +136,12 @@ public class BasicBackwardsCompatibilityTest extends ElasticsearchBackwardsCompa */ @Test public void testIndexAndSearch() throws Exception { - createIndex("test"); + assertAcked(prepareCreate("test").addAlias(new Alias("alias"))); int numDocs = randomIntBetween(10, 20); List builder = new ArrayList<>(); for (int i = 0; i < numDocs; i++) { String id = Integer.toString(i); - builder.add(client().prepareIndex("test", "type1", id).setSource("field1", English.intToEnglish(i), "the_id", id)); + builder.add(client().prepareIndex(indexOrAlias(), "type1", id).setSource("field1", English.intToEnglish(i), "the_id", id)); } indexRandom(true, builder); for (int i = 0; i < numDocs; i++) { @@ -290,8 +298,8 @@ public class BasicBackwardsCompatibilityTest extends ElasticsearchBackwardsCompa docs[i] = client().prepareIndex(indexForDoc[i] = RandomPicks.randomFrom(getRandom(), indices), "type1", String.valueOf(i)).setSource("field1", English.intToEnglish(i), "num_int", randomInt(), "num_double", randomDouble()); } indexRandom(true, docs); - for (int i = 0; i < indices.length; i++) { - assertAllShardsOnNodes(indices[i], backwardsCluster().backwardsNodePattern()); + for (String index : indices) { + assertAllShardsOnNodes(index, backwardsCluster().backwardsNodePattern()); } client().admin().indices().prepareUpdateSettings(indices).setSettings(ImmutableSettings.builder().put(EnableAllocationDecider.INDEX_ROUTING_ALLOCATION_ENABLE, "none")).get(); backwardsCluster().allowOnAllNodes(indices); @@ -326,11 +334,11 @@ public class BasicBackwardsCompatibilityTest extends ElasticsearchBackwardsCompa assertVersionCreated(compatibilityVersion(), indices); } - public void assertVersionCreated(Version version, String... index) { - GetSettingsResponse getSettingsResponse = client().admin().indices().prepareGetSettings(index).get(); + public void assertVersionCreated(Version version, String... indices) { + GetSettingsResponse getSettingsResponse = client().admin().indices().prepareGetSettings(indices).get(); ImmutableOpenMap indexToSettings = getSettingsResponse.getIndexToSettings(); - for (int i = 0; i < index.length; i++) { - Settings settings = indexToSettings.get(index[i]); + for (String index : indices) { + Settings settings = indexToSettings.get(index); assertThat(settings.getAsVersion(IndexMetaData.SETTING_VERSION_CREATED, null), notNullValue()); assertThat(settings.getAsVersion(IndexMetaData.SETTING_VERSION_CREATED, null), equalTo(version)); } @@ -567,7 +575,7 @@ public class BasicBackwardsCompatibilityTest extends ElasticsearchBackwardsCompa int numDocs = iterations(10, 50); IndexRequestBuilder[] indexRequestBuilders = new IndexRequestBuilder[numDocs]; for (int i = 0; i < numDocs - 2; i++) { - indexRequestBuilders[i] = client().prepareIndex("test", "test", Integer.toString(i)) + indexRequestBuilders[i] = client().prepareIndex("test", "test", Integer.toString(i)) .setRouting(randomAsciiOfLength(randomIntBetween(1, 10))).setSource("field", "value"); } String firstDocId = Integer.toString(numDocs - 2); @@ -581,7 +589,7 @@ public class BasicBackwardsCompatibilityTest extends ElasticsearchBackwardsCompa indexRandom(true, indexRequestBuilders); SearchResponse searchResponse = client().prepareSearch("test").get(); - assertThat(searchResponse.getHits().totalHits(), equalTo((long)numDocs)); + assertThat(searchResponse.getHits().totalHits(), equalTo((long) numDocs)); //use routing DeleteResponse deleteResponse = client().prepareDelete("test", "test", firstDocId).setRouting("routing").get(); @@ -590,7 +598,7 @@ public class BasicBackwardsCompatibilityTest extends ElasticsearchBackwardsCompa assertThat(getResponse.isExists(), equalTo(false)); refresh(); searchResponse = client().prepareSearch("test").get(); - assertThat(searchResponse.getHits().totalHits(), equalTo((long)numDocs - 1)); + assertThat(searchResponse.getHits().totalHits(), equalTo((long) numDocs - 1)); //don't use routing and trigger a broadcast delete deleteResponse = client().prepareDelete("test", "test", secondDocId).get(); @@ -599,6 +607,123 @@ public class BasicBackwardsCompatibilityTest extends ElasticsearchBackwardsCompa assertThat(getResponse.isExists(), equalTo(false)); refresh(); searchResponse = client().prepareSearch("test").get(); - assertThat(searchResponse.getHits().totalHits(), equalTo((long)numDocs - 2)); + assertThat(searchResponse.getHits().totalHits(), equalTo((long) numDocs - 2)); + } + + @Test + public void testIndexGetAndDelete() throws ExecutionException, InterruptedException { + assertAcked(prepareCreate("test").addAlias(new Alias("alias"))); + ensureYellow("test"); + + int numDocs = iterations(10, 50); + for (int i = 0; i < numDocs; i++) { + IndexResponse indexResponse = client().prepareIndex(indexOrAlias(), "type", Integer.toString(i)).setSource("field", "value-" + i).get(); + assertThat(indexResponse.isCreated(), equalTo(true)); + assertThat(indexResponse.getIndex(), equalTo("test")); + assertThat(indexResponse.getType(), equalTo("type")); + assertThat(indexResponse.getId(), equalTo(Integer.toString(i))); + } + refresh(); + + String docId = Integer.toString(randomIntBetween(0, numDocs - 1)); + GetResponse getResponse = client().prepareGet(indexOrAlias(), "type", docId).get(); + assertThat(getResponse.isExists(), equalTo(true)); + assertThat(getResponse.getIndex(), equalTo("test")); + assertThat(getResponse.getType(), equalTo("type")); + assertThat(getResponse.getId(), equalTo(docId)); + + DeleteResponse deleteResponse = client().prepareDelete(indexOrAlias(), "type", docId).get(); + assertThat(deleteResponse.isFound(), equalTo(true)); + assertThat(deleteResponse.getIndex(), equalTo("test")); + assertThat(deleteResponse.getType(), equalTo("type")); + assertThat(deleteResponse.getId(), equalTo(docId)); + + getResponse = client().prepareGet(indexOrAlias(), "type", docId).get(); + assertThat(getResponse.isExists(), equalTo(false)); + + refresh(); + + SearchResponse searchResponse = client().prepareSearch(indexOrAlias()).get(); + assertThat(searchResponse.getHits().totalHits(), equalTo((long)numDocs - 1)); + } + + @Test + public void testUpdate() { + assertAcked(prepareCreate("test").addAlias(new Alias("alias"))); + ensureYellow("test"); + + UpdateRequestBuilder updateRequestBuilder = client().prepareUpdate(indexOrAlias(), "type1", "1") + .setUpsert("field1", "value1").setDoc("field2", "value2"); + + UpdateResponse updateResponse = updateRequestBuilder.get(); + assertThat(updateResponse.getIndex(), equalTo("test")); + assertThat(updateResponse.getType(), equalTo("type1")); + assertThat(updateResponse.getId(), equalTo("1")); + assertThat(updateResponse.isCreated(), equalTo(true)); + + GetResponse getResponse = client().prepareGet("test", "type1", "1").get(); + assertThat(getResponse.isExists(), equalTo(true)); + assertThat(getResponse.getSourceAsMap().containsKey("field1"), equalTo(true)); + assertThat(getResponse.getSourceAsMap().containsKey("field2"), equalTo(false)); + + updateResponse = updateRequestBuilder.get(); + assertThat(updateResponse.getIndex(), equalTo("test")); + assertThat(updateResponse.getType(), equalTo("type1")); + assertThat(updateResponse.getId(), equalTo("1")); + assertThat(updateResponse.isCreated(), equalTo(false)); + + getResponse = client().prepareGet("test", "type1", "1").get(); + assertThat(getResponse.isExists(), equalTo(true)); + assertThat(getResponse.getSourceAsMap().containsKey("field1"), equalTo(true)); + assertThat(getResponse.getSourceAsMap().containsKey("field2"), equalTo(true)); + } + + @Test + public void testAnalyze() { + assertAcked(prepareCreate("test").addAlias(new Alias("alias")) + .addMapping("test", "field", "type=string,analyzer=keyword")); + ensureYellow("test"); + AnalyzeResponse analyzeResponse = client().admin().indices().prepareAnalyze("this is a test").setIndex(indexOrAlias()).setField("field").get(); + assertThat(analyzeResponse.getTokens().size(), equalTo(1)); + assertThat(analyzeResponse.getTokens().get(0).getTerm(), equalTo("this is a test")); + } + + @Test + public void testExplain() { + assertAcked(prepareCreate("test").addAlias(new Alias("alias"))); + ensureYellow("test"); + + client().prepareIndex(indexOrAlias(), "test", "1").setSource("field", "value1").get(); + refresh(); + + ExplainResponse response = client().prepareExplain(indexOrAlias(), "test", "1") + .setQuery(QueryBuilders.termQuery("field", "value1")).get(); + assertThat(response.isExists(), equalTo(true)); + assertThat(response.isMatch(), equalTo(true)); + assertThat(response.getExplanation(), notNullValue()); + assertThat(response.getExplanation().isMatch(), equalTo(true)); + assertThat(response.getExplanation().getDetails().length, equalTo(1)); + } + + @Test + public void testGetTermVector() throws IOException { + assertAcked(prepareCreate("test").addAlias(new Alias("alias")) + .addMapping("type1", "field", "type=string,term_vector=with_positions_offsets_payloads")); + ensureYellow("test"); + + client().prepareIndex(indexOrAlias(), "type1", "1") + .setSource("field", "the quick brown fox jumps over the lazy dog").get(); + refresh(); + + TermVectorResponse termVectorResponse = client().prepareTermVector(indexOrAlias(), "type1", "1").get(); + assertThat(termVectorResponse.getIndex(), equalTo("test")); + assertThat(termVectorResponse.isExists(), equalTo(true)); + Fields fields = termVectorResponse.getFields(); + assertThat(fields.size(), equalTo(1)); + assertThat(fields.terms("field").size(), equalTo(8l)); + } + + private static String indexOrAlias() { + return randomBoolean() ? "test" : "alias"; } } diff --git a/src/test/java/org/elasticsearch/deleteByQuery/DeleteByQueryTests.java b/src/test/java/org/elasticsearch/deleteByQuery/DeleteByQueryTests.java index 4dff9e15d18..3b67f5aa35c 100644 --- a/src/test/java/org/elasticsearch/deleteByQuery/DeleteByQueryTests.java +++ b/src/test/java/org/elasticsearch/deleteByQuery/DeleteByQueryTests.java @@ -20,6 +20,7 @@ package org.elasticsearch.deleteByQuery; import org.elasticsearch.action.ShardOperationFailedException; +import org.elasticsearch.action.admin.indices.alias.Alias; import org.elasticsearch.action.deletebyquery.DeleteByQueryRequestBuilder; import org.elasticsearch.action.deletebyquery.DeleteByQueryResponse; import org.elasticsearch.action.search.SearchResponse; @@ -30,6 +31,7 @@ import org.elasticsearch.rest.RestStatus; import org.elasticsearch.test.ElasticsearchIntegrationTest; import org.junit.Test; +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount; import static org.hamcrest.Matchers.*; @@ -101,22 +103,22 @@ public class DeleteByQueryTests extends ElasticsearchIntegrationTest { @Test public void testFailure() throws Exception { - client().admin().indices().prepareCreate("twitter").execute().actionGet(); + assertAcked(prepareCreate("test").addAlias(new Alias("alias"))); - DeleteByQueryResponse response = client().prepareDeleteByQuery("twitter") + DeleteByQueryResponse response = client().prepareDeleteByQuery(indexOrAlias()) .setQuery(QueryBuilders.hasChildQuery("type", QueryBuilders.matchAllQuery())) .execute().actionGet(); - NumShards twitter = getNumShards("twitter"); + NumShards twitter = getNumShards("test"); assertThat(response.status(), equalTo(RestStatus.BAD_REQUEST)); - assertThat(response.getIndex("twitter").getSuccessfulShards(), equalTo(0)); - assertThat(response.getIndex("twitter").getFailedShards(), equalTo(twitter.numPrimaries)); + assertThat(response.getIndex("test").getSuccessfulShards(), equalTo(0)); + assertThat(response.getIndex("test").getFailedShards(), equalTo(twitter.numPrimaries)); assertThat(response.getIndices().size(), equalTo(1)); - assertThat(response.getIndices().get("twitter").getFailedShards(), equalTo(twitter.numPrimaries)); - assertThat(response.getIndices().get("twitter").getFailures().length, equalTo(twitter.numPrimaries)); - for (ShardOperationFailedException failure : response.getIndices().get("twitter").getFailures()) { - assertThat(failure.reason(), containsString("[twitter] [has_child] unsupported in delete_by_query api")); + assertThat(response.getIndices().get("test").getFailedShards(), equalTo(twitter.numPrimaries)); + assertThat(response.getIndices().get("test").getFailures().length, equalTo(twitter.numPrimaries)); + for (ShardOperationFailedException failure : response.getIndices().get("test").getFailures()) { + assertThat(failure.reason(), containsString("[test] [has_child] unsupported in delete_by_query api")); assertThat(failure.status(), equalTo(RestStatus.BAD_REQUEST)); assertThat(failure.shardId(), greaterThan(-1)); } @@ -124,7 +126,7 @@ public class DeleteByQueryTests extends ElasticsearchIntegrationTest { @Test public void testDeleteByFieldQuery() throws Exception { - client().admin().indices().prepareCreate("test").execute().actionGet(); + assertAcked(prepareCreate("test").addAlias(new Alias("alias"))); int numDocs = scaledRandomIntBetween(10, 100); for (int i = 0; i < numDocs; i++) { client().prepareIndex("test", "test", Integer.toString(i)) @@ -134,12 +136,13 @@ public class DeleteByQueryTests extends ElasticsearchIntegrationTest { refresh(); assertHitCount(client().prepareCount("test").setQuery(QueryBuilders.matchQuery("_id", Integer.toString(between(0, numDocs - 1)))).get(), 1); assertHitCount(client().prepareCount("test").setQuery(QueryBuilders.matchAllQuery()).get(), numDocs); - client().prepareDeleteByQuery("test") - .setQuery(QueryBuilders.matchQuery("_id", Integer.toString(between(0, numDocs - 1)))) - .execute().actionGet(); + DeleteByQueryResponse deleteByQueryResponse = client().prepareDeleteByQuery(indexOrAlias()) + .setQuery(QueryBuilders.matchQuery("_id", Integer.toString(between(0, numDocs - 1)))).get(); + assertThat(deleteByQueryResponse.getIndices().size(), equalTo(1)); + assertThat(deleteByQueryResponse.getIndex("test"), notNullValue()); + refresh(); assertHitCount(client().prepareCount("test").setQuery(QueryBuilders.matchAllQuery()).get(), numDocs - 1); - } @Test @@ -153,5 +156,7 @@ public class DeleteByQueryTests extends ElasticsearchIntegrationTest { assertHitCount(client().prepareCount("test").get(), 0); } - + private static String indexOrAlias() { + return randomBoolean() ? "test" : "alias"; + } } diff --git a/src/test/java/org/elasticsearch/document/BulkTests.java b/src/test/java/org/elasticsearch/document/BulkTests.java index ef5376c4afd..2ae723f78bf 100644 --- a/src/test/java/org/elasticsearch/document/BulkTests.java +++ b/src/test/java/org/elasticsearch/document/BulkTests.java @@ -20,6 +20,8 @@ package org.elasticsearch.document; import com.google.common.base.Charsets; +import org.elasticsearch.action.admin.indices.alias.Alias; +import org.elasticsearch.action.bulk.BulkItemResponse; import org.elasticsearch.action.bulk.BulkRequestBuilder; import org.elasticsearch.action.bulk.BulkResponse; import org.elasticsearch.action.count.CountResponse; @@ -51,30 +53,36 @@ public class BulkTests extends ElasticsearchIntegrationTest { @Test public void testBulkUpdate_simple() throws Exception { - createIndex("test"); + assertAcked(prepareCreate("test").addAlias(new Alias("alias"))); ensureGreen(); BulkResponse bulkResponse = client().prepareBulk() - .add(client().prepareIndex().setIndex("test").setType("type1").setId("1").setSource("field", 1)) - .add(client().prepareIndex().setIndex("test").setType("type1").setId("2").setSource("field", 2).setCreate(true)) - .add(client().prepareIndex().setIndex("test").setType("type1").setId("3").setSource("field", 3)) - .add(client().prepareIndex().setIndex("test").setType("type1").setId("4").setSource("field", 4)) - .add(client().prepareIndex().setIndex("test").setType("type1").setId("5").setSource("field", 5)) + .add(client().prepareIndex().setIndex(indexOrAlias()).setType("type1").setId("1").setSource("field", 1)) + .add(client().prepareIndex().setIndex(indexOrAlias()).setType("type1").setId("2").setSource("field", 2).setCreate(true)) + .add(client().prepareIndex().setIndex(indexOrAlias()).setType("type1").setId("3").setSource("field", 3)) + .add(client().prepareIndex().setIndex(indexOrAlias()).setType("type1").setId("4").setSource("field", 4)) + .add(client().prepareIndex().setIndex(indexOrAlias()).setType("type1").setId("5").setSource("field", 5)) .execute().actionGet(); assertThat(bulkResponse.hasFailures(), equalTo(false)); assertThat(bulkResponse.getItems().length, equalTo(5)); + for (BulkItemResponse bulkItemResponse : bulkResponse) { + assertThat(bulkItemResponse.getIndex(), equalTo("test")); + } bulkResponse = client().prepareBulk() - .add(client().prepareUpdate().setIndex("test").setType("type1").setId("1") + .add(client().prepareUpdate().setIndex(indexOrAlias()).setType("type1").setId("1") .setScript("ctx._source.field += 1", ScriptService.ScriptType.INLINE)) - .add(client().prepareUpdate().setIndex("test").setType("type1").setId("2") + .add(client().prepareUpdate().setIndex(indexOrAlias()).setType("type1").setId("2") .setScript("ctx._source.field += 1", ScriptService.ScriptType.INLINE).setRetryOnConflict(3)) - .add(client().prepareUpdate().setIndex("test").setType("type1").setId("3").setDoc(jsonBuilder().startObject().field("field1", "test").endObject())) + .add(client().prepareUpdate().setIndex(indexOrAlias()).setType("type1").setId("3").setDoc(jsonBuilder().startObject().field("field1", "test").endObject())) .execute().actionGet(); assertThat(bulkResponse.hasFailures(), equalTo(false)); assertThat(bulkResponse.getItems().length, equalTo(3)); + for (BulkItemResponse bulkItemResponse : bulkResponse) { + assertThat(bulkItemResponse.getIndex(), equalTo("test")); + } assertThat(((UpdateResponse) bulkResponse.getItems()[0].getResponse()).getId(), equalTo("1")); assertThat(((UpdateResponse) bulkResponse.getItems()[0].getResponse()).getVersion(), equalTo(2l)); assertThat(((UpdateResponse) bulkResponse.getItems()[1].getResponse()).getId(), equalTo("2")); @@ -98,12 +106,12 @@ public class BulkTests extends ElasticsearchIntegrationTest { assertThat(getResponse.getField("field1").getValue().toString(), equalTo("test")); bulkResponse = client().prepareBulk() - .add(client().prepareUpdate().setIndex("test").setType("type1").setId("6") + .add(client().prepareUpdate().setIndex(indexOrAlias()).setType("type1").setId("6") .setScript("ctx._source.field += 1", ScriptService.ScriptType.INLINE) .setUpsert(jsonBuilder().startObject().field("field", 0).endObject())) - .add(client().prepareUpdate().setIndex("test").setType("type1").setId("7") + .add(client().prepareUpdate().setIndex(indexOrAlias()).setType("type1").setId("7") .setScript("ctx._source.field += 1", ScriptService.ScriptType.INLINE)) - .add(client().prepareUpdate().setIndex("test").setType("type1").setId("2") + .add(client().prepareUpdate().setIndex(indexOrAlias()).setType("type1").setId("2") .setScript("ctx._source.field += 1", ScriptService.ScriptType.INLINE)) .execute().actionGet(); @@ -112,9 +120,11 @@ public class BulkTests extends ElasticsearchIntegrationTest { assertThat(((UpdateResponse) bulkResponse.getItems()[0].getResponse()).getId(), equalTo("6")); assertThat(((UpdateResponse) bulkResponse.getItems()[0].getResponse()).getVersion(), equalTo(1l)); assertThat(bulkResponse.getItems()[1].getResponse(), nullValue()); + assertThat(bulkResponse.getItems()[1].getFailure().getIndex(), equalTo("test")); assertThat(bulkResponse.getItems()[1].getFailure().getId(), equalTo("7")); assertThat(bulkResponse.getItems()[1].getFailure().getMessage(), containsString("DocumentMissingException")); assertThat(((UpdateResponse) bulkResponse.getItems()[2].getResponse()).getId(), equalTo("2")); + assertThat(((UpdateResponse) bulkResponse.getItems()[2].getResponse()).getIndex(), equalTo("test")); assertThat(((UpdateResponse) bulkResponse.getItems()[2].getResponse()).getVersion(), equalTo(3l)); getResponse = client().prepareGet().setIndex("test").setType("type1").setId("6").setFields("field").execute().actionGet(); @@ -640,5 +650,9 @@ public class BulkTests extends ElasticsearchIntegrationTest { assertThat(bulkItemResponse.getItems()[4].getOpType(), is("delete")); assertThat(bulkItemResponse.getItems()[5].getOpType(), is("delete")); } + + private static String indexOrAlias() { + return randomBoolean() ? "test" : "alias"; + } } diff --git a/src/test/java/org/elasticsearch/explain/ExplainActionTests.java b/src/test/java/org/elasticsearch/explain/ExplainActionTests.java index a3fea80c02a..5d225ce166e 100644 --- a/src/test/java/org/elasticsearch/explain/ExplainActionTests.java +++ b/src/test/java/org/elasticsearch/explain/ExplainActionTests.java @@ -19,6 +19,7 @@ package org.elasticsearch.explain; +import org.elasticsearch.action.admin.indices.alias.Alias; import org.elasticsearch.action.explain.ExplainResponse; import org.elasticsearch.common.settings.ImmutableSettings; import org.elasticsearch.index.query.FilterBuilders; @@ -33,7 +34,9 @@ import java.util.Map; import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.index.query.QueryBuilders.queryString; +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.notNullValue; /** */ @@ -41,18 +44,15 @@ public class ExplainActionTests extends ElasticsearchIntegrationTest { @Test public void testSimple() throws Exception { - client().admin().indices().prepareCreate("test").setSettings( - ImmutableSettings.settingsBuilder().put("index.refresh_interval", -1) - ).execute().actionGet(); - client().admin().cluster().prepareHealth("test").setWaitForGreenStatus().execute().actionGet(); + assertAcked(prepareCreate("test") + .addAlias(new Alias("alias")) + .setSettings(ImmutableSettings.settingsBuilder().put("index.refresh_interval", -1))); + ensureGreen("test"); - client().prepareIndex("test", "test", "1") - .setSource("field", "value1") - .execute().actionGet(); - - ExplainResponse response = client().prepareExplain("test", "test", "1") - .setQuery(QueryBuilders.matchAllQuery()) - .execute().actionGet(); + client().prepareIndex("test", "test", "1").setSource("field", "value1").get(); + + ExplainResponse response = client().prepareExplain(indexOrAlias(), "test", "1") + .setQuery(QueryBuilders.matchAllQuery()).get(); assertNotNull(response); assertFalse(response.isExists()); // not a match b/c not realtime assertThat(response.getIndex(), equalTo("test")); @@ -60,10 +60,9 @@ public class ExplainActionTests extends ElasticsearchIntegrationTest { assertThat(response.getId(), equalTo("1")); assertFalse(response.isMatch()); // not a match b/c not realtime - client().admin().indices().prepareRefresh("test").execute().actionGet(); - response = client().prepareExplain("test", "test", "1") - .setQuery(QueryBuilders.matchAllQuery()) - .execute().actionGet(); + refresh(); + response = client().prepareExplain(indexOrAlias(), "test", "1") + .setQuery(QueryBuilders.matchAllQuery()).get(); assertNotNull(response); assertTrue(response.isMatch()); assertNotNull(response.getExplanation()); @@ -73,10 +72,8 @@ public class ExplainActionTests extends ElasticsearchIntegrationTest { assertThat(response.getId(), equalTo("1")); assertThat(response.getExplanation().getValue(), equalTo(1.0f)); - client().admin().indices().prepareRefresh("test").execute().actionGet(); - response = client().prepareExplain("test", "test", "1") - .setQuery(QueryBuilders.termQuery("field", "value2")) - .execute().actionGet(); + response = client().prepareExplain(indexOrAlias(), "test", "1") + .setQuery(QueryBuilders.termQuery("field", "value2")).get(); assertNotNull(response); assertTrue(response.isExists()); assertFalse(response.isMatch()); @@ -86,13 +83,10 @@ public class ExplainActionTests extends ElasticsearchIntegrationTest { assertNotNull(response.getExplanation()); assertFalse(response.getExplanation().isMatch()); - client().admin().indices().prepareRefresh("test").execute().actionGet(); - response = client().prepareExplain("test", "test", "1") + response = client().prepareExplain(indexOrAlias(), "test", "1") .setQuery(QueryBuilders.boolQuery() - .must(QueryBuilders.termQuery("field", "value1")) - .must(QueryBuilders.termQuery("field", "value2")) - ) - .execute().actionGet(); + .must(QueryBuilders.termQuery("field", "value1")) + .must(QueryBuilders.termQuery("field", "value2"))).get(); assertNotNull(response); assertTrue(response.isExists()); assertFalse(response.isMatch()); @@ -103,9 +97,8 @@ public class ExplainActionTests extends ElasticsearchIntegrationTest { assertFalse(response.getExplanation().isMatch()); assertThat(response.getExplanation().getDetails().length, equalTo(2)); - response = client().prepareExplain("test", "test", "2") - .setQuery(QueryBuilders.matchAllQuery()) - .execute().actionGet(); + response = client().prepareExplain(indexOrAlias(), "test", "2") + .setQuery(QueryBuilders.matchAllQuery()).get(); assertNotNull(response); assertFalse(response.isExists()); assertFalse(response.isMatch()); @@ -117,8 +110,8 @@ public class ExplainActionTests extends ElasticsearchIntegrationTest { @SuppressWarnings("unchecked") @Test public void testExplainWithFields() throws Exception { - createIndex("test"); - client().admin().cluster().prepareHealth("test").setWaitForGreenStatus().execute().actionGet(); + assertAcked(prepareCreate("test").addAlias(new Alias("alias"))); + ensureGreen("test"); client().prepareIndex("test", "test", "1") .setSource( @@ -127,14 +120,12 @@ public class ExplainActionTests extends ElasticsearchIntegrationTest { .field("field1", "value1") .field("field2", "value2") .endObject() - .endObject() - ).execute().actionGet(); + .endObject()).get(); - client().admin().indices().prepareRefresh("test").execute().actionGet(); - ExplainResponse response = client().prepareExplain("test", "test", "1") + refresh(); + ExplainResponse response = client().prepareExplain(indexOrAlias(), "test", "1") .setQuery(QueryBuilders.matchAllQuery()) - .setFields("obj1.field1") - .execute().actionGet(); + .setFields("obj1.field1").get(); assertNotNull(response); assertTrue(response.isMatch()); assertNotNull(response.getExplanation()); @@ -146,12 +137,10 @@ public class ExplainActionTests extends ElasticsearchIntegrationTest { assertThat(response.getGetResult().getFields().get("obj1.field1").getValue().toString(), equalTo("value1")); assertThat(response.getGetResult().isSourceEmpty(), equalTo(true)); - client().admin().indices().prepareRefresh("test").execute().actionGet(); - response = client().prepareExplain("test", "test", "1") + refresh(); + response = client().prepareExplain(indexOrAlias(), "test", "1") .setQuery(QueryBuilders.matchAllQuery()) - .setFields("obj1.field1") - .setFetchSource(true) - .get(); + .setFields("obj1.field1").setFetchSource(true).get(); assertNotNull(response); assertTrue(response.isMatch()); assertNotNull(response.getExplanation()); @@ -163,10 +152,9 @@ public class ExplainActionTests extends ElasticsearchIntegrationTest { assertThat(response.getGetResult().getFields().get("obj1.field1").getValue().toString(), equalTo("value1")); assertThat(response.getGetResult().isSourceEmpty(), equalTo(false)); - response = client().prepareExplain("test", "test", "1") + response = client().prepareExplain(indexOrAlias(), "test", "1") .setQuery(QueryBuilders.matchAllQuery()) - .setFields("obj1.field1", "obj1.field2") - .execute().actionGet(); + .setFields("obj1.field1", "obj1.field2").get(); assertNotNull(response); assertTrue(response.isMatch()); String v1 = (String) response.getGetResult().field("obj1.field1").getValue(); @@ -178,8 +166,8 @@ public class ExplainActionTests extends ElasticsearchIntegrationTest { @SuppressWarnings("unchecked") @Test public void testExplainWitSource() throws Exception { - client().admin().indices().prepareCreate("test").execute().actionGet(); - client().admin().cluster().prepareHealth("test").setWaitForGreenStatus().execute().actionGet(); + assertAcked(prepareCreate("test").addAlias(new Alias("alias"))); + ensureGreen("test"); client().prepareIndex("test", "test", "1") .setSource( @@ -188,14 +176,12 @@ public class ExplainActionTests extends ElasticsearchIntegrationTest { .field("field1", "value1") .field("field2", "value2") .endObject() - .endObject() - ).execute().actionGet(); + .endObject()).get(); - client().admin().indices().prepareRefresh("test").execute().actionGet(); - ExplainResponse response = client().prepareExplain("test", "test", "1") + refresh(); + ExplainResponse response = client().prepareExplain(indexOrAlias(), "test", "1") .setQuery(QueryBuilders.matchAllQuery()) - .setFetchSource("obj1.field1", null) - .get(); + .setFetchSource("obj1.field1", null).get(); assertNotNull(response); assertTrue(response.isMatch()); assertNotNull(response.getExplanation()); @@ -206,35 +192,53 @@ public class ExplainActionTests extends ElasticsearchIntegrationTest { assertThat(response.getGetResult().getSource().size(), equalTo(1)); assertThat(((Map) response.getGetResult().getSource().get("obj1")).get("field1").toString(), equalTo("value1")); - response = client().prepareExplain("test", "test", "1") + response = client().prepareExplain(indexOrAlias(), "test", "1") .setQuery(QueryBuilders.matchAllQuery()) - .setFetchSource(null, "obj1.field2") - .execute().actionGet(); + .setFetchSource(null, "obj1.field2").get(); assertNotNull(response); assertTrue(response.isMatch()); assertThat(((Map) response.getGetResult().getSource().get("obj1")).get("field1").toString(), equalTo("value1")); } @Test - public void testExplainWithAlias() throws Exception { - client().admin().indices().prepareCreate("test") - .execute().actionGet(); - client().admin().cluster().prepareHealth("test").setWaitForGreenStatus().execute().actionGet(); + public void testExplainWithFilteredAlias() throws Exception { + assertAcked(prepareCreate("test").addAlias(new Alias("alias1").filter(FilterBuilders.termFilter("field2", "value2")))); + ensureGreen("test"); - client().admin().indices().prepareAliases().addAlias("test", "alias1", FilterBuilders.termFilter("field2", "value2")) - .execute().actionGet(); - client().prepareIndex("test", "test", "1").setSource("field1", "value1", "field2", "value1").execute().actionGet(); - client().admin().indices().prepareRefresh("test").execute().actionGet(); + client().prepareIndex("test", "test", "1").setSource("field1", "value1", "field2", "value1").get(); + refresh(); ExplainResponse response = client().prepareExplain("alias1", "test", "1") - .setQuery(QueryBuilders.matchAllQuery()) - .execute().actionGet(); + .setQuery(QueryBuilders.matchAllQuery()).get(); + assertNotNull(response); + assertTrue(response.isExists()); + assertFalse(response.isMatch()); + } + + @Test + public void testExplainWithFilteredAliasFetchSource() throws Exception { + assertAcked(client().admin().indices().prepareCreate("test") + .addAlias(new Alias("alias1").filter(FilterBuilders.termFilter("field2", "value2")))); + ensureGreen("test"); + + client().prepareIndex("test", "test", "1").setSource("field1", "value1", "field2", "value1").get(); + refresh(); + + ExplainResponse response = client().prepareExplain("alias1", "test", "1") + .setQuery(QueryBuilders.matchAllQuery()).setFetchSource(true).get(); + assertNotNull(response); assertTrue(response.isExists()); assertFalse(response.isMatch()); assertThat(response.getIndex(), equalTo("test")); assertThat(response.getType(), equalTo("test")); assertThat(response.getId(), equalTo("1")); + assertThat(response.getGetResult(), notNullValue()); + assertThat(response.getGetResult().getIndex(), equalTo("test")); + assertThat(response.getGetResult().getType(), equalTo("test")); + assertThat(response.getGetResult().getId(), equalTo("1")); + assertThat(response.getGetResult().getSource(), notNullValue()); + assertThat((String)response.getGetResult().getSource().get("field1"), equalTo("value1")); } @Test @@ -252,4 +256,8 @@ public class ExplainActionTests extends ElasticsearchIntegrationTest { assertThat(explainResponse.isExists(), equalTo(true)); assertThat(explainResponse.isMatch(), equalTo(true)); } + + private static String indexOrAlias() { + return randomBoolean() ? "test" : "alias"; + } } diff --git a/src/test/java/org/elasticsearch/get/GetActionTests.java b/src/test/java/org/elasticsearch/get/GetActionTests.java index b28c07fc255..d43c8dbe2ee 100644 --- a/src/test/java/org/elasticsearch/get/GetActionTests.java +++ b/src/test/java/org/elasticsearch/get/GetActionTests.java @@ -22,8 +22,7 @@ package org.elasticsearch.get; import org.elasticsearch.ElasticsearchException; import org.elasticsearch.ElasticsearchIllegalArgumentException; import org.elasticsearch.action.ShardOperationFailedException; -import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse; -import org.elasticsearch.action.admin.cluster.health.ClusterHealthStatus; +import org.elasticsearch.action.admin.indices.alias.Alias; import org.elasticsearch.action.admin.indices.flush.FlushResponse; import org.elasticsearch.action.delete.DeleteResponse; import org.elasticsearch.action.get.*; @@ -42,7 +41,6 @@ import org.junit.Test; import java.io.IOException; import java.util.Map; -import static org.elasticsearch.client.Requests.clusterHealthRequest; import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; import static org.hamcrest.Matchers.*; @@ -51,152 +49,176 @@ public class GetActionTests extends ElasticsearchIntegrationTest { @Test public void simpleGetTests() { - - client().admin().indices().prepareCreate("test").setSettings(ImmutableSettings.settingsBuilder().put("index.refresh_interval", -1)).execute().actionGet(); - + assertAcked(prepareCreate("test") + .setSettings(ImmutableSettings.settingsBuilder().put("index.refresh_interval", -1)) + .addAlias(new Alias("alias"))); ensureGreen(); - GetResponse response = client().prepareGet("test", "type1", "1").execute().actionGet(); + GetResponse response = client().prepareGet(indexOrAlias(), "type1", "1").get(); assertThat(response.isExists(), equalTo(false)); logger.info("--> index doc 1"); client().prepareIndex("test", "type1", "1").setSource("field1", "value1", "field2", "value2").get(); logger.info("--> realtime get 1"); - response = client().prepareGet("test", "type1", "1").execute().actionGet(); + response = client().prepareGet(indexOrAlias(), "type1", "1").get(); assertThat(response.isExists(), equalTo(true)); + assertThat(response.getIndex(), equalTo("test")); assertThat(response.getSourceAsMap().get("field1").toString(), equalTo("value1")); assertThat(response.getSourceAsMap().get("field2").toString(), equalTo("value2")); logger.info("--> realtime get 1 (no source, implicit)"); - response = client().prepareGet("test", "type1", "1").setFields(Strings.EMPTY_ARRAY).get(); + response = client().prepareGet(indexOrAlias(), "type1", "1").setFields(Strings.EMPTY_ARRAY).get(); assertThat(response.isExists(), equalTo(true)); + assertThat(response.getIndex(), equalTo("test")); assertThat(response.getFields().size(), equalTo(0)); assertThat(response.getSourceAsBytes(), nullValue()); logger.info("--> realtime get 1 (no source, explicit)"); - response = client().prepareGet("test", "type1", "1").setFetchSource(false).get(); + response = client().prepareGet(indexOrAlias(), "type1", "1").setFetchSource(false).get(); assertThat(response.isExists(), equalTo(true)); + assertThat(response.getIndex(), equalTo("test")); assertThat(response.getFields().size(), equalTo(0)); assertThat(response.getSourceAsBytes(), nullValue()); logger.info("--> realtime get 1 (no type)"); - response = client().prepareGet("test", null, "1").execute().actionGet(); + response = client().prepareGet(indexOrAlias(), null, "1").get(); assertThat(response.isExists(), equalTo(true)); + assertThat(response.getIndex(), equalTo("test")); assertThat(response.getSourceAsMap().get("field1").toString(), equalTo("value1")); assertThat(response.getSourceAsMap().get("field2").toString(), equalTo("value2")); logger.info("--> non realtime get 1"); - response = client().prepareGet("test", "type1", "1").setRealtime(false).execute().actionGet(); + response = client().prepareGet(indexOrAlias(), "type1", "1").setRealtime(false).get(); assertThat(response.isExists(), equalTo(false)); logger.info("--> realtime fetch of field (requires fetching parsing source)"); - response = client().prepareGet("test", "type1", "1").setFields("field1").execute().actionGet(); + response = client().prepareGet(indexOrAlias(), "type1", "1").setFields("field1").get(); assertThat(response.isExists(), equalTo(true)); + assertThat(response.getIndex(), equalTo("test")); assertThat(response.getSourceAsBytes(), nullValue()); assertThat(response.getField("field1").getValues().get(0).toString(), equalTo("value1")); assertThat(response.getField("field2"), nullValue()); logger.info("--> realtime fetch of field & source (requires fetching parsing source)"); - response = client().prepareGet("test", "type1", "1").setFields("field1").setFetchSource("field1", null).execute().actionGet(); + response = client().prepareGet(indexOrAlias(), "type1", "1").setFields("field1").setFetchSource("field1", null).get(); assertThat(response.isExists(), equalTo(true)); + assertThat(response.getIndex(), equalTo("test")); assertThat(response.getSourceAsMap(), hasKey("field1")); assertThat(response.getSourceAsMap(), not(hasKey("field2"))); assertThat(response.getField("field1").getValues().get(0).toString(), equalTo("value1")); assertThat(response.getField("field2"), nullValue()); logger.info("--> flush the index, so we load it from it"); - client().admin().indices().prepareFlush().execute().actionGet(); + flush(); logger.info("--> realtime get 1 (loaded from index)"); - response = client().prepareGet("test", "type1", "1").execute().actionGet(); + response = client().prepareGet(indexOrAlias(), "type1", "1").get(); assertThat(response.isExists(), equalTo(true)); + assertThat(response.getIndex(), equalTo("test")); assertThat(response.getSourceAsMap().get("field1").toString(), equalTo("value1")); assertThat(response.getSourceAsMap().get("field2").toString(), equalTo("value2")); logger.info("--> non realtime get 1 (loaded from index)"); - response = client().prepareGet("test", "type1", "1").setRealtime(false).execute().actionGet(); + response = client().prepareGet(indexOrAlias(), "type1", "1").setRealtime(false).get(); assertThat(response.isExists(), equalTo(true)); + assertThat(response.getIndex(), equalTo("test")); assertThat(response.getSourceAsMap().get("field1").toString(), equalTo("value1")); assertThat(response.getSourceAsMap().get("field2").toString(), equalTo("value2")); logger.info("--> realtime fetch of field (loaded from index)"); - response = client().prepareGet("test", "type1", "1").setFields("field1").execute().actionGet(); + response = client().prepareGet(indexOrAlias(), "type1", "1").setFields("field1").get(); assertThat(response.isExists(), equalTo(true)); + assertThat(response.getIndex(), equalTo("test")); assertThat(response.getSourceAsBytes(), nullValue()); assertThat(response.getField("field1").getValues().get(0).toString(), equalTo("value1")); assertThat(response.getField("field2"), nullValue()); logger.info("--> realtime fetch of field & source (loaded from index)"); - response = client().prepareGet("test", "type1", "1").setFields("field1").setFetchSource(true).execute().actionGet(); + response = client().prepareGet(indexOrAlias(), "type1", "1").setFields("field1").setFetchSource(true).get(); assertThat(response.isExists(), equalTo(true)); + assertThat(response.getIndex(), equalTo("test")); assertThat(response.getSourceAsBytes(), not(nullValue())); assertThat(response.getField("field1").getValues().get(0).toString(), equalTo("value1")); assertThat(response.getField("field2"), nullValue()); logger.info("--> update doc 1"); - client().prepareIndex("test", "type1", "1").setSource("field1", "value1_1", "field2", "value2_1").execute().actionGet(); + client().prepareIndex("test", "type1", "1").setSource("field1", "value1_1", "field2", "value2_1").get(); logger.info("--> realtime get 1"); - response = client().prepareGet("test", "type1", "1").execute().actionGet(); + response = client().prepareGet(indexOrAlias(), "type1", "1").get(); assertThat(response.isExists(), equalTo(true)); + assertThat(response.getIndex(), equalTo("test")); assertThat(response.getSourceAsMap().get("field1").toString(), equalTo("value1_1")); assertThat(response.getSourceAsMap().get("field2").toString(), equalTo("value2_1")); logger.info("--> update doc 1 again"); - client().prepareIndex("test", "type1", "1").setSource("field1", "value1_2", "field2", "value2_2").execute().actionGet(); + client().prepareIndex("test", "type1", "1").setSource("field1", "value1_2", "field2", "value2_2").get(); - response = client().prepareGet("test", "type1", "1").execute().actionGet(); + response = client().prepareGet(indexOrAlias(), "type1", "1").get(); assertThat(response.isExists(), equalTo(true)); + assertThat(response.getIndex(), equalTo("test")); assertThat(response.getSourceAsMap().get("field1").toString(), equalTo("value1_2")); assertThat(response.getSourceAsMap().get("field2").toString(), equalTo("value2_2")); - DeleteResponse deleteResponse = client().prepareDelete("test", "type1", "1").execute().actionGet(); + DeleteResponse deleteResponse = client().prepareDelete("test", "type1", "1").get(); assertThat(deleteResponse.isFound(), equalTo(true)); - response = client().prepareGet("test", "type1", "1").execute().actionGet(); + response = client().prepareGet(indexOrAlias(), "type1", "1").get(); assertThat(response.isExists(), equalTo(false)); } + private static String indexOrAlias() { + return randomBoolean() ? "test" : "alias"; + } + @Test public void simpleMultiGetTests() throws Exception { - client().admin().indices().prepareCreate("test").setSettings(ImmutableSettings.settingsBuilder().put("index.refresh_interval", -1)).execute().actionGet(); - + assertAcked(prepareCreate("test").addAlias(new Alias("alias")) + .setSettings(ImmutableSettings.settingsBuilder().put("index.refresh_interval", -1))); ensureGreen(); - MultiGetResponse response = client().prepareMultiGet().add("test", "type1", "1").execute().actionGet(); + MultiGetResponse response = client().prepareMultiGet().add(indexOrAlias(), "type1", "1").get(); assertThat(response.getResponses().length, equalTo(1)); assertThat(response.getResponses()[0].getResponse().isExists(), equalTo(false)); for (int i = 0; i < 10; i++) { - client().prepareIndex("test", "type1", Integer.toString(i)).setSource("field", "value" + i).execute().actionGet(); + client().prepareIndex("test", "type1", Integer.toString(i)).setSource("field", "value" + i).get(); } response = client().prepareMultiGet() - .add("test", "type1", "1") - .add("test", "type1", "15") - .add("test", "type1", "3") - .add("test", "type1", "9") - .add("test", "type1", "11") - .execute().actionGet(); + .add(indexOrAlias(), "type1", "1") + .add(indexOrAlias(), "type1", "15") + .add(indexOrAlias(), "type1", "3") + .add(indexOrAlias(), "type1", "9") + .add(indexOrAlias(), "type1", "11").get(); assertThat(response.getResponses().length, equalTo(5)); assertThat(response.getResponses()[0].getId(), equalTo("1")); + assertThat(response.getResponses()[0].getIndex(), equalTo("test")); + assertThat(response.getResponses()[0].getResponse().getIndex(), equalTo("test")); assertThat(response.getResponses()[0].getResponse().isExists(), equalTo(true)); assertThat(response.getResponses()[0].getResponse().getSourceAsMap().get("field").toString(), equalTo("value1")); assertThat(response.getResponses()[1].getId(), equalTo("15")); + assertThat(response.getResponses()[1].getIndex(), equalTo("test")); + assertThat(response.getResponses()[1].getResponse().getIndex(), equalTo("test")); assertThat(response.getResponses()[1].getResponse().isExists(), equalTo(false)); assertThat(response.getResponses()[2].getId(), equalTo("3")); + assertThat(response.getResponses()[2].getIndex(), equalTo("test")); assertThat(response.getResponses()[2].getResponse().isExists(), equalTo(true)); assertThat(response.getResponses()[3].getId(), equalTo("9")); + assertThat(response.getResponses()[3].getIndex(), equalTo("test")); + assertThat(response.getResponses()[3].getResponse().getIndex(), equalTo("test")); assertThat(response.getResponses()[3].getResponse().isExists(), equalTo(true)); assertThat(response.getResponses()[4].getId(), equalTo("11")); + assertThat(response.getResponses()[4].getIndex(), equalTo("test")); + assertThat(response.getResponses()[4].getResponse().getIndex(), equalTo("test")); assertThat(response.getResponses()[4].getResponse().isExists(), equalTo(false)); // multi get with specific field response = client().prepareMultiGet() - .add(new MultiGetRequest.Item("test", "type1", "1").fields("field")) - .add(new MultiGetRequest.Item("test", "type1", "3").fields("field")) - .execute().actionGet(); + .add(new MultiGetRequest.Item(indexOrAlias(), "type1", "1").fields("field")) + .add(new MultiGetRequest.Item(indexOrAlias(), "type1", "3").fields("field")) + .get(); assertThat(response.getResponses().length, equalTo(2)); assertThat(response.getResponses()[0].getResponse().getSourceAsBytes(), nullValue()); @@ -205,11 +227,8 @@ public class GetActionTests extends ElasticsearchIntegrationTest { @Test public void realtimeGetWithCompress() throws Exception { - - client().admin().indices().prepareCreate("test").setSettings(ImmutableSettings.settingsBuilder().put("index.refresh_interval", -1)) - .addMapping("type", jsonBuilder().startObject().startObject("type").startObject("_source").field("compress", true).endObject().endObject().endObject()) - .execute().actionGet(); - + assertAcked(prepareCreate("test").setSettings(ImmutableSettings.settingsBuilder().put("index.refresh_interval", -1)) + .addMapping("type", jsonBuilder().startObject().startObject("type").startObject("_source").field("compress", true).endObject().endObject().endObject())); ensureGreen(); StringBuilder sb = new StringBuilder(); @@ -217,18 +236,17 @@ public class GetActionTests extends ElasticsearchIntegrationTest { sb.append((char) i); } String fieldValue = sb.toString(); - client().prepareIndex("test", "type", "1").setSource("field", fieldValue).execute().actionGet(); + client().prepareIndex("test", "type", "1").setSource("field", fieldValue).get(); // realtime get - GetResponse getResponse = client().prepareGet("test", "type", "1").execute().actionGet(); + GetResponse getResponse = client().prepareGet("test", "type", "1").get(); assertThat(getResponse.isExists(), equalTo(true)); assertThat(getResponse.getSourceAsMap().get("field").toString(), equalTo(fieldValue)); } @Test public void getFieldsWithDifferentTypes() throws Exception { - - client().admin().indices().prepareCreate("test").setSettings(ImmutableSettings.settingsBuilder().put("index.refresh_interval", -1)) + assertAcked(prepareCreate("test").setSettings(ImmutableSettings.settingsBuilder().put("index.refresh_interval", -1)) .addMapping("type1", jsonBuilder().startObject().startObject("type1").startObject("_source").field("enabled", true).endObject().endObject().endObject()) .addMapping("type2", jsonBuilder().startObject().startObject("type2") .startObject("_source").field("enabled", false).endObject() @@ -240,9 +258,7 @@ public class GetActionTests extends ElasticsearchIntegrationTest { .startObject("date").field("type", "date").field("store", "yes").endObject() .startObject("binary").field("type", "binary").field("store", "yes").endObject() .endObject() - .endObject().endObject()) - .execute().actionGet(); - + .endObject().endObject())); ensureGreen(); client().prepareIndex("test", "type1", "1").setSource( @@ -253,7 +269,7 @@ public class GetActionTests extends ElasticsearchIntegrationTest { .field("ints", new int[]{1, 2, 3, 4}) .field("date", "2012-11-13T15:26:14.000Z") .field("binary", Base64.encodeBytes(new byte[]{1, 2, 3})) - .endObject()).execute().actionGet(); + .endObject()).get(); client().prepareIndex("test", "type2", "1").setSource( jsonBuilder().startObject() @@ -263,11 +279,11 @@ public class GetActionTests extends ElasticsearchIntegrationTest { .field("ints", new int[]{1, 2, 3, 4}) .field("date", "2012-11-13T15:26:14.000Z") .field("binary", Base64.encodeBytes(new byte[]{1, 2, 3})) - .endObject()).execute().actionGet(); + .endObject()).get(); // realtime get with stored source logger.info("--> realtime get (from source)"); - GetResponse getResponse = client().prepareGet("test", "type1", "1").setFields("str", "strs", "int", "ints", "date", "binary").execute().actionGet(); + GetResponse getResponse = client().prepareGet("test", "type1", "1").setFields("str", "strs", "int", "ints", "date", "binary").get(); assertThat(getResponse.isExists(), equalTo(true)); assertThat((String) getResponse.getField("str").getValue(), equalTo("test")); assertThat(getResponse.getField("strs").getValues(), contains((Object) "A", "B", "C")); @@ -277,7 +293,7 @@ public class GetActionTests extends ElasticsearchIntegrationTest { assertThat(getResponse.getField("binary").getValue(), instanceOf(String.class)); // its a String..., not binary mapped logger.info("--> realtime get (from stored fields)"); - getResponse = client().prepareGet("test", "type2", "1").setFields("str", "strs", "int", "ints", "date", "binary").execute().actionGet(); + getResponse = client().prepareGet("test", "type2", "1").setFields("str", "strs", "int", "ints", "date", "binary").get(); assertThat(getResponse.isExists(), equalTo(true)); assertThat((String) getResponse.getField("str").getValue(), equalTo("test")); assertThat(getResponse.getField("strs").getValues(), contains((Object) "A", "B", "C")); @@ -287,10 +303,10 @@ public class GetActionTests extends ElasticsearchIntegrationTest { assertThat((BytesReference) getResponse.getField("binary").getValue(), equalTo((BytesReference) new BytesArray(new byte[]{1, 2, 3}))); logger.info("--> flush the index, so we load it from it"); - client().admin().indices().prepareFlush().execute().actionGet(); + flush(); logger.info("--> non realtime get (from source)"); - getResponse = client().prepareGet("test", "type1", "1").setFields("str", "strs", "int", "ints", "date", "binary").execute().actionGet(); + getResponse = client().prepareGet("test", "type1", "1").setFields("str", "strs", "int", "ints", "date", "binary").get(); assertThat(getResponse.isExists(), equalTo(true)); assertThat((String) getResponse.getField("str").getValue(), equalTo("test")); assertThat(getResponse.getField("strs").getValues(), contains((Object) "A", "B", "C")); @@ -300,7 +316,7 @@ public class GetActionTests extends ElasticsearchIntegrationTest { assertThat(getResponse.getField("binary").getValue(), instanceOf(String.class)); // its a String..., not binary mapped logger.info("--> non realtime get (from stored fields)"); - getResponse = client().prepareGet("test", "type2", "1").setFields("str", "strs", "int", "ints", "date", "binary").execute().actionGet(); + getResponse = client().prepareGet("test", "type2", "1").setFields("str", "strs", "int", "ints", "date", "binary").get(); assertThat(getResponse.isExists(), equalTo(true)); assertThat((String) getResponse.getField("str").getValue(), equalTo("test")); assertThat(getResponse.getField("strs").getValues(), contains((Object) "A", "B", "C")); @@ -312,11 +328,6 @@ public class GetActionTests extends ElasticsearchIntegrationTest { @Test public void testGetDocWithMultivaluedFields() throws Exception { - try { - client().admin().indices().prepareDelete("test").execute().actionGet(); - } catch (Exception e) { - // fine - } String mapping1 = XContentFactory.jsonBuilder().startObject().startObject("type1") .startObject("properties") .startObject("field").field("type", "string").field("store", "yes").endObject() @@ -328,30 +339,24 @@ public class GetActionTests extends ElasticsearchIntegrationTest { .endObject() .startObject("_source").field("enabled", false).endObject() .endObject().endObject().string(); - client().admin().indices().prepareCreate("test") + assertAcked(prepareCreate("test") .addMapping("type1", mapping1) .addMapping("type2", mapping2) - .setSettings(ImmutableSettings.settingsBuilder().put("index.refresh_interval", -1)) - .execute().actionGet(); - + .setSettings(ImmutableSettings.settingsBuilder().put("index.refresh_interval", -1))); ensureGreen(); - GetResponse response = client().prepareGet("test", "type1", "1").execute().actionGet(); + GetResponse response = client().prepareGet("test", "type1", "1").get(); assertThat(response.isExists(), equalTo(false)); - response = client().prepareGet("test", "type2", "1").execute().actionGet(); + response = client().prepareGet("test", "type2", "1").get(); assertThat(response.isExists(), equalTo(false)); client().prepareIndex("test", "type1", "1") - .setSource(jsonBuilder().startObject().field("field", "1", "2").endObject()) - .execute().actionGet(); + .setSource(jsonBuilder().startObject().field("field", "1", "2").endObject()).get(); client().prepareIndex("test", "type2", "1") - .setSource(jsonBuilder().startObject().field("field", "1", "2").endObject()) - .execute().actionGet(); + .setSource(jsonBuilder().startObject().field("field", "1", "2").endObject()).get(); - response = client().prepareGet("test", "type1", "1") - .setFields("field") - .execute().actionGet(); + response = client().prepareGet("test", "type1", "1").setFields("field").get(); assertThat(response.isExists(), equalTo(true)); assertThat(response.getId(), equalTo("1")); assertThat(response.getType(), equalTo("type1")); @@ -361,9 +366,7 @@ public class GetActionTests extends ElasticsearchIntegrationTest { assertThat(response.getFields().get("field").getValues().get(1).toString(), equalTo("2")); - response = client().prepareGet("test", "type2", "1") - .setFields("field") - .execute().actionGet(); + response = client().prepareGet("test", "type2", "1").setFields("field").get(); assertThat(response.isExists(), equalTo(true)); assertThat(response.getType(), equalTo("type2")); assertThat(response.getId(), equalTo("1")); @@ -373,10 +376,8 @@ public class GetActionTests extends ElasticsearchIntegrationTest { assertThat(response.getFields().get("field").getValues().get(1).toString(), equalTo("2")); // Now test values being fetched from stored fields. - client().admin().indices().prepareRefresh("test").execute().actionGet(); - response = client().prepareGet("test", "type1", "1") - .setFields("field") - .execute().actionGet(); + refresh(); + response = client().prepareGet("test", "type1", "1").setFields("field").get(); assertThat(response.isExists(), equalTo(true)); assertThat(response.getId(), equalTo("1")); assertThat(response.getFields().size(), equalTo(1)); @@ -384,10 +385,7 @@ public class GetActionTests extends ElasticsearchIntegrationTest { assertThat(response.getFields().get("field").getValues().get(0).toString(), equalTo("1")); assertThat(response.getFields().get("field").getValues().get(1).toString(), equalTo("2")); - - response = client().prepareGet("test", "type2", "1") - .setFields("field") - .execute().actionGet(); + response = client().prepareGet("test", "type2", "1").setFields("field").get(); assertThat(response.isExists(), equalTo(true)); assertThat(response.getId(), equalTo("1")); assertThat(response.getFields().size(), equalTo(1)); @@ -411,18 +409,17 @@ public class GetActionTests extends ElasticsearchIntegrationTest { .endObject() .string(); - client().admin().indices().prepareCreate(index) + assertAcked(prepareCreate(index) .addMapping(type, mapping) - .setSettings(ImmutableSettings.settingsBuilder().put("index.refresh_interval", -1)) - .execute().actionGet(); + .setSettings(ImmutableSettings.settingsBuilder().put("index.refresh_interval", -1))); client().prepareIndex(index, type, "1") .setSource(jsonBuilder().startObject().field("field", "1", "2").field("excluded", "should not be seen").endObject()) - .execute().actionGet(); + .get(); - GetResponse responseBeforeFlush = client().prepareGet(index, type, "1").execute().actionGet(); - client().admin().indices().prepareFlush(index).execute().actionGet(); - GetResponse responseAfterFlush = client().prepareGet(index, type, "1").execute().actionGet(); + GetResponse responseBeforeFlush = client().prepareGet(index, type, "1").get(); + client().admin().indices().prepareFlush(index).get(); + GetResponse responseAfterFlush = client().prepareGet(index, type, "1").get(); assertThat(responseBeforeFlush.isExists(), is(true)); assertThat(responseAfterFlush.isExists(), is(true)); @@ -446,18 +443,17 @@ public class GetActionTests extends ElasticsearchIntegrationTest { .endObject() .string(); - client().admin().indices().prepareCreate(index) + assertAcked(prepareCreate(index) .addMapping(type, mapping) - .setSettings(ImmutableSettings.settingsBuilder().put("index.refresh_interval", -1)) - .execute().actionGet(); + .setSettings(ImmutableSettings.settingsBuilder().put("index.refresh_interval", -1))); client().prepareIndex(index, type, "1") .setSource(jsonBuilder().startObject().field("field", "1", "2").field("included", "should be seen").endObject()) - .execute().actionGet(); + .get(); - GetResponse responseBeforeFlush = client().prepareGet(index, type, "1").execute().actionGet(); - client().admin().indices().prepareFlush(index).execute().actionGet(); - GetResponse responseAfterFlush = client().prepareGet(index, type, "1").execute().actionGet(); + GetResponse responseBeforeFlush = client().prepareGet(index, type, "1").get(); + flush(); + GetResponse responseAfterFlush = client().prepareGet(index, type, "1").get(); assertThat(responseBeforeFlush.isExists(), is(true)); assertThat(responseAfterFlush.isExists(), is(true)); @@ -483,10 +479,9 @@ public class GetActionTests extends ElasticsearchIntegrationTest { .endObject() .string(); - client().admin().indices().prepareCreate(index) + assertAcked(prepareCreate(index) .addMapping(type, mapping) - .setSettings(ImmutableSettings.settingsBuilder().put("index.refresh_interval", -1)) - .execute().actionGet(); + .setSettings(ImmutableSettings.settingsBuilder().put("index.refresh_interval", -1))); client().prepareIndex(index, type, "1") .setSource(jsonBuilder().startObject() @@ -494,9 +489,9 @@ public class GetActionTests extends ElasticsearchIntegrationTest { .startObject("included").field("field", "should be seen").field("field2", "extra field to remove").endObject() .startObject("excluded").field("field", "should not be seen").field("field2", "should not be seen").endObject() .endObject()) - .execute().actionGet(); + .get(); - GetResponse responseBeforeFlush = client().prepareGet(index, type, "1").setFields("_source", "included.field", "excluded.field").execute().actionGet(); + GetResponse responseBeforeFlush = client().prepareGet(index, type, "1").setFields("_source", "included.field", "excluded.field").get(); assertThat(responseBeforeFlush.isExists(), is(true)); assertThat(responseBeforeFlush.getSourceAsMap(), not(hasKey("excluded"))); assertThat(responseBeforeFlush.getSourceAsMap(), not(hasKey("field"))); @@ -512,8 +507,8 @@ public class GetActionTests extends ElasticsearchIntegrationTest { assertThat((Map) responseBeforeFlushWithExtraFilters.getSourceAsMap().get("included"), hasKey("field")); assertThat((Map) responseBeforeFlushWithExtraFilters.getSourceAsMap().get("included"), not(hasKey("field2"))); - client().admin().indices().prepareFlush(index).execute().actionGet(); - GetResponse responseAfterFlush = client().prepareGet(index, type, "1").setFields("_source", "included.field", "excluded.field").execute().actionGet(); + flush(); + GetResponse responseAfterFlush = client().prepareGet(index, type, "1").setFields("_source", "included.field", "excluded.field").get(); GetResponse responseAfterFlushWithExtraFilters = client().prepareGet(index, type, "1").setFields("included.field", "excluded.field") .setFetchSource("*.field", "*.field2").get(); @@ -526,133 +521,137 @@ public class GetActionTests extends ElasticsearchIntegrationTest { @Test public void testGetWithVersion() { - client().admin().indices().prepareCreate("test").setSettings(ImmutableSettings.settingsBuilder().put("index.refresh_interval", -1)).execute().actionGet(); - ClusterHealthResponse clusterHealth = client().admin().cluster().health(clusterHealthRequest().waitForGreenStatus()).actionGet(); - assertThat(clusterHealth.isTimedOut(), equalTo(false)); - assertThat(clusterHealth.getStatus(), equalTo(ClusterHealthStatus.GREEN)); + assertAcked(prepareCreate("test").addAlias(new Alias("alias")) + .setSettings(ImmutableSettings.settingsBuilder().put("index.refresh_interval", -1))); + ensureGreen(); - GetResponse response = client().prepareGet("test", "type1", "1").execute().actionGet(); + GetResponse response = client().prepareGet("test", "type1", "1").get(); assertThat(response.isExists(), equalTo(false)); logger.info("--> index doc 1"); - client().prepareIndex("test", "type1", "1").setSource("field1", "value1", "field2", "value2").execute().actionGet(); + client().prepareIndex("test", "type1", "1").setSource("field1", "value1", "field2", "value2").get(); // From translog: // version 0 means ignore version, which is the default - response = client().prepareGet("test", "type1", "1").setVersion(0).execute().actionGet(); + response = client().prepareGet(indexOrAlias(), "type1", "1").setVersion(0).get(); assertThat(response.isExists(), equalTo(true)); assertThat(response.getId(), equalTo("1")); assertThat(response.getVersion(), equalTo(1l)); - response = client().prepareGet("test", "type1", "1").setVersion(1).execute().actionGet(); + response = client().prepareGet(indexOrAlias(), "type1", "1").setVersion(1).get(); assertThat(response.isExists(), equalTo(true)); assertThat(response.getId(), equalTo("1")); assertThat(response.getVersion(), equalTo(1l)); try { - client().prepareGet("test", "type1", "1").setVersion(2).execute().actionGet(); + client().prepareGet(indexOrAlias(), "type1", "1").setVersion(2).get(); fail(); } catch (VersionConflictEngineException e) { + //all good } // From Lucene index: - client().admin().indices().prepareRefresh("test").execute().actionGet(); + refresh(); // version 0 means ignore version, which is the default - response = client().prepareGet("test", "type1", "1").setVersion(0).setRealtime(false).execute().actionGet(); + response = client().prepareGet(indexOrAlias(), "type1", "1").setVersion(0).setRealtime(false).get(); assertThat(response.isExists(), equalTo(true)); assertThat(response.getId(), equalTo("1")); + assertThat(response.getIndex(), equalTo("test")); assertThat(response.getVersion(), equalTo(1l)); - response = client().prepareGet("test", "type1", "1").setVersion(1).setRealtime(false).execute().actionGet(); + response = client().prepareGet(indexOrAlias(), "type1", "1").setVersion(1).setRealtime(false).get(); assertThat(response.isExists(), equalTo(true)); assertThat(response.getId(), equalTo("1")); + assertThat(response.getIndex(), equalTo("test")); assertThat(response.getVersion(), equalTo(1l)); try { - client().prepareGet("test", "type1", "1").setVersion(2).setRealtime(false).execute().actionGet(); + client().prepareGet(indexOrAlias(), "type1", "1").setVersion(2).setRealtime(false).get(); fail(); } catch (VersionConflictEngineException e) { + //all good } logger.info("--> index doc 1 again, so increasing the version"); - client().prepareIndex("test", "type1", "1").setSource("field1", "value1", "field2", "value2").execute().actionGet(); + client().prepareIndex("test", "type1", "1").setSource("field1", "value1", "field2", "value2").get(); // From translog: // version 0 means ignore version, which is the default - response = client().prepareGet("test", "type1", "1").setVersion(0).execute().actionGet(); + response = client().prepareGet(indexOrAlias(), "type1", "1").setVersion(0).get(); assertThat(response.isExists(), equalTo(true)); assertThat(response.getId(), equalTo("1")); + assertThat(response.getIndex(), equalTo("test")); assertThat(response.getVersion(), equalTo(2l)); try { - client().prepareGet("test", "type1", "1").setVersion(1).execute().actionGet(); + client().prepareGet(indexOrAlias(), "type1", "1").setVersion(1).get(); fail(); } catch (VersionConflictEngineException e) { + //all good } - response = client().prepareGet("test", "type1", "1").setVersion(2).execute().actionGet(); + response = client().prepareGet(indexOrAlias(), "type1", "1").setVersion(2).get(); assertThat(response.isExists(), equalTo(true)); assertThat(response.getId(), equalTo("1")); + assertThat(response.getIndex(), equalTo("test")); assertThat(response.getVersion(), equalTo(2l)); // From Lucene index: - client().admin().indices().prepareRefresh("test").execute().actionGet(); + refresh(); // version 0 means ignore version, which is the default - response = client().prepareGet("test", "type1", "1").setVersion(0).setRealtime(false).execute().actionGet(); + response = client().prepareGet(indexOrAlias(), "type1", "1").setVersion(0).setRealtime(false).get(); assertThat(response.isExists(), equalTo(true)); assertThat(response.getId(), equalTo("1")); + assertThat(response.getIndex(), equalTo("test")); assertThat(response.getVersion(), equalTo(2l)); try { - client().prepareGet("test", "type1", "1").setVersion(1).setRealtime(false).execute().actionGet(); + client().prepareGet(indexOrAlias(), "type1", "1").setVersion(1).setRealtime(false).get(); fail(); } catch (VersionConflictEngineException e) { + //all good } - response = client().prepareGet("test", "type1", "1").setVersion(2).setRealtime(false).execute().actionGet(); + response = client().prepareGet(indexOrAlias(), "type1", "1").setVersion(2).setRealtime(false).get(); assertThat(response.isExists(), equalTo(true)); assertThat(response.getId(), equalTo("1")); + assertThat(response.getIndex(), equalTo("test")); assertThat(response.getVersion(), equalTo(2l)); } @Test public void testMultiGetWithVersion() throws Exception { - try { - client().admin().indices().prepareDelete("test").execute().actionGet(); - } catch (Exception e) { - // fine - } - client().admin().indices().prepareCreate("test").setSettings(ImmutableSettings.settingsBuilder().put("index.refresh_interval", -1)).execute().actionGet(); + assertAcked(prepareCreate("test").addAlias(new Alias("alias")) + .setSettings(ImmutableSettings.settingsBuilder().put("index.refresh_interval", -1))); + ensureGreen(); - ClusterHealthResponse clusterHealth = client().admin().cluster().health(clusterHealthRequest().waitForGreenStatus()).actionGet(); - assertThat(clusterHealth.isTimedOut(), equalTo(false)); - assertThat(clusterHealth.getStatus(), equalTo(ClusterHealthStatus.GREEN)); - - MultiGetResponse response = client().prepareMultiGet().add("test", "type1", "1").execute().actionGet(); + MultiGetResponse response = client().prepareMultiGet().add(indexOrAlias(), "type1", "1").get(); assertThat(response.getResponses().length, equalTo(1)); assertThat(response.getResponses()[0].getResponse().isExists(), equalTo(false)); for (int i = 0; i < 3; i++) { - client().prepareIndex("test", "type1", Integer.toString(i)).setSource("field", "value" + i).execute().actionGet(); + client().prepareIndex("test", "type1", Integer.toString(i)).setSource("field", "value" + i).get(); } // Version from translog response = client().prepareMultiGet() - .add(new MultiGetRequest.Item("test", "type1", "1").version(0)) - .add(new MultiGetRequest.Item("test", "type1", "1").version(1)) - .add(new MultiGetRequest.Item("test", "type1", "1").version(2)) - .execute().actionGet(); + .add(new MultiGetRequest.Item(indexOrAlias(), "type1", "1").version(0)) + .add(new MultiGetRequest.Item(indexOrAlias(), "type1", "1").version(1)) + .add(new MultiGetRequest.Item(indexOrAlias(), "type1", "1").version(2)) + .get(); assertThat(response.getResponses().length, equalTo(3)); // [0] version doesn't matter, which is the default assertThat(response.getResponses()[0].getFailure(), nullValue()); assertThat(response.getResponses()[0].getId(), equalTo("1")); + assertThat(response.getResponses()[0].getIndex(), equalTo("test")); assertThat(response.getResponses()[0].getResponse().isExists(), equalTo(true)); assertThat(response.getResponses()[0].getResponse().getSourceAsMap().get("field").toString(), equalTo("value1")); assertThat(response.getResponses()[1].getId(), equalTo("1")); + assertThat(response.getResponses()[1].getIndex(), equalTo("test")); assertThat(response.getResponses()[1].getFailure(), nullValue()); assertThat(response.getResponses()[1].getResponse().isExists(), equalTo(true)); assertThat(response.getResponses()[1].getResponse().getSourceAsMap().get("field").toString(), equalTo("value1")); @@ -661,13 +660,13 @@ public class GetActionTests extends ElasticsearchIntegrationTest { assertThat(response.getResponses()[2].getFailure().getMessage(), startsWith("VersionConflictEngineException")); //Version from Lucene index - client().admin().indices().prepareRefresh("test").execute().actionGet(); + refresh(); response = client().prepareMultiGet() - .add(new MultiGetRequest.Item("test", "type1", "1").version(0)) - .add(new MultiGetRequest.Item("test", "type1", "1").version(1)) - .add(new MultiGetRequest.Item("test", "type1", "1").version(2)) + .add(new MultiGetRequest.Item(indexOrAlias(), "type1", "1").version(0)) + .add(new MultiGetRequest.Item(indexOrAlias(), "type1", "1").version(1)) + .add(new MultiGetRequest.Item(indexOrAlias(), "type1", "1").version(2)) .setRealtime(false) - .execute().actionGet(); + .get(); assertThat(response.getResponses().length, equalTo(3)); // [0] version doesn't matter, which is the default assertThat(response.getResponses()[0].getFailure(), nullValue()); @@ -684,48 +683,54 @@ public class GetActionTests extends ElasticsearchIntegrationTest { for (int i = 0; i < 3; i++) { - client().prepareIndex("test", "type1", Integer.toString(i)).setSource("field", "value" + i).execute().actionGet(); + client().prepareIndex("test", "type1", Integer.toString(i)).setSource("field", "value" + i).get(); } // Version from translog response = client().prepareMultiGet() - .add(new MultiGetRequest.Item("test", "type1", "2").version(0)) - .add(new MultiGetRequest.Item("test", "type1", "2").version(1)) - .add(new MultiGetRequest.Item("test", "type1", "2").version(2)) - .execute().actionGet(); + .add(new MultiGetRequest.Item(indexOrAlias(), "type1", "2").version(0)) + .add(new MultiGetRequest.Item(indexOrAlias(), "type1", "2").version(1)) + .add(new MultiGetRequest.Item(indexOrAlias(), "type1", "2").version(2)) + .get(); assertThat(response.getResponses().length, equalTo(3)); // [0] version doesn't matter, which is the default assertThat(response.getResponses()[0].getFailure(), nullValue()); assertThat(response.getResponses()[0].getId(), equalTo("2")); + assertThat(response.getResponses()[0].getIndex(), equalTo("test")); assertThat(response.getResponses()[0].getResponse().isExists(), equalTo(true)); assertThat(response.getResponses()[0].getResponse().getSourceAsMap().get("field").toString(), equalTo("value2")); assertThat(response.getResponses()[1].getFailure(), notNullValue()); assertThat(response.getResponses()[1].getFailure().getId(), equalTo("2")); + assertThat(response.getResponses()[1].getIndex(), equalTo("test")); assertThat(response.getResponses()[1].getFailure().getMessage(), startsWith("VersionConflictEngineException")); assertThat(response.getResponses()[2].getId(), equalTo("2")); + assertThat(response.getResponses()[2].getIndex(), equalTo("test")); assertThat(response.getResponses()[2].getFailure(), nullValue()); assertThat(response.getResponses()[2].getResponse().isExists(), equalTo(true)); assertThat(response.getResponses()[2].getResponse().getSourceAsMap().get("field").toString(), equalTo("value2")); //Version from Lucene index - client().admin().indices().prepareRefresh("test").execute().actionGet(); + refresh(); response = client().prepareMultiGet() - .add(new MultiGetRequest.Item("test", "type1", "2").version(0)) - .add(new MultiGetRequest.Item("test", "type1", "2").version(1)) - .add(new MultiGetRequest.Item("test", "type1", "2").version(2)) + .add(new MultiGetRequest.Item(indexOrAlias(), "type1", "2").version(0)) + .add(new MultiGetRequest.Item(indexOrAlias(), "type1", "2").version(1)) + .add(new MultiGetRequest.Item(indexOrAlias(), "type1", "2").version(2)) .setRealtime(false) - .execute().actionGet(); + .get(); assertThat(response.getResponses().length, equalTo(3)); // [0] version doesn't matter, which is the default assertThat(response.getResponses()[0].getFailure(), nullValue()); assertThat(response.getResponses()[0].getId(), equalTo("2")); + assertThat(response.getResponses()[0].getIndex(), equalTo("test")); assertThat(response.getResponses()[0].getResponse().isExists(), equalTo(true)); assertThat(response.getResponses()[0].getResponse().getSourceAsMap().get("field").toString(), equalTo("value2")); assertThat(response.getResponses()[1].getFailure(), notNullValue()); assertThat(response.getResponses()[1].getFailure().getId(), equalTo("2")); + assertThat(response.getResponses()[1].getIndex(), equalTo("test")); assertThat(response.getResponses()[1].getFailure().getMessage(), startsWith("VersionConflictEngineException")); assertThat(response.getResponses()[2].getId(), equalTo("2")); + assertThat(response.getResponses()[2].getIndex(), equalTo("test")); assertThat(response.getResponses()[2].getFailure(), nullValue()); assertThat(response.getResponses()[2].getResponse().isExists(), equalTo(true)); assertThat(response.getResponses()[2].getResponse().getSourceAsMap().get("field").toString(), equalTo("value2")); @@ -733,16 +738,15 @@ public class GetActionTests extends ElasticsearchIntegrationTest { @Test public void testGetFields_metaData() throws Exception { - client().admin().indices().prepareCreate("my-index") - .setSettings(ImmutableSettings.settingsBuilder().put("index.refresh_interval", -1)) - .get(); + assertAcked(prepareCreate("test").addAlias(new Alias("alias")) + .setSettings(ImmutableSettings.settingsBuilder().put("index.refresh_interval", -1))); - client().prepareIndex("my-index", "my-type1", "1") + client().prepareIndex("test", "my-type1", "1") .setRouting("1") .setSource(jsonBuilder().startObject().field("field1", "value").endObject()) .get(); - GetResponse getResponse = client().prepareGet("my-index", "my-type1", "1") + GetResponse getResponse = client().prepareGet(indexOrAlias(), "my-type1", "1") .setRouting("1") .setFields("field1", "_routing") .get(); @@ -752,9 +756,9 @@ public class GetActionTests extends ElasticsearchIntegrationTest { assertThat(getResponse.getField("_routing").isMetadataField(), equalTo(true)); assertThat(getResponse.getField("_routing").getValue().toString(), equalTo("1")); - client().admin().indices().prepareFlush("my-index").get(); + flush(); - client().prepareGet("my-index", "my-type1", "1") + client().prepareGet(indexOrAlias(), "my-type1", "1") .setFields("field1", "_routing") .setRouting("1") .get(); @@ -767,36 +771,39 @@ public class GetActionTests extends ElasticsearchIntegrationTest { @Test public void testGetFields_nonLeafField() throws Exception { - client().admin().indices().prepareCreate("my-index") + assertAcked(prepareCreate("test").addAlias(new Alias("alias")) .addMapping("my-type1", jsonBuilder().startObject().startObject("my-type1").startObject("properties") .startObject("field1").startObject("properties") .startObject("field2").field("type", "string").endObject() .endObject().endObject() .endObject().endObject().endObject()) - .setSettings(ImmutableSettings.settingsBuilder().put("index.refresh_interval", -1)) - .get(); + .setSettings(ImmutableSettings.settingsBuilder().put("index.refresh_interval", -1))); - client().prepareIndex("my-index", "my-type1", "1") + client().prepareIndex("test", "my-type1", "1") .setSource(jsonBuilder().startObject().startObject("field1").field("field2", "value1").endObject().endObject()) .get(); try { - client().prepareGet("my-index", "my-type1", "1").setFields("field1").get(); + client().prepareGet(indexOrAlias(), "my-type1", "1").setFields("field1").get(); fail(); - } catch (ElasticsearchIllegalArgumentException e) {} + } catch (ElasticsearchIllegalArgumentException e) { + //all well + } - client().admin().indices().prepareFlush("my-index").get(); + flush(); try { - client().prepareGet("my-index", "my-type1", "1").setFields("field1").get(); + client().prepareGet(indexOrAlias(), "my-type1", "1").setFields("field1").get(); fail(); - } catch (ElasticsearchIllegalArgumentException e) {} + } catch (ElasticsearchIllegalArgumentException e) { + //all well + } } @Test @TestLogging("index.shard.service:TRACE,cluster.service:TRACE,action.admin.indices.flush:TRACE") public void testGetFields_complexField() throws Exception { - client().admin().indices().prepareCreate("my-index") + assertAcked(prepareCreate("my-index") .setSettings(ImmutableSettings.settingsBuilder().put("index.refresh_interval", -1)) .addMapping("my-type2", jsonBuilder().startObject().startObject("my-type2").startObject("properties") .startObject("field1").field("type", "object") @@ -806,8 +813,7 @@ public class GetActionTests extends ElasticsearchIntegrationTest { .endObject() .endObject() .endObject() - .endObject().endObject().endObject()) - .get(); + .endObject().endObject().endObject())); BytesReference source = jsonBuilder().startObject() .startArray("field1") @@ -889,7 +895,8 @@ public class GetActionTests extends ElasticsearchIntegrationTest { @Test public void testGet_allField() throws Exception { - prepareCreate("my-index") + assertAcked(prepareCreate("test") + .addAlias(new Alias("alias")) .addMapping("my-type1", jsonBuilder() .startObject() .startObject("my-type1") @@ -902,12 +909,11 @@ public class GetActionTests extends ElasticsearchIntegrationTest { .endObject() .endObject() .endObject() - .endObject()) - .get(); - index("my-index", "my-type1", "1", "some_field", "some text"); + .endObject())); + index("test", "my-type1", "1", "some_field", "some text"); refresh(); - GetResponse getResponse = client().prepareGet("my-index", "my-type1", "1").setFields("_all").get(); + GetResponse getResponse = client().prepareGet(indexOrAlias(), "my-type1", "1").setFields("_all").get(); assertNotNull(getResponse.getField("_all").getValue()); assertThat(getResponse.getField("_all").getValue().toString(), equalTo("some text" + " ")); } @@ -932,7 +938,7 @@ public class GetActionTests extends ElasticsearchIntegrationTest { " }\n" + " }\n" + "}"; - assertAcked(prepareCreate("testidx").setSource(createIndexSource)); + assertAcked(prepareCreate("test").addAlias(new Alias("alias")).setSource(createIndexSource)); ensureGreen(); String doc = "{\n" + " \"suggest\": {\n" + @@ -944,16 +950,16 @@ public class GetActionTests extends ElasticsearchIntegrationTest { " }\n" + "}"; - index("testidx", "doc", "1", doc); + index("test", "doc", "1", doc); String[] fieldsList = {"suggest"}; // before refresh - document is only in translog - assertGetFieldsAlwaysNull("testidx", "doc", "1", fieldsList); + assertGetFieldsAlwaysNull(indexOrAlias(), "doc", "1", fieldsList); refresh(); //after refresh - document is in translog and also indexed - assertGetFieldsAlwaysNull("testidx", "doc", "1", fieldsList); + assertGetFieldsAlwaysNull(indexOrAlias(), "doc", "1", fieldsList); flush(); //after flush - document is in not anymore translog - only indexed - assertGetFieldsAlwaysNull("testidx", "doc", "1", fieldsList); + assertGetFieldsAlwaysNull(indexOrAlias(), "doc", "1", fieldsList); } @Test @@ -981,23 +987,23 @@ public class GetActionTests extends ElasticsearchIntegrationTest { " }\n" + " }\n" + "}"; - assertAcked(prepareCreate("testidx").setSource(createIndexSource)); + assertAcked(prepareCreate("test").addAlias(new Alias("alias")).setSource(createIndexSource)); ensureGreen(); String doc = "{\n" + " \"_ttl\": \"1h\"\n" + "}"; - client().prepareIndex("testidx", "doc").setId("1").setSource(doc).setParent("1").execute().actionGet(); + client().prepareIndex("test", "doc").setId("1").setSource(doc).setParent("1").get(); String[] fieldsList = {"_ttl", "_parent"}; // before refresh - document is only in translog - assertGetFieldsAlwaysWorks("testidx", "doc", "1", fieldsList, "1"); + assertGetFieldsAlwaysWorks(indexOrAlias(), "doc", "1", fieldsList, "1"); refresh(); //after refresh - document is in translog and also indexed - assertGetFieldsAlwaysWorks("testidx", "doc", "1", fieldsList, "1"); + assertGetFieldsAlwaysWorks(indexOrAlias(), "doc", "1", fieldsList, "1"); flush(); //after flush - document is in not anymore translog - only indexed - assertGetFieldsAlwaysWorks("testidx", "doc", "1", fieldsList, "1"); + assertGetFieldsAlwaysWorks(indexOrAlias(), "doc", "1", fieldsList, "1"); } @Test @@ -1005,13 +1011,13 @@ public class GetActionTests extends ElasticsearchIntegrationTest { indexSingleDocumentWithUngeneratedFieldsThatArePartOf_source(false, false); String[] fieldsList = {"my_boost"}; // before refresh - document is only in translog - assertGetFieldsAlwaysNull("testidx", "doc", "1", fieldsList); + assertGetFieldsAlwaysNull(indexOrAlias(), "doc", "1", fieldsList); refresh(); //after refresh - document is in translog and also indexed - assertGetFieldsAlwaysNull("testidx", "doc", "1", fieldsList); + assertGetFieldsAlwaysNull(indexOrAlias(), "doc", "1", fieldsList); flush(); //after flush - document is in not anymore translog - only indexed - assertGetFieldsAlwaysNull("testidx", "doc", "1", fieldsList); + assertGetFieldsAlwaysNull(indexOrAlias(), "doc", "1", fieldsList); } @Test @@ -1024,13 +1030,13 @@ public class GetActionTests extends ElasticsearchIntegrationTest { indexSingleDocumentWithUngeneratedFieldsThatArePartOf_source(stored, sourceEnabled); String[] fieldsList = {"my_boost"}; // before refresh - document is only in translog - assertGetFieldsAlwaysWorks("testidx", "doc", "1", fieldsList); + assertGetFieldsAlwaysWorks(indexOrAlias(), "doc", "1", fieldsList); refresh(); //after refresh - document is in translog and also indexed - assertGetFieldsAlwaysWorks("testidx", "doc", "1", fieldsList); + assertGetFieldsAlwaysWorks(indexOrAlias(), "doc", "1", fieldsList); flush(); //after flush - document is in not anymore translog - only indexed - assertGetFieldsAlwaysWorks("testidx", "doc", "1", fieldsList); + assertGetFieldsAlwaysWorks(indexOrAlias(), "doc", "1", fieldsList); } void indexSingleDocumentWithUngeneratedFieldsThatArePartOf_source(boolean stored, boolean sourceEnabled) { @@ -1053,14 +1059,14 @@ public class GetActionTests extends ElasticsearchIntegrationTest { " }\n" + " }\n" + "}"; - assertAcked(prepareCreate("testidx").setSource(createIndexSource)); + assertAcked(prepareCreate("test").addAlias(new Alias("alias")).setSource(createIndexSource)); ensureGreen(); String doc = "{\n" + " \"my_boost\": 5.0,\n" + " \"_ttl\": \"1h\"\n" + "}\n"; - client().prepareIndex("testidx", "doc").setId("1").setSource(doc).setRouting("1").execute().actionGet(); + client().prepareIndex("test", "doc").setId("1").setSource(doc).setRouting("1").get(); } @@ -1069,13 +1075,13 @@ public class GetActionTests extends ElasticsearchIntegrationTest { indexSingleDocumentWithUngeneratedFieldsThatAreNeverPartOf_source(false, randomBoolean()); String[] fieldsList = {"_timestamp", "_size", "_routing"}; // before refresh - document is only in translog - assertGetFieldsAlwaysNull("testidx", "doc", "1", fieldsList, "1"); + assertGetFieldsAlwaysNull(indexOrAlias(), "doc", "1", fieldsList, "1"); refresh(); //after refresh - document is in translog and also indexed - assertGetFieldsAlwaysNull("testidx", "doc", "1", fieldsList, "1"); + assertGetFieldsAlwaysNull(indexOrAlias(), "doc", "1", fieldsList, "1"); flush(); //after flush - document is in not anymore translog - only indexed - assertGetFieldsAlwaysNull("testidx", "doc", "1", fieldsList, "1"); + assertGetFieldsAlwaysNull(indexOrAlias(), "doc", "1", fieldsList, "1"); } @Test @@ -1083,13 +1089,13 @@ public class GetActionTests extends ElasticsearchIntegrationTest { indexSingleDocumentWithUngeneratedFieldsThatAreNeverPartOf_source(true, randomBoolean()); String[] fieldsList = {"_timestamp", "_size", "_routing"}; // before refresh - document is only in translog - assertGetFieldsAlwaysWorks("testidx", "doc", "1", fieldsList, "1"); + assertGetFieldsAlwaysWorks(indexOrAlias(), "doc", "1", fieldsList, "1"); refresh(); //after refresh - document is in translog and also indexed - assertGetFieldsAlwaysWorks("testidx", "doc", "1", fieldsList, "1"); + assertGetFieldsAlwaysWorks(indexOrAlias(), "doc", "1", fieldsList, "1"); flush(); //after flush - document is in not anymore translog - only indexed - assertGetFieldsAlwaysWorks("testidx", "doc", "1", fieldsList, "1"); + assertGetFieldsAlwaysWorks(indexOrAlias(), "doc", "1", fieldsList, "1"); } void indexSingleDocumentWithUngeneratedFieldsThatAreNeverPartOf_source(boolean stored, boolean sourceEnabled) { @@ -1117,12 +1123,12 @@ public class GetActionTests extends ElasticsearchIntegrationTest { " }\n" + "}"; - assertAcked(prepareCreate("testidx").setSource(createIndexSource)); + assertAcked(prepareCreate("test").addAlias(new Alias("alias")).setSource(createIndexSource)); ensureGreen(); String doc = "{\n" + " \"text\": \"some text.\"\n" + "}\n"; - client().prepareIndex("testidx", "doc").setId("1").setSource(doc).setRouting("1").execute().actionGet(); + client().prepareIndex("test", "doc").setId("1").setSource(doc).setRouting("1").get(); } @@ -1131,13 +1137,13 @@ public class GetActionTests extends ElasticsearchIntegrationTest { indexSingleDocumentWithStringFieldsGeneratedFromText(false, randomBoolean()); String[] fieldsList = {"_all", "_field_names"}; // before refresh - document is only in translog - assertGetFieldsAlwaysNull("testidx", "doc", "1", fieldsList); + assertGetFieldsAlwaysNull(indexOrAlias(), "doc", "1", fieldsList); refresh(); //after refresh - document is in translog and also indexed - assertGetFieldsAlwaysNull("testidx", "doc", "1", fieldsList); + assertGetFieldsAlwaysNull(indexOrAlias(), "doc", "1", fieldsList); flush(); //after flush - document is in not anymore translog - only indexed - assertGetFieldsAlwaysNull("testidx", "doc", "1", fieldsList); + assertGetFieldsAlwaysNull(indexOrAlias(), "doc", "1", fieldsList); } @Test @@ -1145,14 +1151,14 @@ public class GetActionTests extends ElasticsearchIntegrationTest { indexSingleDocumentWithStringFieldsGeneratedFromText(true, randomBoolean()); String[] fieldsList = {"_all", "_field_names"}; // before refresh - document is only in translog - assertGetFieldsNull("testidx", "doc", "1", fieldsList); - assertGetFieldsException("testidx", "doc", "1", fieldsList); + assertGetFieldsNull(indexOrAlias(), "doc", "1", fieldsList); + assertGetFieldsException(indexOrAlias(), "doc", "1", fieldsList); refresh(); //after refresh - document is in translog and also indexed - assertGetFieldsAlwaysWorks("testidx", "doc", "1", fieldsList); + assertGetFieldsAlwaysWorks(indexOrAlias(), "doc", "1", fieldsList); flush(); //after flush - document is in not anymore translog - only indexed - assertGetFieldsAlwaysWorks("testidx", "doc", "1", fieldsList); + assertGetFieldsAlwaysWorks(indexOrAlias(), "doc", "1", fieldsList); } void indexSingleDocumentWithStringFieldsGeneratedFromText(boolean stored, boolean sourceEnabled) { @@ -1172,13 +1178,13 @@ public class GetActionTests extends ElasticsearchIntegrationTest { " }\n" + "}"; - assertAcked(prepareCreate("testidx").setSource(createIndexSource)); + assertAcked(prepareCreate("test").addAlias(new Alias("alias")).setSource(createIndexSource)); ensureGreen(); String doc = "{\n" + " \"text1\": \"some text.\"\n," + " \"text2\": \"more text.\"\n" + "}\n"; - index("testidx", "doc", "1", doc); + index("test", "doc", "1", doc); } @@ -1187,13 +1193,13 @@ public class GetActionTests extends ElasticsearchIntegrationTest { indexSingleDocumentWithNumericFieldsGeneratedFromText(false, randomBoolean()); String[] fieldsList = {"token_count", "text.token_count", "murmur", "text.murmur"}; // before refresh - document is only in translog - assertGetFieldsAlwaysNull("testidx", "doc", "1", fieldsList); + assertGetFieldsAlwaysNull(indexOrAlias(), "doc", "1", fieldsList); refresh(); //after refresh - document is in translog and also indexed - assertGetFieldsAlwaysNull("testidx", "doc", "1", fieldsList); + assertGetFieldsAlwaysNull(indexOrAlias(), "doc", "1", fieldsList); flush(); //after flush - document is in not anymore translog - only indexed - assertGetFieldsAlwaysNull("testidx", "doc", "1", fieldsList); + assertGetFieldsAlwaysNull(indexOrAlias(), "doc", "1", fieldsList); } @Test @@ -1201,14 +1207,14 @@ public class GetActionTests extends ElasticsearchIntegrationTest { indexSingleDocumentWithNumericFieldsGeneratedFromText(true, randomBoolean()); String[] fieldsList = {"token_count", "text.token_count", "murmur", "text.murmur"}; // before refresh - document is only in translog - assertGetFieldsNull("testidx", "doc", "1", fieldsList); - assertGetFieldsException("testidx", "doc", "1", fieldsList); + assertGetFieldsNull(indexOrAlias(), "doc", "1", fieldsList); + assertGetFieldsException(indexOrAlias(), "doc", "1", fieldsList); refresh(); //after refresh - document is in translog and also indexed - assertGetFieldsAlwaysWorks("testidx", "doc", "1", fieldsList); + assertGetFieldsAlwaysWorks(indexOrAlias(), "doc", "1", fieldsList); flush(); //after flush - document is in not anymore translog - only indexed - assertGetFieldsAlwaysWorks("testidx", "doc", "1", fieldsList); + assertGetFieldsAlwaysWorks(indexOrAlias(), "doc", "1", fieldsList); } void indexSingleDocumentWithNumericFieldsGeneratedFromText(boolean stored, boolean sourceEnabled) { @@ -1250,14 +1256,14 @@ public class GetActionTests extends ElasticsearchIntegrationTest { " }\n" + "}"; - assertAcked(prepareCreate("testidx").setSource(createIndexSource)); + assertAcked(prepareCreate("test").addAlias(new Alias("alias")).setSource(createIndexSource)); ensureGreen(); String doc = "{\n" + " \"murmur\": \"Some value that can be hashed\",\n" + " \"token_count\": \"A text with five words.\",\n" + " \"text\": \"A text with five words.\"\n" + "}\n"; - index("testidx", "doc", "1", doc); + index("test", "doc", "1", doc); } private void assertGetFieldsAlwaysWorks(String index, String type, String docId, String[] fields) { diff --git a/src/test/java/org/elasticsearch/index/mapper/timestamp/TimestampMappingTests.java b/src/test/java/org/elasticsearch/index/mapper/timestamp/TimestampMappingTests.java index 60fbfe46086..2f1c0ca3941 100644 --- a/src/test/java/org/elasticsearch/index/mapper/timestamp/TimestampMappingTests.java +++ b/src/test/java/org/elasticsearch/index/mapper/timestamp/TimestampMappingTests.java @@ -183,7 +183,7 @@ public class TimestampMappingTests extends ElasticsearchSingleNodeTest { MappingMetaData mappingMetaData = new MappingMetaData(docMapper); IndexRequest request = new IndexRequest("test", "type", "1").source(doc); - request.process(metaData, null, mappingMetaData, true); + request.process(metaData, mappingMetaData, true, "test"); assertThat(request.timestamp(), notNullValue()); // We should have less than one minute (probably some ms) @@ -209,7 +209,7 @@ public class TimestampMappingTests extends ElasticsearchSingleNodeTest { MappingMetaData mappingMetaData = new MappingMetaData(docMapper); IndexRequest request = new IndexRequest("test", "type", "1").source(doc); - request.process(metaData, null, mappingMetaData, true); + request.process(metaData, mappingMetaData, true, "test"); assertThat(request.timestamp(), notNullValue()); // We should have less than one minute (probably some ms) @@ -238,7 +238,7 @@ public class TimestampMappingTests extends ElasticsearchSingleNodeTest { MappingMetaData mappingMetaData = new MappingMetaData(docMapper); IndexRequest request = new IndexRequest("test", "type", "1").source(doc); - request.process(metaData, null, mappingMetaData, true); + request.process(metaData, mappingMetaData, true, "test"); assertThat(request.timestamp(), notNullValue()); assertThat(request.timestamp(), is(MappingMetaData.Timestamp.parseStringTimestamp("1970-01-01", Joda.forPattern("YYYY-MM-dd")))); } @@ -263,7 +263,7 @@ public class TimestampMappingTests extends ElasticsearchSingleNodeTest { MappingMetaData mappingMetaData = new MappingMetaData(docMapper); IndexRequest request = new IndexRequest("test", "type", "1").source(doc); - request.process(metaData, null, mappingMetaData, true); + request.process(metaData, mappingMetaData, true, "test"); assertThat(request.timestamp(), notNullValue()); assertThat(request.timestamp(), is(MappingMetaData.Timestamp.parseStringTimestamp("1970-01-01", Joda.forPattern("YYYY-MM-dd")))); } @@ -289,7 +289,7 @@ public class TimestampMappingTests extends ElasticsearchSingleNodeTest { MappingMetaData mappingMetaData = new MappingMetaData(docMapper); IndexRequest request = new IndexRequest("test", "type", "1").source(doc); - request.process(metaData, null, mappingMetaData, true); + request.process(metaData, mappingMetaData, true, "test"); assertThat(request.timestamp(), notNullValue()); // We should have less than one minute (probably some ms) @@ -317,7 +317,7 @@ public class TimestampMappingTests extends ElasticsearchSingleNodeTest { MappingMetaData mappingMetaData = new MappingMetaData(docMapper); IndexRequest request = new IndexRequest("test", "type", "1").source(doc); - request.process(metaData, null, mappingMetaData, true); + request.process(metaData, mappingMetaData, true, "test"); assertThat(request.timestamp(), notNullValue()); // We should have less than one minute (probably some ms) @@ -345,7 +345,7 @@ public class TimestampMappingTests extends ElasticsearchSingleNodeTest { MappingMetaData mappingMetaData = new MappingMetaData(docMapper); IndexRequest request = new IndexRequest("test", "type", "1").source(doc); - request.process(metaData, null, mappingMetaData, true); + request.process(metaData, mappingMetaData, true, "test"); } @Test(expected = TimestampParsingException.class) // Issue 4718: was throwing a TimestampParsingException: failed to parse timestamp [null] @@ -367,9 +367,10 @@ public class TimestampMappingTests extends ElasticsearchSingleNodeTest { MappingMetaData mappingMetaData = new MappingMetaData(docMapper); IndexRequest request = new IndexRequest("test", "type", "1").source(doc); - request.process(metaData, null, mappingMetaData, true); + request.process(metaData, mappingMetaData, true, "test"); } + @Test public void testDefaultTimestampStream() throws IOException { // Testing null value for default timestamp { diff --git a/src/test/java/org/elasticsearch/indices/analyze/AnalyzeActionTests.java b/src/test/java/org/elasticsearch/indices/analyze/AnalyzeActionTests.java index 48dfeded927..faf0b673703 100644 --- a/src/test/java/org/elasticsearch/indices/analyze/AnalyzeActionTests.java +++ b/src/test/java/org/elasticsearch/indices/analyze/AnalyzeActionTests.java @@ -20,9 +20,9 @@ package org.elasticsearch.indices.analyze; import org.elasticsearch.ElasticsearchException; import org.elasticsearch.ElasticsearchIllegalArgumentException; +import org.elasticsearch.action.admin.indices.alias.Alias; import org.elasticsearch.action.admin.indices.analyze.AnalyzeRequestBuilder; import org.elasticsearch.action.admin.indices.analyze.AnalyzeResponse; -import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.test.ElasticsearchIntegrationTest; import org.junit.Test; @@ -30,9 +30,7 @@ import java.io.IOException; import static org.elasticsearch.common.settings.ImmutableSettings.settingsBuilder; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.hasSize; -import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.*; /** * @@ -41,11 +39,11 @@ public class AnalyzeActionTests extends ElasticsearchIntegrationTest { @Test public void simpleAnalyzerTests() throws Exception { - createIndex("test"); + assertAcked(prepareCreate("test").addAlias(new Alias("alias"))); ensureGreen(); for (int i = 0; i < 10; i++) { - AnalyzeResponse analyzeResponse = client().admin().indices().prepareAnalyze("test", "this is a test").execute().actionGet(); + AnalyzeResponse analyzeResponse = client().admin().indices().prepareAnalyze(indexOrAlias(), "this is a test").get(); assertThat(analyzeResponse.getTokens().size(), equalTo(4)); AnalyzeResponse.AnalyzeToken token = analyzeResponse.getTokens().get(0); assertThat(token.getTerm(), equalTo("this")); @@ -68,38 +66,34 @@ public class AnalyzeActionTests extends ElasticsearchIntegrationTest { @Test public void analyzeNumericField() throws ElasticsearchException, IOException { - createIndex("test"); - ensureGreen(); - - client().prepareIndex("test", "test", "1") - .setSource(XContentFactory.jsonBuilder() - .startObject() - .field("long", 1l) - .field("double", 1.0d) - .endObject()) - .setRefresh(true).execute().actionGet(); + assertAcked(prepareCreate("test").addAlias(new Alias("alias")).addMapping("test", "long", "type=long", "double", "type=double")); + ensureGreen("test"); try { - client().admin().indices().prepareAnalyze("test", "123").setField("long").execute().actionGet(); + client().admin().indices().prepareAnalyze(indexOrAlias(), "123").setField("long").get(); + fail("shouldn't get here"); } catch (ElasticsearchIllegalArgumentException ex) { + //all good } try { - client().admin().indices().prepareAnalyze("test", "123.0").setField("double").execute().actionGet(); + client().admin().indices().prepareAnalyze(indexOrAlias(), "123.0").setField("double").get(); + fail("shouldn't get here"); } catch (ElasticsearchIllegalArgumentException ex) { + //all good } } @Test public void analyzeWithNoIndex() throws Exception { - AnalyzeResponse analyzeResponse = client().admin().indices().prepareAnalyze("THIS IS A TEST").setAnalyzer("simple").execute().actionGet(); + AnalyzeResponse analyzeResponse = client().admin().indices().prepareAnalyze("THIS IS A TEST").setAnalyzer("simple").get(); assertThat(analyzeResponse.getTokens().size(), equalTo(4)); - analyzeResponse = client().admin().indices().prepareAnalyze("THIS IS A TEST").setTokenizer("keyword").setTokenFilters("lowercase").execute().actionGet(); + analyzeResponse = client().admin().indices().prepareAnalyze("THIS IS A TEST").setTokenizer("keyword").setTokenFilters("lowercase").get(); assertThat(analyzeResponse.getTokens().size(), equalTo(1)); assertThat(analyzeResponse.getTokens().get(0).getTerm(), equalTo("this is a test")); - analyzeResponse = client().admin().indices().prepareAnalyze("THIS IS A TEST").setTokenizer("standard").setTokenFilters("lowercase", "reverse").execute().actionGet(); + analyzeResponse = client().admin().indices().prepareAnalyze("THIS IS A TEST").setTokenizer("standard").setTokenFilters("lowercase", "reverse").get(); assertThat(analyzeResponse.getTokens().size(), equalTo(4)); AnalyzeResponse.AnalyzeToken token = analyzeResponse.getTokens().get(0); assertThat(token.getTerm(), equalTo("siht")); @@ -114,26 +108,26 @@ public class AnalyzeActionTests extends ElasticsearchIntegrationTest { @Test public void analyzeWithCharFilters() throws Exception { - assertAcked(prepareCreate("test").setSettings(settingsBuilder() - .put(indexSettings()) - .put("index.analysis.char_filter.custom_mapping.type", "mapping") - .putArray("index.analysis.char_filter.custom_mapping.mappings", "ph=>f", "qu=>q") - .put("index.analysis.analyzer.custom_with_char_filter.tokenizer", "standard") - .putArray("index.analysis.analyzer.custom_with_char_filter.char_filter", "custom_mapping"))); + assertAcked(prepareCreate("test").addAlias(new Alias("alias")) + .setSettings(settingsBuilder().put(indexSettings()) + .put("index.analysis.char_filter.custom_mapping.type", "mapping") + .putArray("index.analysis.char_filter.custom_mapping.mappings", "ph=>f", "qu=>q") + .put("index.analysis.analyzer.custom_with_char_filter.tokenizer", "standard") + .putArray("index.analysis.analyzer.custom_with_char_filter.char_filter", "custom_mapping"))); ensureGreen(); - AnalyzeResponse analyzeResponse = client().admin().indices().prepareAnalyze("

THIS IS A

TEST").setTokenizer("standard").setCharFilters("html_strip").execute().actionGet(); + AnalyzeResponse analyzeResponse = client().admin().indices().prepareAnalyze("

THIS IS A

TEST").setTokenizer("standard").setCharFilters("html_strip").get(); assertThat(analyzeResponse.getTokens().size(), equalTo(4)); - analyzeResponse = client().admin().indices().prepareAnalyze("THIS IS A TEST").setTokenizer("keyword").setTokenFilters("lowercase").setCharFilters("html_strip").execute().actionGet(); + analyzeResponse = client().admin().indices().prepareAnalyze("THIS IS A TEST").setTokenizer("keyword").setTokenFilters("lowercase").setCharFilters("html_strip").get(); assertThat(analyzeResponse.getTokens().size(), equalTo(1)); assertThat(analyzeResponse.getTokens().get(0).getTerm(), equalTo("this is a test")); - analyzeResponse = client().admin().indices().prepareAnalyze("test", "jeff quit phish").setTokenizer("keyword").setTokenFilters("lowercase").setCharFilters("custom_mapping").execute().actionGet(); + analyzeResponse = client().admin().indices().prepareAnalyze(indexOrAlias(), "jeff quit phish").setTokenizer("keyword").setTokenFilters("lowercase").setCharFilters("custom_mapping").get(); assertThat(analyzeResponse.getTokens().size(), equalTo(1)); assertThat(analyzeResponse.getTokens().get(0).getTerm(), equalTo("jeff qit fish")); - analyzeResponse = client().admin().indices().prepareAnalyze("test", "jeff quit fish").setTokenizer("standard").setCharFilters("html_strip", "custom_mapping").execute().actionGet(); + analyzeResponse = client().admin().indices().prepareAnalyze(indexOrAlias(), "jeff quit fish").setTokenizer("standard").setCharFilters("html_strip", "custom_mapping").get(); assertThat(analyzeResponse.getTokens().size(), equalTo(3)); AnalyzeResponse.AnalyzeToken token = analyzeResponse.getTokens().get(0); assertThat(token.getTerm(), equalTo("jeff")); @@ -145,8 +139,7 @@ public class AnalyzeActionTests extends ElasticsearchIntegrationTest { @Test public void analyzerWithFieldOrTypeTests() throws Exception { - - createIndex("test"); + assertAcked(prepareCreate("test").addAlias(new Alias("alias"))); ensureGreen(); client().admin().indices().preparePutMapping("test") @@ -161,12 +154,13 @@ public class AnalyzeActionTests extends ElasticsearchIntegrationTest { " }\n" + " }\n" + "}" - ).execute().actionGet(); + ).get(); for (int i = 0; i < 10; i++) { - final AnalyzeRequestBuilder requestBuilder = client().admin().indices().prepareAnalyze("test", "THIS IS A TEST"); + final AnalyzeRequestBuilder requestBuilder = client().admin().indices().prepareAnalyze("THIS IS A TEST"); + requestBuilder.setIndex(indexOrAlias()); requestBuilder.setField("document.simple"); - AnalyzeResponse analyzeResponse = requestBuilder.execute().actionGet(); + AnalyzeResponse analyzeResponse = requestBuilder.get(); assertThat(analyzeResponse.getTokens().size(), equalTo(4)); AnalyzeResponse.AnalyzeToken token = analyzeResponse.getTokens().get(3); assertThat(token.getTerm(), equalTo("test")); @@ -193,4 +187,8 @@ public class AnalyzeActionTests extends ElasticsearchIntegrationTest { assertThat(response.getTokens().get(i).getTerm(), is(tokens[i])); } } + + private static String indexOrAlias() { + return randomBoolean() ? "test" : "alias"; + } } diff --git a/src/test/java/org/elasticsearch/mget/SimpleMgetTests.java b/src/test/java/org/elasticsearch/mget/SimpleMgetTests.java index 599d345371f..c5025f3e368 100644 --- a/src/test/java/org/elasticsearch/mget/SimpleMgetTests.java +++ b/src/test/java/org/elasticsearch/mget/SimpleMgetTests.java @@ -18,6 +18,7 @@ */ package org.elasticsearch.mget; +import org.elasticsearch.action.admin.indices.alias.Alias; import org.elasticsearch.action.get.MultiGetItemResponse; import org.elasticsearch.action.get.MultiGetRequest; import org.elasticsearch.action.get.MultiGetRequestBuilder; @@ -71,25 +72,24 @@ public class SimpleMgetTests extends ElasticsearchIntegrationTest { @Test public void testThatParentPerDocumentIsSupported() throws Exception { - createIndex("test"); + assertAcked(prepareCreate("test").addAlias(new Alias("alias")) + .addMapping("test", jsonBuilder() + .startObject() + .startObject("test") + .startObject("_parent") + .field("type", "foo") + .endObject() + .endObject() + .endObject())); ensureYellow(); - client().admin().indices().preparePutMapping("test").setType("test").setSource(jsonBuilder() - .startObject() - .startObject("test") - .startObject("_parent") - .field("type", "foo") - .endObject() - .endObject() - .endObject() - ).get(); client().prepareIndex("test", "test", "1").setParent("4").setRefresh(true) .setSource(jsonBuilder().startObject().field("foo", "bar").endObject()) .execute().actionGet(); MultiGetResponse mgetResponse = client().prepareMultiGet() - .add(new MultiGetRequest.Item("test", "test", "1").parent("4")) - .add(new MultiGetRequest.Item("test", "test", "1")) + .add(new MultiGetRequest.Item(indexOrAlias(), "test", "1").parent("4")) + .add(new MultiGetRequest.Item(indexOrAlias(), "test", "1")) .execute().actionGet(); assertThat(mgetResponse.getResponses().length, is(2)); @@ -104,7 +104,7 @@ public class SimpleMgetTests extends ElasticsearchIntegrationTest { @SuppressWarnings("unchecked") @Test public void testThatSourceFilteringIsSupported() throws Exception { - createIndex("test"); + assertAcked(prepareCreate("test").addAlias(new Alias("alias"))); ensureYellow(); BytesReference sourceBytesRef = jsonBuilder().startObject() .field("field", "1", "2") @@ -118,9 +118,9 @@ public class SimpleMgetTests extends ElasticsearchIntegrationTest { MultiGetRequestBuilder request = client().prepareMultiGet(); for (int i = 0; i < 100; i++) { if (i % 2 == 0) { - request.add(new MultiGetRequest.Item("test", "type", Integer.toString(i)).fetchSourceContext(new FetchSourceContext("included", "*.hidden_field"))); + request.add(new MultiGetRequest.Item(indexOrAlias(), "type", Integer.toString(i)).fetchSourceContext(new FetchSourceContext("included", "*.hidden_field"))); } else { - request.add(new MultiGetRequest.Item("test", "type", Integer.toString(i)).fetchSourceContext(new FetchSourceContext(false))); + request.add(new MultiGetRequest.Item(indexOrAlias(), "type", Integer.toString(i)).fetchSourceContext(new FetchSourceContext(false))); } } @@ -129,6 +129,7 @@ public class SimpleMgetTests extends ElasticsearchIntegrationTest { assertThat(response.getResponses().length, equalTo(100)); for (int i = 0; i < 100; i++) { MultiGetItemResponse responseItem = response.getResponses()[i]; + assertThat(responseItem.getIndex(), equalTo("test")); if (i % 2 == 0) { Map source = responseItem.getResponse().getSourceAsMap(); assertThat(source.size(), equalTo(1)); @@ -139,15 +140,14 @@ public class SimpleMgetTests extends ElasticsearchIntegrationTest { assertThat(responseItem.getResponse().getSourceAsBytes(), nullValue()); } } - - } @Test public void testThatRoutingPerDocumentIsSupported() throws Exception { - assertAcked(prepareCreate("test").setSettings(ImmutableSettings.builder() - .put(indexSettings()) - .put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, between(2, DEFAULT_MAX_NUM_SHARDS)))); + assertAcked(prepareCreate("test").addAlias(new Alias("alias")) + .setSettings(ImmutableSettings.builder() + .put(indexSettings()) + .put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, between(2, DEFAULT_MAX_NUM_SHARDS)))); ensureYellow(); client().prepareIndex("test", "test", "1").setRefresh(true).setRouting("2") @@ -155,15 +155,21 @@ public class SimpleMgetTests extends ElasticsearchIntegrationTest { .execute().actionGet(); MultiGetResponse mgetResponse = client().prepareMultiGet() - .add(new MultiGetRequest.Item("test", "test", "1").routing("2")) - .add(new MultiGetRequest.Item("test", "test", "1")) + .add(new MultiGetRequest.Item(indexOrAlias(), "test", "1").routing("2")) + .add(new MultiGetRequest.Item(indexOrAlias(), "test", "1")) .execute().actionGet(); assertThat(mgetResponse.getResponses().length, is(2)); assertThat(mgetResponse.getResponses()[0].isFailed(), is(false)); assertThat(mgetResponse.getResponses()[0].getResponse().isExists(), is(true)); + assertThat(mgetResponse.getResponses()[0].getResponse().getIndex(), is("test")); assertThat(mgetResponse.getResponses()[1].isFailed(), is(false)); assertThat(mgetResponse.getResponses()[1].getResponse().isExists(), is(false)); + assertThat(mgetResponse.getResponses()[1].getResponse().getIndex(), is("test")); + } + + private static String indexOrAlias() { + return randomBoolean() ? "test" : "alias"; } } diff --git a/src/test/java/org/elasticsearch/update/UpdateTests.java b/src/test/java/org/elasticsearch/update/UpdateTests.java index 54a59c78b1f..5baa63f833f 100644 --- a/src/test/java/org/elasticsearch/update/UpdateTests.java +++ b/src/test/java/org/elasticsearch/update/UpdateTests.java @@ -26,6 +26,7 @@ import org.elasticsearch.ElasticsearchException; import org.elasticsearch.ElasticsearchTimeoutException; import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.ActionRequestValidationException; +import org.elasticsearch.action.admin.indices.alias.Alias; import org.elasticsearch.action.delete.DeleteRequest; import org.elasticsearch.action.delete.DeleteResponse; import org.elasticsearch.action.get.GetResponse; @@ -57,48 +58,48 @@ import java.util.concurrent.Semaphore; import java.util.concurrent.TimeUnit; import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertThrows; import static org.hamcrest.Matchers.*; public class UpdateTests extends ElasticsearchIntegrationTest { - - protected void createIndex() throws Exception { + private void createTestIndex() throws Exception { logger.info("--> creating index test"); - client().admin().indices().prepareCreate("test") + assertAcked(prepareCreate("test").addAlias(new Alias("alias")) .addMapping("type1", XContentFactory.jsonBuilder() .startObject() .startObject("type1") .startObject("_timestamp").field("enabled", true).field("store", "yes").endObject() .startObject("_ttl").field("enabled", true).field("store", "yes").endObject() .endObject() - .endObject()) - .execute().actionGet(); + .endObject())); } @Test public void testUpsert() throws Exception { - createIndex(); + createTestIndex(); ensureGreen(); - UpdateResponse updateResponse = client().prepareUpdate("test", "type1", "1") + UpdateResponse updateResponse = client().prepareUpdate(indexOrAlias(), "type1", "1") .setUpsert(XContentFactory.jsonBuilder().startObject().field("field", 1).endObject()) .setScript("ctx._source.field += 1", ScriptService.ScriptType.INLINE) .execute().actionGet(); assertTrue(updateResponse.isCreated()); + assertThat(updateResponse.getIndex(), equalTo("test")); for (int i = 0; i < 5; i++) { GetResponse getResponse = client().prepareGet("test", "type1", "1").execute().actionGet(); assertThat(getResponse.getSourceAsMap().get("field").toString(), equalTo("1")); } - updateResponse = client().prepareUpdate("test", "type1", "1") + updateResponse = client().prepareUpdate(indexOrAlias(), "type1", "1") .setUpsert(XContentFactory.jsonBuilder().startObject().field("field", 1).endObject()) .setScript("ctx._source.field += 1", ScriptService.ScriptType.INLINE) .execute().actionGet(); assertFalse(updateResponse.isCreated()); - + assertThat(updateResponse.getIndex(), equalTo("test")); for (int i = 0; i < 5; i++) { GetResponse getResponse = client().prepareGet("test", "type1", "1").execute().actionGet(); @@ -108,7 +109,7 @@ public class UpdateTests extends ElasticsearchIntegrationTest { @Test public void testScriptedUpsert() throws Exception { - createIndex(); + createTestIndex(); ensureGreen(); // Script logic is @@ -122,13 +123,14 @@ public class UpdateTests extends ElasticsearchIntegrationTest { // Pay money from what will be a new account and opening balance comes from upsert doc // provided by client - UpdateResponse updateResponse = client().prepareUpdate("test", "type1", "1") + UpdateResponse updateResponse = client().prepareUpdate(indexOrAlias(), "type1", "1") .setUpsert(XContentFactory.jsonBuilder().startObject().field("balance", openingBalance).endObject()) .setScriptedUpsert(true) .addScriptParam("payment", 2) .setScript(script, ScriptService.ScriptType.INLINE) .execute().actionGet(); assertTrue(updateResponse.isCreated()); + assertThat(updateResponse.getIndex(), equalTo("test")); for (int i = 0; i < 5; i++) { GetResponse getResponse = client().prepareGet("test", "type1", "1").execute().actionGet(); @@ -136,42 +138,44 @@ public class UpdateTests extends ElasticsearchIntegrationTest { } // Now pay money for an existing account where balance is stored in es - updateResponse = client().prepareUpdate("test", "type1", "1") + updateResponse = client().prepareUpdate(indexOrAlias(), "type1", "1") .setUpsert(XContentFactory.jsonBuilder().startObject().field("balance", openingBalance).endObject()) .setScriptedUpsert(true) .addScriptParam("payment", 2) .setScript(script, ScriptService.ScriptType.INLINE) .execute().actionGet(); assertFalse(updateResponse.isCreated()); - + assertThat(updateResponse.getIndex(), equalTo("test")); for (int i = 0; i < 5; i++) { GetResponse getResponse = client().prepareGet("test", "type1", "1").execute().actionGet(); assertThat(getResponse.getSourceAsMap().get("balance").toString(), equalTo("7")); } } - + @Test public void testUpsertDoc() throws Exception { - createIndex(); + createTestIndex(); ensureGreen(); - UpdateResponse updateResponse = client().prepareUpdate("test", "type1", "1") + UpdateResponse updateResponse = client().prepareUpdate(indexOrAlias(), "type1", "1") .setDoc(XContentFactory.jsonBuilder().startObject().field("bar", "baz").endObject()) .setDocAsUpsert(true) .setFields("_source") .execute().actionGet(); + assertThat(updateResponse.getIndex(), equalTo("test")); assertThat(updateResponse.getGetResult(), notNullValue()); + assertThat(updateResponse.getGetResult().getIndex(), equalTo("test")); assertThat(updateResponse.getGetResult().sourceAsMap().get("bar").toString(), equalTo("baz")); } @Test // See: https://github.com/elasticsearch/elasticsearch/issues/3265 public void testNotUpsertDoc() throws Exception { - createIndex(); + createTestIndex(); ensureGreen(); - assertThrows(client().prepareUpdate("test", "type1", "1") + assertThrows(client().prepareUpdate(indexOrAlias(), "type1", "1") .setDoc(XContentFactory.jsonBuilder().startObject().field("bar", "baz").endObject()) .setDocAsUpsert(false) .setFields("_source") @@ -180,47 +184,51 @@ public class UpdateTests extends ElasticsearchIntegrationTest { @Test public void testUpsertFields() throws Exception { - createIndex(); + createTestIndex(); ensureGreen(); - UpdateResponse updateResponse = client().prepareUpdate("test", "type1", "1") + UpdateResponse updateResponse = client().prepareUpdate(indexOrAlias(), "type1", "1") .setUpsert(XContentFactory.jsonBuilder().startObject().field("bar", "baz").endObject()) .setScript("ctx._source.extra = \"foo\"", ScriptService.ScriptType.INLINE) .setFields("_source") .execute().actionGet(); + assertThat(updateResponse.getIndex(), equalTo("test")); assertThat(updateResponse.getGetResult(), notNullValue()); + assertThat(updateResponse.getGetResult().getIndex(), equalTo("test")); assertThat(updateResponse.getGetResult().sourceAsMap().get("bar").toString(), equalTo("baz")); assertThat(updateResponse.getGetResult().sourceAsMap().get("extra"), nullValue()); - updateResponse = client().prepareUpdate("test", "type1", "1") + updateResponse = client().prepareUpdate(indexOrAlias(), "type1", "1") .setUpsert(XContentFactory.jsonBuilder().startObject().field("bar", "baz").endObject()) .setScript("ctx._source.extra = \"foo\"", ScriptService.ScriptType.INLINE) .setFields("_source") .execute().actionGet(); + assertThat(updateResponse.getIndex(), equalTo("test")); assertThat(updateResponse.getGetResult(), notNullValue()); + assertThat(updateResponse.getGetResult().getIndex(), equalTo("test")); assertThat(updateResponse.getGetResult().sourceAsMap().get("bar").toString(), equalTo("baz")); assertThat(updateResponse.getGetResult().sourceAsMap().get("extra").toString(), equalTo("foo")); } @Test public void testVersionedUpdate() throws Exception { - createIndex("test"); + assertAcked(prepareCreate("test").addAlias(new Alias("alias"))); ensureGreen(); index("test", "type", "1", "text", "value"); // version is now 1 - assertThrows(client().prepareUpdate("test", "type", "1") + assertThrows(client().prepareUpdate(indexOrAlias(), "type", "1") .setScript("ctx._source.text = 'v2'", ScriptService.ScriptType.INLINE).setVersion(2).execute(), VersionConflictEngineException.class); - client().prepareUpdate("test", "type", "1") + client().prepareUpdate(indexOrAlias(), "type", "1") .setScript("ctx._source.text = 'v2'", ScriptService.ScriptType.INLINE).setVersion(1).get(); assertThat(client().prepareGet("test", "type", "1").get().getVersion(), equalTo(2l)); // and again with a higher version.. - client().prepareUpdate("test", "type", "1") + client().prepareUpdate(indexOrAlias(), "type", "1") .setScript("ctx._source.text = 'v3'", ScriptService.ScriptType.INLINE).setVersion(2).get(); assertThat(client().prepareGet("test", "type", "1").get().getVersion(), equalTo(3l)); @@ -234,7 +242,7 @@ public class UpdateTests extends ElasticsearchIntegrationTest { // external versioning client().prepareIndex("test", "type", "2").setSource("text", "value").setVersion(10).setVersionType(VersionType.EXTERNAL).get(); - assertThrows(client().prepareUpdate("test", "type", "2") + assertThrows(client().prepareUpdate(indexOrAlias(), "type", "2") .setScript("ctx._source.text = 'v2'", ScriptService.ScriptType.INLINE).setVersion(2) .setVersionType(VersionType.EXTERNAL).execute(), ActionRequestValidationException.class); @@ -242,14 +250,14 @@ public class UpdateTests extends ElasticsearchIntegrationTest { // upserts - the combination with versions is a bit weird. Test are here to ensure we do not change our behavior unintentionally // With internal versions, tt means "if object is there with version X, update it or explode. If it is not there, index. - client().prepareUpdate("test", "type", "3").setScript("ctx._source.text = 'v2'", ScriptService.ScriptType.INLINE) + client().prepareUpdate(indexOrAlias(), "type", "3").setScript("ctx._source.text = 'v2'", ScriptService.ScriptType.INLINE) .setVersion(10).setUpsert("{ \"text\": \"v0\" }").get(); GetResponse get = get("test", "type", "3"); assertThat(get.getVersion(), equalTo(1l)); assertThat((String) get.getSource().get("text"), equalTo("v0")); // With force version - client().prepareUpdate("test", "type", "4").setScript("ctx._source.text = 'v2'", ScriptService.ScriptType.INLINE) + client().prepareUpdate(indexOrAlias(), "type", "4").setScript("ctx._source.text = 'v2'", ScriptService.ScriptType.INLINE) .setVersion(10).setVersionType(VersionType.FORCE).setUpsert("{ \"text\": \"v0\" }").get(); get = get("test", "type", "4"); @@ -258,9 +266,7 @@ public class UpdateTests extends ElasticsearchIntegrationTest { // retry on conflict is rejected: - - assertThrows(client().prepareUpdate("test", "type", "1").setVersion(10).setRetryOnConflict(5), ActionRequestValidationException.class); - + assertThrows(client().prepareUpdate(indexOrAlias(), "type", "1").setVersion(10).setRetryOnConflict(5), ActionRequestValidationException.class); } @Test @@ -271,18 +277,20 @@ public class UpdateTests extends ElasticsearchIntegrationTest { .setFields("_source") .execute().actionGet(); + assertThat(updateResponse.getIndex(), equalTo("test")); assertThat(updateResponse.getGetResult(), notNullValue()); + assertThat(updateResponse.getGetResult().getIndex(), equalTo("test")); assertThat(updateResponse.getGetResult().sourceAsMap().get("bar").toString(), equalTo("baz")); assertThat(updateResponse.getGetResult().sourceAsMap().get("extra"), nullValue()); } @Test public void testUpdate() throws Exception { - createIndex(); + createTestIndex(); ensureGreen(); try { - client().prepareUpdate("test", "type1", "1") + client().prepareUpdate(indexOrAlias(), "type1", "1") .setScript("ctx._source.field++", ScriptService.ScriptType.INLINE).execute().actionGet(); fail(); } catch (DocumentMissingException e) { @@ -291,21 +299,23 @@ public class UpdateTests extends ElasticsearchIntegrationTest { client().prepareIndex("test", "type1", "1").setSource("field", 1).execute().actionGet(); - UpdateResponse updateResponse = client().prepareUpdate("test", "type1", "1") + UpdateResponse updateResponse = client().prepareUpdate(indexOrAlias(), "type1", "1") .setScript("ctx._source.field += 1", ScriptService.ScriptType.INLINE).execute().actionGet(); assertThat(updateResponse.getVersion(), equalTo(2L)); assertFalse(updateResponse.isCreated()); + assertThat(updateResponse.getIndex(), equalTo("test")); for (int i = 0; i < 5; i++) { GetResponse getResponse = client().prepareGet("test", "type1", "1").execute().actionGet(); assertThat(getResponse.getSourceAsMap().get("field").toString(), equalTo("2")); } - updateResponse = client().prepareUpdate("test", "type1", "1") + updateResponse = client().prepareUpdate(indexOrAlias(), "type1", "1") .setScript("ctx._source.field += count", ScriptService.ScriptType.INLINE) .addScriptParam("count", 3).execute().actionGet(); assertThat(updateResponse.getVersion(), equalTo(3L)); assertFalse(updateResponse.isCreated()); + assertThat(updateResponse.getIndex(), equalTo("test")); for (int i = 0; i < 5; i++) { GetResponse getResponse = client().prepareGet("test", "type1", "1").execute().actionGet(); @@ -313,10 +323,11 @@ public class UpdateTests extends ElasticsearchIntegrationTest { } // check noop - updateResponse = client().prepareUpdate("test", "type1", "1") + updateResponse = client().prepareUpdate(indexOrAlias(), "type1", "1") .setScript("ctx.op = 'none'", ScriptService.ScriptType.INLINE).execute().actionGet(); assertThat(updateResponse.getVersion(), equalTo(3L)); assertFalse(updateResponse.isCreated()); + assertThat(updateResponse.getIndex(), equalTo("test")); for (int i = 0; i < 5; i++) { GetResponse getResponse = client().prepareGet("test", "type1", "1").execute().actionGet(); @@ -324,10 +335,11 @@ public class UpdateTests extends ElasticsearchIntegrationTest { } // check delete - updateResponse = client().prepareUpdate("test", "type1", "1") + updateResponse = client().prepareUpdate(indexOrAlias(), "type1", "1") .setScript("ctx.op = 'delete'", ScriptService.ScriptType.INLINE).execute().actionGet(); assertThat(updateResponse.getVersion(), equalTo(4L)); assertFalse(updateResponse.isCreated()); + assertThat(updateResponse.getIndex(), equalTo("test")); for (int i = 0; i < 5; i++) { GetResponse getResponse = client().prepareGet("test", "type1", "1").execute().actionGet(); @@ -339,13 +351,13 @@ public class UpdateTests extends ElasticsearchIntegrationTest { GetResponse getResponse = client().prepareGet("test", "type1", "2").setFields("_ttl").execute().actionGet(); long ttl = ((Number) getResponse.getField("_ttl").getValue()).longValue(); assertThat(ttl, greaterThan(0L)); - client().prepareUpdate("test", "type1", "2").setScript("ctx._source.field += 1", ScriptService.ScriptType.INLINE).execute().actionGet(); + client().prepareUpdate(indexOrAlias(), "type1", "2").setScript("ctx._source.field += 1", ScriptService.ScriptType.INLINE).execute().actionGet(); getResponse = client().prepareGet("test", "type1", "2").setFields("_ttl").execute().actionGet(); ttl = ((Number) getResponse.getField("_ttl").getValue()).longValue(); assertThat(ttl, greaterThan(0L)); // check TTL update - client().prepareUpdate("test", "type1", "2").setScript("ctx._ttl = 3600000", ScriptService.ScriptType.INLINE).execute().actionGet(); + client().prepareUpdate(indexOrAlias(), "type1", "2").setScript("ctx._ttl = 3600000", ScriptService.ScriptType.INLINE).execute().actionGet(); getResponse = client().prepareGet("test", "type1", "2").setFields("_ttl").execute().actionGet(); ttl = ((Number) getResponse.getField("_ttl").getValue()).longValue(); assertThat(ttl, greaterThan(0L)); @@ -353,7 +365,7 @@ public class UpdateTests extends ElasticsearchIntegrationTest { // check timestamp update client().prepareIndex("test", "type1", "3").setSource("field", 1).setRefresh(true).execute().actionGet(); - client().prepareUpdate("test", "type1", "3") + client().prepareUpdate(indexOrAlias(), "type1", "3") .setScript("ctx._timestamp = \"2009-11-15T14:12:12\"", ScriptService.ScriptType.INLINE).execute().actionGet(); getResponse = client().prepareGet("test", "type1", "3").setFields("_timestamp").execute().actionGet(); long timestamp = ((Number) getResponse.getField("_timestamp").getValue()).longValue(); @@ -361,16 +373,18 @@ public class UpdateTests extends ElasticsearchIntegrationTest { // check fields parameter client().prepareIndex("test", "type1", "1").setSource("field", 1).execute().actionGet(); - updateResponse = client().prepareUpdate("test", "type1", "1") + updateResponse = client().prepareUpdate(indexOrAlias(), "type1", "1") .setScript("ctx._source.field += 1", ScriptService.ScriptType.INLINE).setFields("_source", "field").execute().actionGet(); + assertThat(updateResponse.getIndex(), equalTo("test")); assertThat(updateResponse.getGetResult(), notNullValue()); + assertThat(updateResponse.getGetResult().getIndex(), equalTo("test")); assertThat(updateResponse.getGetResult().sourceRef(), notNullValue()); assertThat(updateResponse.getGetResult().field("field").getValue(), notNullValue()); // check updates without script // add new field client().prepareIndex("test", "type1", "1").setSource("field", 1).execute().actionGet(); - updateResponse = client().prepareUpdate("test", "type1", "1").setDoc(XContentFactory.jsonBuilder().startObject().field("field2", 2).endObject()).execute().actionGet(); + updateResponse = client().prepareUpdate(indexOrAlias(), "type1", "1").setDoc(XContentFactory.jsonBuilder().startObject().field("field2", 2).endObject()).execute().actionGet(); for (int i = 0; i < 5; i++) { getResponse = client().prepareGet("test", "type1", "1").execute().actionGet(); assertThat(getResponse.getSourceAsMap().get("field").toString(), equalTo("1")); @@ -378,7 +392,7 @@ public class UpdateTests extends ElasticsearchIntegrationTest { } // change existing field - updateResponse = client().prepareUpdate("test", "type1", "1").setDoc(XContentFactory.jsonBuilder().startObject().field("field", 3).endObject()).execute().actionGet(); + updateResponse = client().prepareUpdate(indexOrAlias(), "type1", "1").setDoc(XContentFactory.jsonBuilder().startObject().field("field", 3).endObject()).execute().actionGet(); for (int i = 0; i < 5; i++) { getResponse = client().prepareGet("test", "type1", "1").execute().actionGet(); assertThat(getResponse.getSourceAsMap().get("field").toString(), equalTo("3")); @@ -396,7 +410,7 @@ public class UpdateTests extends ElasticsearchIntegrationTest { testMap.put("map1", 8); client().prepareIndex("test", "type1", "1").setSource("map", testMap).execute().actionGet(); - updateResponse = client().prepareUpdate("test", "type1", "1").setDoc(XContentFactory.jsonBuilder().startObject().field("map", testMap3).endObject()).execute().actionGet(); + updateResponse = client().prepareUpdate(indexOrAlias(), "type1", "1").setDoc(XContentFactory.jsonBuilder().startObject().field("map", testMap3).endObject()).execute().actionGet(); for (int i = 0; i < 5; i++) { getResponse = client().prepareGet("test", "type1", "1").execute().actionGet(); Map map1 = (Map) getResponse.getSourceAsMap().get("map"); @@ -414,11 +428,11 @@ public class UpdateTests extends ElasticsearchIntegrationTest { @Test public void testUpdateRequestWithBothScriptAndDoc() throws Exception { - createIndex(); + createTestIndex(); ensureGreen(); try { - client().prepareUpdate("test", "type1", "1") + client().prepareUpdate(indexOrAlias(), "type1", "1") .setDoc(XContentFactory.jsonBuilder().startObject().field("field", 1).endObject()) .setScript("ctx._source.field += 1", ScriptService.ScriptType.INLINE) .execute().actionGet(); @@ -432,10 +446,10 @@ public class UpdateTests extends ElasticsearchIntegrationTest { @Test public void testUpdateRequestWithScriptAndShouldUpsertDoc() throws Exception { - createIndex(); + createTestIndex(); ensureGreen(); try { - client().prepareUpdate("test", "type1", "1") + client().prepareUpdate(indexOrAlias(), "type1", "1") .setScript("ctx._source.field += 1", ScriptService.ScriptType.INLINE) .setDocAsUpsert(true) .execute().actionGet(); @@ -451,7 +465,7 @@ public class UpdateTests extends ElasticsearchIntegrationTest { @Slow public void testConcurrentUpdateWithRetryOnConflict() throws Exception { final boolean useBulkApi = randomBoolean(); - createIndex(); + createTestIndex(); ensureGreen(); int numberOfThreads = scaledRandomIntBetween(2,5); @@ -468,13 +482,13 @@ public class UpdateTests extends ElasticsearchIntegrationTest { startLatch.await(); for (int i = 0; i < numberOfUpdatesPerThread; i++) { if (useBulkApi) { - UpdateRequestBuilder updateRequestBuilder = client().prepareUpdate("test", "type1", Integer.toString(i)) + UpdateRequestBuilder updateRequestBuilder = client().prepareUpdate(indexOrAlias(), "type1", Integer.toString(i)) .setScript("ctx._source.field += 1", ScriptService.ScriptType.INLINE) .setRetryOnConflict(Integer.MAX_VALUE) .setUpsert(jsonBuilder().startObject().field("field", 1).endObject()); client().prepareBulk().add(updateRequestBuilder).execute().actionGet(); } else { - client().prepareUpdate("test", "type1", Integer.toString(i)) + client().prepareUpdate(indexOrAlias(), "type1", Integer.toString(i)) .setScript("ctx._source.field += 1", ScriptService.ScriptType.INLINE) .setRetryOnConflict(Integer.MAX_VALUE) .setUpsert(jsonBuilder().startObject().field("field", 1).endObject()) @@ -528,7 +542,7 @@ public class UpdateTests extends ElasticsearchIntegrationTest { @Slow public void stressUpdateDeleteConcurrency() throws Exception { //We create an index with merging disabled so that deletes don't get merged away - client().admin().indices().prepareCreate("test") + assertAcked(prepareCreate("test") .addMapping("type1", XContentFactory.jsonBuilder() .startObject() .startObject("type1") @@ -536,8 +550,7 @@ public class UpdateTests extends ElasticsearchIntegrationTest { .startObject("_ttl").field("enabled", true).field("store", "yes").endObject() .endObject() .endObject()) - .setSettings(ImmutableSettings.builder().put(MergePolicyModule.MERGE_POLICY_TYPE_KEY, NoMergePolicyProvider.class)) - .execute().actionGet(); + .setSettings(ImmutableSettings.builder().put(MergePolicyModule.MERGE_POLICY_TYPE_KEY, NoMergePolicyProvider.class))); ensureGreen(); final int numberOfThreads = scaledRandomIntBetween(3,5); @@ -748,4 +761,7 @@ public class UpdateTests extends ElasticsearchIntegrationTest { } } + private static String indexOrAlias() { + return randomBoolean() ? "test" : "alias"; + } }