Update request that uses python script with no parameters fails with NullPointerException

Closes #4.
This commit is contained in:
David Pilato 2014-01-14 19:09:10 +01:00
parent c065e3884b
commit 3fc121f603
2 changed files with 29 additions and 3 deletions

View File

@ -107,8 +107,10 @@ public class PythonScriptEngineService extends AbstractComponent implements Scri
public PythonExecutableScript(PyCode code, Map<String, Object> vars) {
this.code = code;
this.pyVars = new PyStringMap();
for (Map.Entry<String, Object> entry : vars.entrySet()) {
pyVars.__setitem__(entry.getKey(), Py.java2py(entry.getValue()));
if (vars != null) {
for (Map.Entry<String, Object> entry : vars.entrySet()) {
pyVars.__setitem__(entry.getKey(), Py.java2py(entry.getValue()));
}
}
}

View File

@ -23,6 +23,7 @@ import org.elasticsearch.action.search.SearchResponse;
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.Arrays;
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 class PythonScriptSearchTests extends ElasticsearchIntegrationTest {
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"));
}
}