get one field from elasticsearch by spring data -
i have es document this
class user { string name; string describe; list<string> items; }
i'm using spring data talk es repository interface
interface userrepository extends repository<user, string> { }
now need build rest interface responses json-format data this
{"name": string, "firstitem": string}
because describe
, items
in user
big, it's expensive retrieve field es.
i know es have feature named "response filtering" can fit requirement, don't find way using in spring data.
how in spring data?
what need mix of source filtering (for not retrieving heavy fields) , response filtering (for not returning heavy fields). however, latter not supported in spring data es (yet)
for former, can leverage nativesearchquerybuilder
, specify fetchsourcefilter
retrieve fields need. latter not supported yet in spring data es. can create field named firstitem
in you'd store first element of items
can return query.
private elasticsearchtemplate elasticsearchtemplate; string[] includes = new string[]{"name", "firstitem"}; searchquery searchquery = new nativesearchquerybuilder() .withquery(matchallquery()) .withsourcefilter(new fetchsourcebuilder(includes, null)) .build(); page<user> userpage = elasticsearchtemplate.queryforpage(searchquery, user.class);
Comments
Post a Comment