Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adapt to ElasticSearch v1.3.4 #2

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
</parent>

<properties>
<elasticsearch.version>0.20.5</elasticsearch.version>
<elasticsearch.version>1.4.2</elasticsearch.version>
<mvel2.version>2.1.3.Final</mvel2.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<licenses>
<license>
<name>The Apache Software License, Version 2.0</name>
Expand Down Expand Up @@ -59,7 +59,7 @@
<dependency>
<groupId>com.github.tlrx</groupId>
<artifactId>elasticsearch-test</artifactId>
<version>0.0.7</version>
<version>1.2.1</version>
<scope>test</scope>
<exclusions>
<exclusion>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -31,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;
Expand All @@ -52,6 +52,8 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.elasticsearch.action.support.ActionFilters;
import org.elasticsearch.index.shard.ShardId;


public class TransportViewAction extends TransportShardSingleOperationAction<ViewRequest, ViewResponse> {
Expand All @@ -66,13 +68,19 @@ public TransportViewAction(Settings settings, ThreadPool threadPool,
TransportService transportService,
IndicesService indicesService,
ViewService viewService,
TransportSearchAction searchAction) {
super(settings, threadPool, clusterService, transportService);
TransportSearchAction searchAction,
ActionFilters actionFilters) {
super(settings, ViewAction.NAME, threadPool, clusterService, transportService, actionFilters);
this.indicesService = indicesService;
this.viewService = viewService;
this.searchAction = searchAction;
}

@Override
protected boolean resolveIndex() {
return true;
}

@Override
protected String executor() {
return ThreadPool.Names.GENERIC;
Expand All @@ -88,37 +96,30 @@ 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);
}

@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.concreteIndex(), 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);
IndexShard indexShard = indexService.shardSafe(shardId.id());
GetResult getResult = indexShard.getService().get(request.type(), request.id(), null, false, 1, VersionType.INTERNAL, null, false);

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
Expand All @@ -132,7 +133,7 @@ protected ViewResponse shardOperation(ViewRequest request, int shardId) throws E
Map<String, Object> 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);
}
}
}
Expand All @@ -142,10 +143,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
Expand Down Expand Up @@ -283,7 +284,7 @@ private ViewContext extract(Map<String, Object> 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);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/elasticsearch/action/view/ViewAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<ViewRequest, ViewResponse, ViewRequestBuilder> {
public class ViewAction extends ClientAction<ViewRequest, ViewResponse, ViewRequestBuilder> {

public static final ViewAction INSTANCE = new ViewAction();
public static final String NAME = "view";
Expand Down
74 changes: 70 additions & 4 deletions src/main/java/org/elasticsearch/action/view/ViewRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,15 @@
*/
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.Required;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;


public class ViewRequest extends SingleShardOperationRequest<ViewRequest> {

Expand All @@ -30,12 +36,42 @@ public class ViewRequest extends SingleShardOperationRequest<ViewRequest> {
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
*/
Expand All @@ -59,7 +95,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;
Expand All @@ -84,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 + "]";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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<ViewRequest, ViewResponse, ViewRequestBuilder> {

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));
}

/**
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/org/elasticsearch/action/view/ViewResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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);
}
}
2 changes: 2 additions & 0 deletions src/main/java/org/elasticsearch/plugin/view/ViewPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@

public class ViewPlugin extends AbstractPlugin {

@Override
public String description() {
return "Elasticsearch View Plugin";
}

@Override
public String name() {
return "view-plugin";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ 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);
}

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"));
Expand All @@ -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);
}
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down