diff --git a/src/main/java/org/elasticsearch/script/python/PythonScriptEngineService.java b/src/main/java/org/elasticsearch/script/python/PythonScriptEngineService.java index 16da9eba698f2..2a94949830d93 100644 --- a/src/main/java/org/elasticsearch/script/python/PythonScriptEngineService.java +++ b/src/main/java/org/elasticsearch/script/python/PythonScriptEngineService.java @@ -107,8 +107,10 @@ public class PythonExecutableScript implements ExecutableScript { public PythonExecutableScript(PyCode code, Map vars) { this.code = code; this.pyVars = new PyStringMap(); - for (Map.Entry entry : vars.entrySet()) { - pyVars.__setitem__(entry.getKey(), Py.java2py(entry.getValue())); + if (vars != null) { + for (Map.Entry entry : vars.entrySet()) { + pyVars.__setitem__(entry.getKey(), Py.java2py(entry.getValue())); + } } } diff --git a/src/test/java/org/elasticsearch/script/python/PythonScriptSearchTests.java b/src/test/java/org/elasticsearch/script/python/PythonScriptSearchTests.java index 642658d9b1c2e..2f90291d6517f 100644 --- a/src/test/java/org/elasticsearch/script/python/PythonScriptSearchTests.java +++ b/src/test/java/org/elasticsearch/script/python/PythonScriptSearchTests.java @@ -23,6 +23,7 @@ import org.elasticsearch.action.search.SearchType; import org.elasticsearch.search.sort.SortOrder; import org.elasticsearch.test.ElasticsearchIntegrationTest; +import org.hamcrest.CoreMatchers; import org.junit.After; import org.junit.Test; @@ -30,11 +31,12 @@ import java.util.List; import java.util.Map; -import static org.elasticsearch.client.Requests.*; +import static org.elasticsearch.client.Requests.searchRequest; import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.index.query.FilterBuilders.scriptFilter; import static org.elasticsearch.index.query.QueryBuilders.*; import static org.elasticsearch.search.builder.SearchSourceBuilder.searchSource; +import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.Matchers.equalTo; /** @@ -205,4 +207,26 @@ public void testCustomScriptBoost() throws Exception { logger.info(" --> Hit[0] {} Explanation {}", response.getHits().getAt(0).id(), response.getHits().getAt(0).explanation()); logger.info(" --> Hit[1] {} Explanation {}", response.getHits().getAt(1).id(), response.getHits().getAt(1).explanation()); } + + /** + * Test case for #4: https://github.com/elasticsearch/elasticsearch-lang-python/issues/4 + * Update request that uses python script with no parameters fails with NullPointerException + * @throws Exception + */ + @Test + public void testPythonEmptyParameters() throws Exception { + wipeIndices("test"); + createIndex("test"); + index("test", "type1", "1", jsonBuilder().startObject().field("myfield", "foo").endObject()); + refresh(); + + client().prepareUpdate("test", "type1", "1").setScriptLang("python").setScript("ctx[\"_source\"][\"myfield\"]=\"bar\"") + .execute().actionGet(); + refresh(); + + Object value = get("test", "type1", "1").getSourceAsMap().get("myfield"); + assertThat(value instanceof String, is(true)); + + assertThat((String) value, CoreMatchers.equalTo("bar")); + } }