From d660b18298fcdd13343494d2492bff8be7727253 Mon Sep 17 00:00:00 2001 From: Martijn van Groningen Date: Fri, 1 Nov 2013 11:17:16 +0100 Subject: [PATCH] Fixed bug (NPE) in mpercolate api, defaults from percolating existing documents where also applied for the regular percolate api, these defaults were none existing (null). --- .../percolate/MultiPercolateRequest.java | 28 +++++++++---------- .../MultiPercolatorRequestTests.java | 12 +++++++- .../action/percolate/mpercolate2.json | 2 ++ 3 files changed, 27 insertions(+), 15 deletions(-) diff --git a/src/main/java/org/elasticsearch/action/percolate/MultiPercolateRequest.java b/src/main/java/org/elasticsearch/action/percolate/MultiPercolateRequest.java index c1b3d036b62..46f5c2d843a 100644 --- a/src/main/java/org/elasticsearch/action/percolate/MultiPercolateRequest.java +++ b/src/main/java/org/elasticsearch/action/percolate/MultiPercolateRequest.java @@ -199,6 +199,20 @@ public class MultiPercolateRequest extends ActionRequest percolateRequest.ignoreIndices(IgnoreIndices.fromString((String) value)); } } + + // Setting values based on get request, if needed... + if ((percolateRequest.indices() == null || percolateRequest.indices().length == 0) && getRequest.index() != null) { + percolateRequest.indices(getRequest.index()); + } + if (percolateRequest.documentType() == null && getRequest.type() != null) { + percolateRequest.documentType(getRequest.type()); + } + if (percolateRequest.routing() == null && getRequest.routing() != null) { + percolateRequest.routing(getRequest.routing()); + } + if (percolateRequest.preference() == null && getRequest.preference() != null) { + percolateRequest.preference(getRequest.preference()); + } } else { for (Map.Entry entry : header.entrySet()) { Object value = entry.getValue(); @@ -219,20 +233,6 @@ public class MultiPercolateRequest extends ActionRequest } } } - - // Setting defaults, if needed... - if ((percolateRequest.indices() == null || percolateRequest.indices().length == 0) && percolateRequest.getRequest().index() != null) { - percolateRequest.indices(percolateRequest.getRequest().index()); - } - if (percolateRequest.documentType() == null && percolateRequest.getRequest().type() != null) { - percolateRequest.documentType(percolateRequest.getRequest().type()); - } - if (percolateRequest.routing() == null && percolateRequest.getRequest().routing() != null) { - percolateRequest.routing(percolateRequest.getRequest().routing()); - } - if (percolateRequest.preference() == null && percolateRequest.getRequest().preference() != null) { - percolateRequest.preference(percolateRequest.getRequest().preference()); - } } private String[] parseArray(XContentParser parser) throws IOException { diff --git a/src/test/java/org/elasticsearch/action/percolate/MultiPercolatorRequestTests.java b/src/test/java/org/elasticsearch/action/percolate/MultiPercolatorRequestTests.java index 2a0fdf7bbb2..164f5bdd386 100644 --- a/src/test/java/org/elasticsearch/action/percolate/MultiPercolatorRequestTests.java +++ b/src/test/java/org/elasticsearch/action/percolate/MultiPercolatorRequestTests.java @@ -125,7 +125,7 @@ public class MultiPercolatorRequestTests extends ElasticsearchTestCase { request.indices("my-index1").documentType("my-type1").ignoreIndices(IgnoreIndices.MISSING); request.add(data, 0, data.length, false); - assertThat(request.requests().size(), equalTo(2)); + assertThat(request.requests().size(), equalTo(3)); PercolateRequest percolateRequest = request.requests().get(0); assertThat(percolateRequest.indices()[0], equalTo("my-index1")); assertThat(percolateRequest.documentType(), equalTo("my-type1")); @@ -149,6 +149,16 @@ public class MultiPercolatorRequestTests extends ElasticsearchTestCase { assertThat(percolateRequest.source(), notNullValue()); sourceMap = XContentFactory.xContent(percolateRequest.source()).createParser(percolateRequest.source()).map(); assertThat(sourceMap.get("doc"), equalTo((Object)MapBuilder.newMapBuilder().put("field1", "value2").map())); + + percolateRequest = request.requests().get(2); + assertThat(percolateRequest.indices()[0], equalTo("my-index1")); + assertThat(percolateRequest.documentType(), equalTo("my-type1")); + assertThat(percolateRequest.ignoreIndices(), equalTo(IgnoreIndices.MISSING)); + assertThat(percolateRequest.onlyCount(), equalTo(false)); + assertThat(percolateRequest.getRequest(), nullValue()); + assertThat(percolateRequest.source(), notNullValue()); + sourceMap = XContentFactory.xContent(percolateRequest.source()).createParser(percolateRequest.source()).map(); + assertThat(sourceMap.get("doc"), equalTo((Object)MapBuilder.newMapBuilder().put("field1", "value3").map())); } } diff --git a/src/test/java/org/elasticsearch/action/percolate/mpercolate2.json b/src/test/java/org/elasticsearch/action/percolate/mpercolate2.json index 8d5acf492d7..27899a45152 100644 --- a/src/test/java/org/elasticsearch/action/percolate/mpercolate2.json +++ b/src/test/java/org/elasticsearch/action/percolate/mpercolate2.json @@ -2,3 +2,5 @@ {"doc" : {"field1" : "value1"}} {"percolate" : {"index" : "my-index1", "type" : "my-type1", "routing" : "my-routing-1", "preference" : "_local", "ignore_indices" : "missing"}} {"doc" : {"field1" : "value2"}} +{"percolate" : {}} +{"doc" : {"field1" : "value3"}}