From c2f8ea087933149e4ff0fe9352ab3fe28101260a Mon Sep 17 00:00:00 2001 From: David JANSSEN Date: Mon, 2 Feb 2015 09:44:30 +0100 Subject: [PATCH 1/4] Adapt to ElasticSearch v1.3.4 Adapt to ElasticSearch v1.3.4 --- pom.xml | 5 +-- .../action/view/TransportViewAction.java | 34 ++++++++----------- .../elasticsearch/action/view/ViewAction.java | 4 +-- .../action/view/ViewRequest.java | 3 +- .../action/view/ViewRequestBuilder.java | 6 ++-- .../rest/action/view/RestViewAction.java | 13 +++---- .../ElasticSearchViewNotFoundException.java | 4 +-- 7 files changed, 31 insertions(+), 38 deletions(-) diff --git a/pom.xml b/pom.xml index 3503967..d8a30f4 100644 --- a/pom.xml +++ b/pom.xml @@ -17,11 +17,12 @@ - 0.20.5 + 1.3.4 + 4.9.0 2.1.3.Final UTF-8 - + The Apache Software License, Version 2.0 diff --git a/src/main/java/org/elasticsearch/action/view/TransportViewAction.java b/src/main/java/org/elasticsearch/action/view/TransportViewAction.java index 73de55d..1a7849e 100644 --- a/src/main/java/org/elasticsearch/action/view/TransportViewAction.java +++ b/src/main/java/org/elasticsearch/action/view/TransportViewAction.java @@ -18,9 +18,10 @@ */ package org.elasticsearch.action.view; -import org.elasticsearch.ElasticSearchException; -import org.elasticsearch.ElasticSearchIllegalArgumentException; -import org.elasticsearch.ElasticSearchParseException; +import org.elasticsearch.index.VersionType; +import org.elasticsearch.ElasticsearchException; +import org.elasticsearch.ElasticsearchIllegalArgumentException; +import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.search.TransportSearchAction; @@ -67,7 +68,7 @@ public TransportViewAction(Settings settings, ThreadPool threadPool, IndicesService indicesService, ViewService viewService, TransportSearchAction searchAction) { - super(settings, threadPool, clusterService, transportService); + super(settings, ViewAction.NAME, threadPool, clusterService, transportService); this.indicesService = indicesService; this.viewService = viewService; this.searchAction = searchAction; @@ -88,11 +89,6 @@ protected ViewResponse newResponse() { return new ViewResponse(); } - @Override - protected String transportAction() { - return ViewAction.NAME; - } - @Override protected ClusterBlockException checkGlobalBlock(ClusterState state, ViewRequest request) { return state.blocks().globalBlockedException(ClusterBlockLevel.READ); @@ -110,15 +106,15 @@ protected ShardIterator shards(ClusterState state, ViewRequest request) { } @Override - protected ViewResponse shardOperation(ViewRequest request, int shardId) throws ElasticSearchException { + protected ViewResponse shardOperation(ViewRequest request, int shardId) throws ElasticsearchException { // Get the doc first IndexService indexService = indicesService.indexService(request.index()); IndexShard indexShard = indexService.shardSafe(shardId); - GetResult getResult = indexShard.getService().get(request.type(), request.id(), null, false); + GetResult getResult = indexShard.getService().get(request.type(), request.id(), null, false, 1, VersionType.INTERNAL, null); - if (!getResult.exists()) { - throw new ElasticSearchIllegalArgumentException("Document not found, cannot render view"); + if (!getResult.isExists()) { + throw new ElasticsearchIllegalArgumentException("Document not found, cannot render view"); } // Try to get a view stored at document level @@ -132,7 +128,7 @@ protected ViewResponse shardOperation(ViewRequest request, int shardId) throws E Map mapping = mappingMetaData.sourceAsMap(); viewContext = extract(mapping, request.format()); } catch (IOException e) { - throw new ElasticSearchParseException("Failed to parse mapping content to map", e); + throw new ElasticsearchParseException("Failed to parse mapping content to map", e); } } } @@ -142,10 +138,10 @@ protected ViewResponse shardOperation(ViewRequest request, int shardId) throws E } // Set some org.elasticsearch.test.integration.views.mappings.data required for view rendering - viewContext.index(getResult.index()) - .type(getResult.type()) - .id(getResult.id()) - .version(getResult.version()) + viewContext.index(getResult.getIndex()) + .type(getResult.getType()) + .id(getResult.getId()) + .version(getResult.getVersion()) .source(getResult.sourceAsMap()); // Ok, let's render it with a ViewEngineService @@ -283,7 +279,7 @@ private ViewContext extract(Map sourceAsMap, String format) { } SearchResponse searchResponse = searchAction.execute(searchRequest).get(); - viewContext.queriesAndHits(queryName, searchResponse.hits()); + viewContext.queriesAndHits(queryName, searchResponse.getHits()); } catch (Exception e) { viewContext.queriesAndHits(queryName, null); diff --git a/src/main/java/org/elasticsearch/action/view/ViewAction.java b/src/main/java/org/elasticsearch/action/view/ViewAction.java index 513bf05..db690f0 100644 --- a/src/main/java/org/elasticsearch/action/view/ViewAction.java +++ b/src/main/java/org/elasticsearch/action/view/ViewAction.java @@ -18,10 +18,10 @@ */ package org.elasticsearch.action.view; -import org.elasticsearch.action.Action; +import org.elasticsearch.action.ClientAction; import org.elasticsearch.client.Client; -public class ViewAction extends Action { +public class ViewAction extends ClientAction { public static final ViewAction INSTANCE = new ViewAction(); public static final String NAME = "view"; diff --git a/src/main/java/org/elasticsearch/action/view/ViewRequest.java b/src/main/java/org/elasticsearch/action/view/ViewRequest.java index f96f065..009c38d 100644 --- a/src/main/java/org/elasticsearch/action/view/ViewRequest.java +++ b/src/main/java/org/elasticsearch/action/view/ViewRequest.java @@ -20,7 +20,7 @@ import org.elasticsearch.action.support.single.shard.SingleShardOperationRequest; import org.elasticsearch.common.Nullable; -import org.elasticsearch.common.Required; + public class ViewRequest extends SingleShardOperationRequest { @@ -59,7 +59,6 @@ public ViewRequest type(@Nullable String type) { /** * Sets the id of the document to fetch. */ - @Required public ViewRequest id(String id) { this.id = id; return this; diff --git a/src/main/java/org/elasticsearch/action/view/ViewRequestBuilder.java b/src/main/java/org/elasticsearch/action/view/ViewRequestBuilder.java index 9c67a9a..54a2775 100644 --- a/src/main/java/org/elasticsearch/action/view/ViewRequestBuilder.java +++ b/src/main/java/org/elasticsearch/action/view/ViewRequestBuilder.java @@ -21,16 +21,16 @@ import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.support.single.shard.SingleShardOperationRequestBuilder; import org.elasticsearch.client.Client; -import org.elasticsearch.client.internal.InternalClient; + public class ViewRequestBuilder extends SingleShardOperationRequestBuilder { public ViewRequestBuilder(Client client) { - super((InternalClient) client, new ViewRequest()); + super(client, new ViewRequest()); } public ViewRequestBuilder(Client client, String index, String type, String id) { - super((InternalClient) client, new ViewRequest(index, type, id)); + super(client, new ViewRequest(index, type, id)); } /** diff --git a/src/main/java/org/elasticsearch/rest/action/view/RestViewAction.java b/src/main/java/org/elasticsearch/rest/action/view/RestViewAction.java index 2aa66f5..514dfb6 100644 --- a/src/main/java/org/elasticsearch/rest/action/view/RestViewAction.java +++ b/src/main/java/org/elasticsearch/rest/action/view/RestViewAction.java @@ -42,7 +42,7 @@ public RestViewAction(Settings settings, Client client, RestController controlle controller.registerHandler(GET, "/_view/{index}/{type}/{id}/{format}", this); } - public void handleRequest(final RestRequest request, final RestChannel channel) { + public void handleRequest(final RestRequest request, final RestChannel channel, Client client) { ViewRequest viewRequest = new ViewRequest(request.param("index"), request.param("type"), request.param("id")); if (request.hasParam("format")) { viewRequest.format(request.param("format")); @@ -57,22 +57,19 @@ public void handleRequest(final RestRequest request, final RestChannel channel) public void onResponse(ViewResponse response) { try { - channel.sendResponse(new BytesRestResponse(response.content(), response.contentType())); + channel.sendResponse(new BytesRestResponse(RestStatus.OK, response.contentType(), response.content())); } catch (Exception e) { onFailure(e); } } public void onFailure(Throwable e) { - try { + if (e instanceof ElasticSearchViewNotFoundException) { - channel.sendResponse(new XContentThrowableRestResponse(request, NOT_FOUND, e)); + channel.sendResponse(new BytesRestResponse(NOT_FOUND, e.toString())); } else { - channel.sendResponse(new XContentThrowableRestResponse(request, e)); + channel.sendResponse(new BytesRestResponse(RestStatus.BAD_REQUEST, e.toString())); } - } catch (IOException e1) { - logger.error("Failed to send failure response", e1); - } } }); } diff --git a/src/main/java/org/elasticsearch/view/exception/ElasticSearchViewNotFoundException.java b/src/main/java/org/elasticsearch/view/exception/ElasticSearchViewNotFoundException.java index 8d844ae..4582f8b 100644 --- a/src/main/java/org/elasticsearch/view/exception/ElasticSearchViewNotFoundException.java +++ b/src/main/java/org/elasticsearch/view/exception/ElasticSearchViewNotFoundException.java @@ -19,9 +19,9 @@ package org.elasticsearch.view.exception; -import org.elasticsearch.ElasticSearchException; +import org.elasticsearch.ElasticsearchException; -public class ElasticSearchViewNotFoundException extends ElasticSearchException { +public class ElasticSearchViewNotFoundException extends ElasticsearchException { public ElasticSearchViewNotFoundException(String msg) { super(msg); } From 5ede177ad11e313adf553e9b0258656811d206ab Mon Sep 17 00:00:00 2001 From: David JANSSEN Date: Fri, 6 Feb 2015 10:07:21 +0100 Subject: [PATCH 2/4] Adapt to ElasticSearch v1.4.2 --- pom.xml | 3 +-- .../action/view/TransportViewAction.java | 25 +++++++++++-------- .../rest/action/view/RestViewAction.java | 2 +- 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/pom.xml b/pom.xml index d8a30f4..98aa6d0 100644 --- a/pom.xml +++ b/pom.xml @@ -17,8 +17,7 @@ - 1.3.4 - 4.9.0 + 1.4.2 2.1.3.Final UTF-8 diff --git a/src/main/java/org/elasticsearch/action/view/TransportViewAction.java b/src/main/java/org/elasticsearch/action/view/TransportViewAction.java index 1a7849e..5419dfa 100644 --- a/src/main/java/org/elasticsearch/action/view/TransportViewAction.java +++ b/src/main/java/org/elasticsearch/action/view/TransportViewAction.java @@ -32,7 +32,6 @@ import org.elasticsearch.cluster.block.ClusterBlockLevel; import org.elasticsearch.cluster.metadata.MappingMetaData; import org.elasticsearch.cluster.routing.ShardIterator; -import org.elasticsearch.common.Strings; import org.elasticsearch.common.collect.ImmutableMap; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.settings.Settings; @@ -53,6 +52,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import org.elasticsearch.index.shard.ShardId; public class TransportViewAction extends TransportShardSingleOperationAction { @@ -68,12 +68,17 @@ public TransportViewAction(Settings settings, ThreadPool threadPool, IndicesService indicesService, ViewService viewService, TransportSearchAction searchAction) { - super(settings, ViewAction.NAME, threadPool, clusterService, transportService); + super(settings, ViewAction.NAME, threadPool, clusterService, transportService, null); this.indicesService = indicesService; this.viewService = viewService; this.searchAction = searchAction; } + @Override + protected boolean resolveIndex() { + return true; + } + @Override protected String executor() { return ThreadPool.Names.GENERIC; @@ -89,29 +94,27 @@ protected ViewResponse newResponse() { return new ViewResponse(); } - @Override protected ClusterBlockException checkGlobalBlock(ClusterState state, ViewRequest request) { return state.blocks().globalBlockedException(ClusterBlockLevel.READ); } - @Override protected ClusterBlockException checkRequestBlock(ClusterState state, ViewRequest request) { return state.blocks().indexBlockedException(ClusterBlockLevel.READ, request.index()); } - + @Override - protected ShardIterator shards(ClusterState state, ViewRequest request) { + protected ShardIterator shards(ClusterState state, InternalRequest action) { return clusterService.operationRouting() - .getShards(clusterService.state(), request.index(), request.type(), request.id(), null, null); + .getShards(clusterService.state(), action.request().index(), action.request().type(), action.request().id(), null, null); } - + @Override - protected ViewResponse shardOperation(ViewRequest request, int shardId) throws ElasticsearchException { + protected ViewResponse shardOperation(ViewRequest request, ShardId shardId) throws ElasticsearchException { // Get the doc first IndexService indexService = indicesService.indexService(request.index()); - IndexShard indexShard = indexService.shardSafe(shardId); - GetResult getResult = indexShard.getService().get(request.type(), request.id(), null, false, 1, VersionType.INTERNAL, null); + IndexShard indexShard = indexService.shardSafe(shardId.id()); + GetResult getResult = indexShard.getService().get(request.type(), request.id(), null, false, 1, VersionType.INTERNAL, null, false); if (!getResult.isExists()) { throw new ElasticsearchIllegalArgumentException("Document not found, cannot render view"); diff --git a/src/main/java/org/elasticsearch/rest/action/view/RestViewAction.java b/src/main/java/org/elasticsearch/rest/action/view/RestViewAction.java index 514dfb6..dc0b6a1 100644 --- a/src/main/java/org/elasticsearch/rest/action/view/RestViewAction.java +++ b/src/main/java/org/elasticsearch/rest/action/view/RestViewAction.java @@ -37,7 +37,7 @@ public class RestViewAction extends BaseRestHandler { @Inject public RestViewAction(Settings settings, Client client, RestController controller) { - super(settings, client); + super(settings, controller, client); controller.registerHandler(GET, "/_view/{index}/{type}/{id}", this); controller.registerHandler(GET, "/_view/{index}/{type}/{id}/{format}", this); } From 2ad39441251d881064d010fdd0ce402d1962d5bf Mon Sep 17 00:00:00 2001 From: David JANSSEN Date: Fri, 6 Feb 2015 13:09:09 +0100 Subject: [PATCH 3/4] Adapt to ElasticSearch v1.4.2 Bug fix TransportViewAction : we should provide actionFilters --- pom.xml | 2 +- .../elasticsearch/action/view/TransportViewAction.java | 8 +++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/pom.xml b/pom.xml index 98aa6d0..5236194 100644 --- a/pom.xml +++ b/pom.xml @@ -59,7 +59,7 @@ com.github.tlrx elasticsearch-test - 0.0.7 + 1.2.1 test diff --git a/src/main/java/org/elasticsearch/action/view/TransportViewAction.java b/src/main/java/org/elasticsearch/action/view/TransportViewAction.java index 5419dfa..d0d09dd 100644 --- a/src/main/java/org/elasticsearch/action/view/TransportViewAction.java +++ b/src/main/java/org/elasticsearch/action/view/TransportViewAction.java @@ -52,6 +52,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import org.elasticsearch.action.support.ActionFilters; import org.elasticsearch.index.shard.ShardId; @@ -67,8 +68,9 @@ public TransportViewAction(Settings settings, ThreadPool threadPool, TransportService transportService, IndicesService indicesService, ViewService viewService, - TransportSearchAction searchAction) { - super(settings, ViewAction.NAME, threadPool, clusterService, transportService, null); + TransportSearchAction searchAction, + ActionFilters actionFilters) { + super(settings, ViewAction.NAME, threadPool, clusterService, transportService, actionFilters); this.indicesService = indicesService; this.viewService = viewService; this.searchAction = searchAction; @@ -105,7 +107,7 @@ protected ClusterBlockException checkRequestBlock(ClusterState state, ViewReques @Override protected ShardIterator shards(ClusterState state, InternalRequest action) { return clusterService.operationRouting() - .getShards(clusterService.state(), action.request().index(), action.request().type(), action.request().id(), null, null); + .getShards(clusterService.state(), action.concreteIndex(), action.request().type(), action.request().id(), null, null); } @Override From cb5d739f471557ac5b8e73544fbe69b715e4b6ca Mon Sep 17 00:00:00 2001 From: David JANSSEN Date: Fri, 27 Feb 2015 16:26:02 +0100 Subject: [PATCH 4/4] Added multi shards/nodes support ViewRequest and ViewResponse should overwrite writeTo and readFrom methods in order to provide request parameters and response content. --- .../action/view/ViewRequest.java | 71 ++++++++++++++++++- .../action/view/ViewResponse.java | 22 ++++++ .../elasticsearch/plugin/view/ViewPlugin.java | 2 + 3 files changed, 93 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/elasticsearch/action/view/ViewRequest.java b/src/main/java/org/elasticsearch/action/view/ViewRequest.java index 009c38d..87907da 100644 --- a/src/main/java/org/elasticsearch/action/view/ViewRequest.java +++ b/src/main/java/org/elasticsearch/action/view/ViewRequest.java @@ -18,8 +18,14 @@ */ package org.elasticsearch.action.view; +import java.io.IOException; +import org.elasticsearch.action.ActionRequest; +import org.elasticsearch.action.ActionRequestValidationException; +import org.elasticsearch.action.ValidateActions; import org.elasticsearch.action.support.single.shard.SingleShardOperationRequest; import org.elasticsearch.common.Nullable; +import org.elasticsearch.common.io.stream.StreamInput; +import org.elasticsearch.common.io.stream.StreamOutput; public class ViewRequest extends SingleShardOperationRequest { @@ -30,12 +36,42 @@ public class ViewRequest extends SingleShardOperationRequest { public static final String DEFAULT_VIEW = "default"; ViewRequest() { + this.type = "_all"; + } + + /** + * Constructs a new view request against the specified index. The {@link #type(String)} and {@link #id(String)} + * must be set. + */ + public ViewRequest(String index) { + super(index); + this.type = "_all"; + } + + /* + * Copy constructor that creates a new view request that is a copy of the one provided as an argument. + * The new request will inherit though headers and context from the original request that caused it. + */ + public ViewRequest(ViewRequest viewRequest, ActionRequest originalRequest) { + + super(originalRequest); + this.index = viewRequest.index; + this.type = viewRequest.type; + this.id = viewRequest.id; + } + + /** + * Constructs a new view request starting from the provided request, meaning that it will + * inherit its headers and context, and against the specified index. + */ + public ViewRequest(ActionRequest request, String index) { + super(request, index); } /** * Constructs a new view request against the specified index with the type and id. * - * @param index The index to get the document from + * @param index The index to view the document from * @param type The type of the document * @param id The id of the document */ @@ -83,5 +119,36 @@ public String id() { public String format() { return format; } - + @Override + public ActionRequestValidationException validate() { + ActionRequestValidationException validationException = super.validate(); + if (type == null) { + validationException = ValidateActions.addValidationError("type is missing", validationException); + } + if (id == null) { + validationException = ValidateActions.addValidationError("id is missing", validationException); + } + return validationException; + } + + @Override + public void readFrom(StreamInput in) throws IOException + { + super.readFrom(in); + type = in.readSharedString(); + id = in.readString(); + } + + @Override + public void writeTo(StreamOutput out) throws IOException { + super.writeTo(out); + out.writeSharedString(type); + out.writeString(id); + } + + @Override + public String toString() { + return "view [" + index + "][" + type + "][" + id + "]"; + } + } diff --git a/src/main/java/org/elasticsearch/action/view/ViewResponse.java b/src/main/java/org/elasticsearch/action/view/ViewResponse.java index 5e8336b..51c45e8 100644 --- a/src/main/java/org/elasticsearch/action/view/ViewResponse.java +++ b/src/main/java/org/elasticsearch/action/view/ViewResponse.java @@ -18,7 +18,11 @@ */ package org.elasticsearch.action.view; +import java.io.IOException; import org.elasticsearch.action.ActionResponse; +import org.elasticsearch.common.io.stream.StreamInput; +import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.index.get.GetResult; public class ViewResponse extends ActionResponse { @@ -40,4 +44,22 @@ public byte[] content() { public String contentType() { return this.contentType; } + + + @Override + public void readFrom(StreamInput in) throws IOException { + super.readFrom(in); + contentType = in.readString(); + int contentlength = in.readInt(); + content = new byte[contentlength]; + in.readFully(content); + } + + @Override + public void writeTo(StreamOutput out) throws IOException { + super.writeTo(out); + out.writeString(contentType); + out.writeInt(content.length); + out.writeBytes(content); + } } diff --git a/src/main/java/org/elasticsearch/plugin/view/ViewPlugin.java b/src/main/java/org/elasticsearch/plugin/view/ViewPlugin.java index c177bd0..32bbebf 100644 --- a/src/main/java/org/elasticsearch/plugin/view/ViewPlugin.java +++ b/src/main/java/org/elasticsearch/plugin/view/ViewPlugin.java @@ -34,10 +34,12 @@ public class ViewPlugin extends AbstractPlugin { + @Override public String description() { return "Elasticsearch View Plugin"; } + @Override public String name() { return "view-plugin"; }