From 2a0842f1b261a3cf22e0fce15bc2fb540b939290 Mon Sep 17 00:00:00 2001 From: Martijn van Groningen Date: Wed, 8 Jan 2014 11:18:50 +0100 Subject: [PATCH] Multi-percolate respects the `rest.action.multi.allow_explicit_index` setting Closes #4284 --- .../action/percolate/MultiPercolateRequest.java | 16 +++++++++++----- .../percolate/RestMultiPercolateAction.java | 6 +++++- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/elasticsearch/action/percolate/MultiPercolateRequest.java b/src/main/java/org/elasticsearch/action/percolate/MultiPercolateRequest.java index 4991d62cc57..6299c008fae 100644 --- a/src/main/java/org/elasticsearch/action/percolate/MultiPercolateRequest.java +++ b/src/main/java/org/elasticsearch/action/percolate/MultiPercolateRequest.java @@ -70,10 +70,10 @@ public class MultiPercolateRequest extends ActionRequest } 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 } 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 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 header = new HashMap(); @@ -182,6 +182,9 @@ public class MultiPercolateRequest extends ActionRequest 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 for (Map.Entry 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 { diff --git a/src/main/java/org/elasticsearch/rest/action/percolate/RestMultiPercolateAction.java b/src/main/java/org/elasticsearch/rest/action/percolate/RestMultiPercolateAction.java index 1abb7ab4672..7dc18d6f08f 100644 --- a/src/main/java/org/elasticsearch/rest/action/percolate/RestMultiPercolateAction.java +++ b/src/main/java/org/elasticsearch/rest/action/percolate/RestMultiPercolateAction.java @@ -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));