Skip to content

Commit

Permalink
Multi GET API, closes #1068.
Browse files Browse the repository at this point in the history
  • Loading branch information
kimchy committed Jun 27, 2011
1 parent 4755644 commit 9bf686e
Show file tree
Hide file tree
Showing 24 changed files with 1,549 additions and 49 deletions.
Expand Up @@ -59,6 +59,8 @@
import org.elasticsearch.action.deletebyquery.TransportIndexDeleteByQueryAction;
import org.elasticsearch.action.deletebyquery.TransportShardDeleteByQueryAction;
import org.elasticsearch.action.get.TransportGetAction;
import org.elasticsearch.action.get.TransportMultiGetAction;
import org.elasticsearch.action.get.TransportShardMultiGetAction;
import org.elasticsearch.action.index.TransportIndexAction;
import org.elasticsearch.action.mlt.TransportMoreLikeThisAction;
import org.elasticsearch.action.percolate.TransportPercolateAction;
Expand Down Expand Up @@ -124,6 +126,9 @@ public class TransportActionModule extends AbstractModule {
bind(TransportShardDeleteAction.class).asEagerSingleton();
bind(TransportCountAction.class).asEagerSingleton();

bind(TransportMultiGetAction.class).asEagerSingleton();
bind(TransportShardMultiGetAction.class).asEagerSingleton();

bind(TransportBulkAction.class).asEagerSingleton();
bind(TransportShardBulkAction.class).asEagerSingleton();

Expand Down
Expand Up @@ -36,6 +36,8 @@ public class TransportActions {

public static final String GET = "indices/get";

public static final String MULTI_GET = "indices/mget";

public static final String SEARCH = "indices/search";

public static final String SEARCH_SCROLL = "indices/searchScroll";
Expand Down
Expand Up @@ -247,6 +247,7 @@ static final class Fields {
static final XContentBuilderString _TYPE = new XContentBuilderString("_type");
static final XContentBuilderString _ID = new XContentBuilderString("_id");
static final XContentBuilderString _VERSION = new XContentBuilderString("_version");
static final XContentBuilderString EXISTS = new XContentBuilderString("exists");
static final XContentBuilderString FIELDS = new XContentBuilderString("fields");
}

Expand All @@ -256,6 +257,7 @@ static final class Fields {
builder.field(Fields._INDEX, index);
builder.field(Fields._TYPE, type);
builder.field(Fields._ID, id);
builder.field(Fields.EXISTS, false);
builder.endObject();
} else {
builder.startObject();
Expand All @@ -265,6 +267,7 @@ static final class Fields {
if (version != -1) {
builder.field(Fields._VERSION, version);
}
builder.field(Fields.EXISTS, true);
if (source != null) {
RestXContentBuilder.restDocumentSource(source.bytes(), source.offset(), source.length(), builder, params);
}
Expand Down
@@ -0,0 +1,162 @@
/*
* Licensed to Elastic Search and Shay Banon under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. Elastic Search licenses this
* file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.elasticsearch.action.get;

import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Streamable;

import java.io.IOException;

/**
* A single multi get response.
*/
public class MultiGetItemResponse implements Streamable {

private GetResponse response;
private MultiGetResponse.Failure failure;

MultiGetItemResponse() {

}

public MultiGetItemResponse(GetResponse response, MultiGetResponse.Failure failure) {
this.response = response;
this.failure = failure;
}

/**
* The index name of the document.
*/
public String index() {
if (failure != null) {
return failure.index();
}
return response.index();
}

/**
* The index name of the document.
*/
public String getIndex() {
return index();
}

/**
* The type of the document.
*/
public String type() {
if (failure != null) {
return failure.type();
}
return response.type();
}

/**
* The type of the document.
*/
public String getType() {
return type();
}

/**
* The id of the document.
*/
public String id() {
if (failure != null) {
return failure.id();
}
return response.id();
}

/**
* The id of the document.
*/
public String getId() {
return id();
}

/**
* Is this a failed execution?
*/
public boolean failed() {
return failure != null;
}

/**
* Is this a failed execution?
*/
public boolean isFailed() {
return failed();
}

/**
* The actual get response, <tt>null</tt> if its a failure.
*/
public GetResponse response() {
return this.response;
}

/**
* The actual get response, <tt>null</tt> if its a failure.
*/
public GetResponse getResponse() {
return this.response;
}

/**
* The failure if relevant.
*/
public MultiGetResponse.Failure failure() {
return this.failure;
}

/**
* The failure if relevant.
*/
public MultiGetResponse.Failure getFailure() {
return failure();
}

public static MultiGetItemResponse readItemResponse(StreamInput in) throws IOException {
MultiGetItemResponse response = new MultiGetItemResponse();
response.readFrom(in);
return response;
}

@Override public void readFrom(StreamInput in) throws IOException {
if (in.readBoolean()) {
failure = MultiGetResponse.Failure.readFailure(in);
} else {
response = new GetResponse();
response.readFrom(in);
}
}

@Override public void writeTo(StreamOutput out) throws IOException {
if (failure != null) {
out.writeBoolean(true);
failure.writeTo(out);
} else {
out.writeBoolean(false);
response.writeTo(out);
}
}
}

0 comments on commit 9bf686e

Please sign in to comment.