Search code examples
spring-bootelasticsearchspring-dataspring-data-elasticsearch

Get all non-full fields in Spring Data Elasticsearch Query using source filtering


We are using native search query builder as follows:

 String[] includeFields = new String[]{"idDl", "clientName"};
 String[] excludeFields = new String[]{"Address"};
 Query searchQuery = new NativeSearchQueryBuilder()
                .withQuery(matchAllQuery())
                .withSourceFilter(new FetchSourceFilter(includeFields, excludeFields))
                 .build();
return elasticsearchRestTemplate.queryForObject((StringQuery) searchQuery, User.class);

I am able to get all the response data but first response object is all null fields, i want to exclude all null fields object in the final response. We are using we're using spring-data-elasticsearch 3.2.6.RELEASE and here is the sample response:

   [{
        "idDl": null,
        "clientName": null,
        "Address": null
    },
    {
        "idDl": 19810008,
        "clientName": "ABC",
        "Address": "NYC"
        
    }]

Solution

  • What you should do is to create a query that will exclude the documents with those fields having null values instead of doing a match_all. Change your query with this instead:

                ...
                .withQuery(existsQuery("idDl"))
                ...
    

    Also worth noting that source filtering alone will not look at the values of your fields to check if they are null or not, it simply returns the field by name if it's present in the source document.