Include script field even if it value is null

Include script field even if it value is null.

Closes #16408.
This commit is contained in:
Jeff Evans 2016-06-17 15:41:25 -05:00 committed by Nik Everett
parent 1e16c22d03
commit e9f2548ee0
2 changed files with 20 additions and 3 deletions

View File

@ -64,9 +64,7 @@ public final class ScriptFieldsFetchSubPhase implements FetchSubPhase {
SearchHitField hitField = hitContext.hit().fields().get(scriptField.name());
if (hitField == null) {
final List<Object> values;
if (value == null) {
values = Collections.emptyList();
} else if (value instanceof Collection) {
if (value instanceof Collection) {
// TODO: use diamond operator once JI-9019884 is fixed
values = new ArrayList<>((Collection<?>) value);
} else {

View File

@ -69,6 +69,7 @@ import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertNoFa
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
@ -369,6 +370,24 @@ public class SearchFieldsTests extends ESIntegTestCase {
assertThat(((Map<?, ?>) sObj2Arr3.get(0)).get("arr3_field1").toString(), equalTo("arr3_value1"));
}
public void testScriptFieldsForNullReturn() throws Exception {
client().prepareIndex("test", "type1", "1")
.setSource("foo", "bar")
.setRefresh(true).get();
SearchResponse response = client().prepareSearch().setQuery(matchAllQuery())
.addScriptField("test_script_1", new Script("return null"))
.get();
assertNoFailures(response);
SearchHitField fieldObj = response.getHits().getAt(0).field("test_script_1");
assertThat(fieldObj, notNullValue());
List<?> fieldValues = fieldObj.values();
assertThat(fieldValues, hasSize(1));
assertThat(fieldValues.get(0), nullValue());
}
public void testPartialFields() throws Exception {
createIndex("test");
client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForYellowStatus().execute().actionGet();