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 { 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); XContent xContent = XContentFactory.xContent(data);
int from = 0; int from = 0;
int length = data.length(); int length = data.length();
@ -119,10 +119,10 @@ public class MultiPercolateRequest extends ActionRequest<MultiPercolateRequest>
} }
String percolateAction = parser.currentName(); String percolateAction = parser.currentName();
if ("percolate".equals(percolateAction)) { if ("percolate".equals(percolateAction)) {
parsePercolateAction(parser, percolateRequest); parsePercolateAction(parser, percolateRequest, allowExplicitIndex);
} else if ("count".equals(percolateAction)) { } else if ("count".equals(percolateAction)) {
percolateRequest.onlyCount(true); percolateRequest.onlyCount(true);
parsePercolateAction(parser, percolateRequest); parsePercolateAction(parser, percolateRequest, allowExplicitIndex);
} else { } else {
throw new ElasticsearchParseException(percolateAction + " isn't a supported percolate operation"); throw new ElasticsearchParseException(percolateAction + " isn't a supported percolate operation");
} }
@ -151,7 +151,7 @@ public class MultiPercolateRequest extends ActionRequest<MultiPercolateRequest>
return this; 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; String globalIndex = indices != null && indices.length > 0 ? indices[0] : null;
Map<String, Object> header = new HashMap<String, Object>(); Map<String, Object> header = new HashMap<String, Object>();
@ -182,6 +182,9 @@ public class MultiPercolateRequest extends ActionRequest<MultiPercolateRequest>
getRequest.id((String) value); getRequest.id((String) value);
header.put("id", entry.getValue()); header.put("id", entry.getValue());
} else if ("index".equals(entry.getKey()) || "indices".equals(entry.getKey())) { } 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); getRequest.index((String) value);
} else if ("type".equals(entry.getKey())) { } else if ("type".equals(entry.getKey())) {
getRequest.type((String) value); getRequest.type((String) value);
@ -242,6 +245,9 @@ public class MultiPercolateRequest extends ActionRequest<MultiPercolateRequest>
for (Map.Entry<String, Object> entry : header.entrySet()) { for (Map.Entry<String, Object> entry : header.entrySet()) {
Object value = entry.getValue(); Object value = entry.getValue();
if ("index".equals(entry.getKey()) || "indices".equals(entry.getKey())) { 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[]) { if (value instanceof String[]) {
percolateRequest.indices((String[]) value); percolateRequest.indices((String[]) value);
} else { } else {

View File

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