allow to provide fields for multi get
This commit is contained in:
parent
2e83a2f045
commit
f6ebee3785
|
@ -44,6 +44,7 @@ public class MultiGetRequest implements ActionRequest {
|
|||
private String type;
|
||||
private String id;
|
||||
private String routing;
|
||||
private String[] fields;
|
||||
|
||||
Item() {
|
||||
|
||||
|
@ -86,6 +87,15 @@ public class MultiGetRequest implements ActionRequest {
|
|||
return this.routing;
|
||||
}
|
||||
|
||||
public Item fields(String... fields) {
|
||||
this.fields = fields;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String[] fields() {
|
||||
return this.fields;
|
||||
}
|
||||
|
||||
public static Item readItem(StreamInput in) throws IOException {
|
||||
Item item = new Item();
|
||||
item.readFrom(in);
|
||||
|
@ -101,6 +111,13 @@ public class MultiGetRequest implements ActionRequest {
|
|||
if (in.readBoolean()) {
|
||||
routing = in.readUTF();
|
||||
}
|
||||
int size = in.readVInt();
|
||||
if (size > 0) {
|
||||
fields = new String[size];
|
||||
for (int i = 0; i < size; i++) {
|
||||
fields[i] = in.readUTF();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override public void writeTo(StreamOutput out) throws IOException {
|
||||
|
@ -118,6 +135,14 @@ public class MultiGetRequest implements ActionRequest {
|
|||
out.writeBoolean(true);
|
||||
out.writeUTF(routing);
|
||||
}
|
||||
if (fields == null) {
|
||||
out.writeVInt(0);
|
||||
} else {
|
||||
out.writeVInt(fields.length);
|
||||
for (String field : fields) {
|
||||
out.writeUTF(field);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -216,6 +241,7 @@ public class MultiGetRequest implements ActionRequest {
|
|||
String type = defaultType;
|
||||
String id = null;
|
||||
String routing = null;
|
||||
List<String> fields = null;
|
||||
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
|
||||
if (token == XContentParser.Token.FIELD_NAME) {
|
||||
currentFieldName = parser.currentName();
|
||||
|
@ -229,9 +255,16 @@ public class MultiGetRequest implements ActionRequest {
|
|||
} else if ("_routing".equals(currentFieldName) || "routing".equals(currentFieldName)) {
|
||||
routing = parser.text();
|
||||
}
|
||||
} else if (token == XContentParser.Token.START_ARRAY) {
|
||||
if ("fields".equals(currentFieldName)) {
|
||||
fields = new ArrayList<String>();
|
||||
while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
|
||||
fields.add(parser.text());
|
||||
}
|
||||
}
|
||||
add(new Item(index, type, id).routing(routing));
|
||||
}
|
||||
}
|
||||
add(new Item(index, type, id).routing(routing).fields(fields == null ? null : fields.toArray(new String[fields.size()])));
|
||||
}
|
||||
} else if ("ids".equals(currentFieldName)) {
|
||||
while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
|
||||
|
|
|
@ -39,6 +39,7 @@ public class MultiGetShardRequest extends SingleShardOperationRequest {
|
|||
TIntArrayList locations;
|
||||
List<String> types;
|
||||
List<String> ids;
|
||||
List<String[]> fields;
|
||||
|
||||
MultiGetShardRequest() {
|
||||
|
||||
|
@ -50,6 +51,7 @@ public class MultiGetShardRequest extends SingleShardOperationRequest {
|
|||
locations = new TIntArrayList();
|
||||
types = new ArrayList<String>();
|
||||
ids = new ArrayList<String>();
|
||||
fields = new ArrayList<String[]>();
|
||||
}
|
||||
|
||||
public int shardId() {
|
||||
|
@ -88,10 +90,11 @@ public class MultiGetShardRequest extends SingleShardOperationRequest {
|
|||
return this;
|
||||
}
|
||||
|
||||
public void add(int location, @Nullable String type, String id) {
|
||||
locations.add(location);
|
||||
types.add(type);
|
||||
ids.add(id);
|
||||
public void add(int location, @Nullable String type, String id, String[] fields) {
|
||||
this.locations.add(location);
|
||||
this.types.add(type);
|
||||
this.ids.add(id);
|
||||
this.fields.add(fields);
|
||||
}
|
||||
|
||||
@Override public void readFrom(StreamInput in) throws IOException {
|
||||
|
@ -100,6 +103,7 @@ public class MultiGetShardRequest extends SingleShardOperationRequest {
|
|||
locations = new TIntArrayList(size);
|
||||
types = new ArrayList<String>(size);
|
||||
ids = new ArrayList<String>(size);
|
||||
fields = new ArrayList<String[]>(size);
|
||||
for (int i = 0; i < size; i++) {
|
||||
locations.add(in.readVInt());
|
||||
if (in.readBoolean()) {
|
||||
|
@ -108,6 +112,16 @@ public class MultiGetShardRequest extends SingleShardOperationRequest {
|
|||
types.add(null);
|
||||
}
|
||||
ids.add(in.readUTF());
|
||||
int size1 = in.readVInt();
|
||||
if (size1 > 0) {
|
||||
String[] fields = new String[size1];
|
||||
for (int j = 0; j < size1; j++) {
|
||||
fields[j] = in.readUTF();
|
||||
}
|
||||
this.fields.add(fields);
|
||||
} else {
|
||||
fields.add(null);
|
||||
}
|
||||
}
|
||||
|
||||
if (in.readBoolean()) {
|
||||
|
@ -134,6 +148,14 @@ public class MultiGetShardRequest extends SingleShardOperationRequest {
|
|||
out.writeUTF(types.get(i));
|
||||
}
|
||||
out.writeUTF(ids.get(i));
|
||||
if (fields.get(i) == null) {
|
||||
out.writeVInt(0);
|
||||
} else {
|
||||
out.writeVInt(fields.get(i).length);
|
||||
for (String field : fields.get(i)) {
|
||||
out.writeUTF(field);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (preference == null) {
|
||||
|
|
|
@ -67,7 +67,7 @@ public class TransportMultiGetAction extends BaseAction<MultiGetRequest, MultiGe
|
|||
|
||||
shardRequests.put(shardId, shardRequest);
|
||||
}
|
||||
shardRequest.add(i, item.type(), item.id());
|
||||
shardRequest.add(i, item.type(), item.id(), item.fields());
|
||||
}
|
||||
|
||||
final MultiGetItemResponse[] responses = new MultiGetItemResponse[request.items.size()];
|
||||
|
|
|
@ -102,9 +102,10 @@ public class TransportShardMultiGetAction extends TransportShardSingleOperationA
|
|||
for (int i = 0; i < request.locations.size(); i++) {
|
||||
String type = request.types.get(i);
|
||||
String id = request.ids.get(i);
|
||||
String[] fields = request.fields.get(i);
|
||||
|
||||
try {
|
||||
GetResponse getResponse = TransportGetAction.load(logger, scriptService, indexService, indexShard, request.index(), type, id, null, request.realtime());
|
||||
GetResponse getResponse = TransportGetAction.load(logger, scriptService, indexService, indexShard, request.index(), type, id, fields, request.realtime());
|
||||
response.add(request.locations.get(i), getResponse);
|
||||
} catch (Exception e) {
|
||||
response.add(request.locations.get(i), new MultiGetResponse.Failure(request.index(), type, id, ExceptionsHelper.detailedMessage(e)));
|
||||
|
|
|
@ -23,6 +23,7 @@ import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse;
|
|||
import org.elasticsearch.action.admin.cluster.health.ClusterHealthStatus;
|
||||
import org.elasticsearch.action.delete.DeleteResponse;
|
||||
import org.elasticsearch.action.get.GetResponse;
|
||||
import org.elasticsearch.action.get.MultiGetRequest;
|
||||
import org.elasticsearch.action.get.MultiGetResponse;
|
||||
import org.elasticsearch.client.Client;
|
||||
import org.elasticsearch.common.settings.ImmutableSettings;
|
||||
|
@ -176,5 +177,15 @@ public class GetActionTests extends AbstractNodesTests {
|
|||
assertThat(response.responses()[3].response().exists(), equalTo(true));
|
||||
assertThat(response.responses()[4].id(), equalTo("11"));
|
||||
assertThat(response.responses()[4].response().exists(), 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();
|
||||
|
||||
assertThat(response.responses().length, equalTo(2));
|
||||
assertThat(response.responses()[0].response().source(), nullValue());
|
||||
assertThat(response.responses()[0].response().field("field").values().get(0).toString(), equalTo("value1"));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue