Return specified fields using spring data elastic search

I have a document User having fields like User(id, name, address, List ) etc. depending upon request we want to return either these three: id, name, address or all the fields, by default ElasticsearchRepository find** methods return all the fields, we kinda want to do something similar to projection , is there any way to achieve this in spring data? we do not want to use ElasticsearchRestTemplate and write our own custom queries.

You could do this using a NativeQueryBuilder and the ElasticsearchOperations class. Alternatively you could have a second repository class, that returns a user with less attributes.

Thanks. But for first option we still need to right our own queries right? We want to use ready-made methods provided by spring data, is there any other way to do this? and in option 2, are you proposing to make new document class with less fields?

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.