Skip to content

Commit

Permalink
Multi Get: Allow to specify fields to fetch in the URI, and apply it …
Browse files Browse the repository at this point in the history
…automatically to all docs to get without explicit fields, closes elastic#1281.
  • Loading branch information
kimchy committed Aug 26, 2011
1 parent 4e1618c commit 3fd015a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
Expand Up @@ -228,7 +228,7 @@ public MultiGetRequest refresh(boolean refresh) {
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;
Expand Down Expand Up @@ -269,7 +269,13 @@ public void add(@Nullable String defaultIndex, @Nullable String defaultType, byt
}
}
}
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) {
Expand Down
Expand Up @@ -23,6 +23,7 @@
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;
Expand Down Expand Up @@ -58,8 +59,14 @@ public class RestMultiGetAction extends BaseRestHandler {
multiGetRequest.preference(request.param("preference"));
multiGetRequest.realtime(request.paramAsBoolean("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);
Expand Down

0 comments on commit 3fd015a

Please sign in to comment.