Multi Get: Allow to specify fields to fetch in the URI, and apply it automatically to all docs to get without explicit fields, closes #1281.

This commit is contained in:
Shay Banon 2011-08-26 17:42:22 +03:00
parent 58bfe5d86f
commit fe0a1d424d
2 changed files with 16 additions and 3 deletions

View File

@ -228,7 +228,7 @@ public class MultiGetRequest implements ActionRequest {
return this;
}
public void add(@Nullable String defaultIndex, @Nullable String defaultType, byte[] data, int from, int length) throws Exception {
public void add(@Nullable String defaultIndex, @Nullable String defaultType, @Nullable String[] defaultFields, byte[] data, int from, int length) throws Exception {
XContentParser parser = XContentFactory.xContent(data, from, length).createParser(data, from, length);
try {
XContentParser.Token token;
@ -269,7 +269,13 @@ public class MultiGetRequest implements ActionRequest {
}
}
}
add(new Item(index, type, id).routing(routing).fields(fields == null ? null : fields.toArray(new String[fields.size()])));
String[] aFields;
if (fields != null) {
aFields = fields.toArray(new String[fields.size()]);
} else {
aFields = defaultFields;
}
add(new Item(index, type, id).routing(routing).fields(aFields));
}
} else if ("ids".equals(currentFieldName)) {
while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {

View File

@ -23,6 +23,7 @@ import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.get.MultiGetRequest;
import org.elasticsearch.action.get.MultiGetResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
@ -58,8 +59,14 @@ public class RestMultiGetAction extends BaseRestHandler {
multiGetRequest.preference(request.param("preference"));
multiGetRequest.realtime(request.paramAsBooleanOptional("realtime", null));
String[] sFields = null;
String sField = request.param("fields");
if (sField != null) {
sFields = Strings.splitStringByCommaToArray(sField);
}
try {
multiGetRequest.add(request.param("index"), request.param("type"), request.contentByteArray(), request.contentByteArrayOffset(), request.contentLength());
multiGetRequest.add(request.param("index"), request.param("type"), sFields, request.contentByteArray(), request.contentByteArrayOffset(), request.contentLength());
} catch (Exception e) {
try {
XContentBuilder builder = restContentBuilder(request);