Skip to content

Commit

Permalink
Update request that uses python script with no parameters fails with …
Browse files Browse the repository at this point in the history
…NullPointerException

Closes #4.
  • Loading branch information
dadoonet committed Jan 14, 2014
1 parent c065e38 commit 3fc121f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,10 @@ public class PythonExecutableScript implements ExecutableScript {
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()));
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,20 @@
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;

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;

/**
Expand Down Expand Up @@ -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"));
}
}

0 comments on commit 3fc121f

Please sign in to comment.