Search code examples
spring-data-elasticsearch

how to ignore null field when deserializing in spring data elasticsearch


I have saved two objects.

@Document(indexName = "test_index", createIndex = false)
public class ZTest {

    @Id
    private String id;

    private String testOne;

    @Transient
    private String testTwo;

    private String testThree;
}

      {
        "_index" : "test_index",
        "_type" : "_doc",
        "_id" : "w0mCgYEBXSVfca67-ayl",
        "_score" : 1.0,
        "_source" : {
          "_class" : "com.ZTest",
          "testOne" : "test_one"
        }
      },
      {
        "_index" : "test_index",
        "_type" : "_doc",
        "_id" : "xEmLgYEBXSVfca67r6wf",
        "_score" : 1.0,
        "_source" : {
          "_class" : "com.ZTest",
          "testOne" : "test_one",
          "testThree" : "test_three"
        }
      }

    Iterable<ZTest> t = zTestRepository.findAll();

Is it possible to exclude those null fields in the read result?

null_fields

I have tried setting this spring.jackson.default-property-inclusion=non_null in property files. It doesn't work.


Solution

  • Spring Data Elasticsearch returns entities, and these Java POJO entities have 3 defined properties. And for properties that are transient or that are not returned by Elasticsearch the property value will be the value that is set in the object when on object of the entity class is created - here it's null.

    What else do you expect to be returned?