Multi-percolate respects the `rest.action.multi.allow_explicit_index` setting

Closes #4284
This commit is contained in:
Martijn van Groningen 2014-01-08 11:18:50 +01:00
parent ad50afbec8
commit 2a0842f1b2
2 changed files with 16 additions and 6 deletions

View File

@ -70,10 +70,10 @@ public class MultiPercolateRequest extends ActionRequest<MultiPercolateRequest>
}
public MultiPercolateRequest add(byte[] data, int from, int length, boolean contentUnsafe) throws Exception {
return add(new BytesArray(data, from, length), contentUnsafe);
return add(new BytesArray(data, from, length), contentUnsafe, true);
}
public MultiPercolateRequest add(BytesReference data, boolean contentUnsafe) throws Exception {
public MultiPercolateRequest add(BytesReference data, boolean contentUnsafe, boolean allowExplicitIndex) throws Exception {
XContent xContent = XContentFactory.xContent(data);
int from = 0;
int length = data.length();
@ -119,10 +119,10 @@ public class MultiPercolateRequest extends ActionRequest<MultiPercolateRequest>
}
String percolateAction = parser.currentName();
if ("percolate".equals(percolateAction)) {
parsePercolateAction(parser, percolateRequest);
parsePercolateAction(parser, percolateRequest, allowExplicitIndex);
} else if ("count".equals(percolateAction)) {
percolateRequest.onlyCount(true);
parsePercolateAction(parser, percolateRequest);
parsePercolateAction(parser, percolateRequest, allowExplicitIndex);
} else {
throw new ElasticsearchParseException(percolateAction + " isn't a supported percolate operation");
}
@ -151,7 +151,7 @@ public class MultiPercolateRequest extends ActionRequest<MultiPercolateRequest>
return this;
}
private void parsePercolateAction(XContentParser parser, PercolateRequest percolateRequest) throws IOException {
private void parsePercolateAction(XContentParser parser, PercolateRequest percolateRequest, boolean allowExplicitIndex) throws IOException {
String globalIndex = indices != null && indices.length > 0 ? indices[0] : null;
Map<String, Object> header = new HashMap<String, Object>();
@ -182,6 +182,9 @@ public class MultiPercolateRequest extends ActionRequest<MultiPercolateRequest>
getRequest.id((String) value);
header.put("id", entry.getValue());
} else if ("index".equals(entry.getKey()) || "indices".equals(entry.getKey())) {
if (!allowExplicitIndex) {
throw new ElasticsearchIllegalArgumentException("explicit index in multi percolate is not allowed");
}
getRequest.index((String) value);
} else if ("type".equals(entry.getKey())) {
getRequest.type((String) value);
@ -242,6 +245,9 @@ public class MultiPercolateRequest extends ActionRequest<MultiPercolateRequest>
for (Map.Entry<String, Object> entry : header.entrySet()) {
Object value = entry.getValue();
if ("index".equals(entry.getKey()) || "indices".equals(entry.getKey())) {
if (!allowExplicitIndex) {
throw new ElasticsearchIllegalArgumentException("explicit index in multi percolate is not allowed");
}
if (value instanceof String[]) {
percolateRequest.indices((String[]) value);
} else {

View File

@ -41,6 +41,8 @@ import static org.elasticsearch.rest.action.support.RestXContentBuilder.restCont
*/
public class RestMultiPercolateAction extends BaseRestHandler {
private final boolean allowExplicitIndex;
@Inject
public RestMultiPercolateAction(Settings settings, Client client, RestController controller) {
super(settings, client);
@ -51,6 +53,8 @@ public class RestMultiPercolateAction extends BaseRestHandler {
controller.registerHandler(GET, "/_mpercolate", this);
controller.registerHandler(GET, "/{index}/_mpercolate", this);
controller.registerHandler(GET, "/{index}/{type}/_mpercolate", this);
this.allowExplicitIndex = settings.getAsBoolean("rest.action.multi.allow_explicit_index", true);
}
@Override
@ -61,7 +65,7 @@ public class RestMultiPercolateAction extends BaseRestHandler {
multiPercolateRequest.documentType(restRequest.param("type"));
try {
multiPercolateRequest.add(restRequest.content(), restRequest.contentUnsafe());
multiPercolateRequest.add(restRequest.content(), restRequest.contentUnsafe(), allowExplicitIndex);
} catch (Exception e) {
try {
restChannel.sendResponse(new XContentThrowableRestResponse(restRequest, e));