0

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"
        
    }]

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi @val, if I made changes like this then ElasticsearchRepository's findAll() is also getting affected, It is also returning first object as All null fields, I just want it to return all objects with non-null fields, it's expected behavior, do i need to make any changes? I have posted separate question for it, would you be kind enough to answer it stackoverflow.com/questions/62771342/…
Hi @val, I just wanted to know whether this custom field filtering method is interfering with implementation of ElasticsearchRepository's findAll() method, as requested in previous comment. Any suggestions will be really helpful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.