Browse Source

Include script field even if it value is null

Include script field even if it value is null.

Closes #16408.
Jeff Evans 9 years ago
parent
commit
e9f2548ee0

+ 1 - 3
core/src/main/java/org/elasticsearch/search/fetch/script/ScriptFieldsFetchSubPhase.java

@@ -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 {

+ 19 - 0
modules/lang-groovy/src/test/java/org/elasticsearch/messy/tests/SearchFieldsTests.java

@@ -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();