How to get partial field values in Es, by default, ES searches all fields of source, but sometimes we do not want to get all the field data, such as in development, we have dozens of fields in index, good dozens of g of data per day, all the return amount is too large.
There are two ways to get the data for a specified field,
1. Use the default method to find all the source data, and then filter the reorganization data according to the specified field.
2. Use AddField (fields) to request the data, and then use the GetFields method to obtain the result data.
The second way to get data is listed here.
//set Request Searchresponse sr;if (Fields != null) { Sr = client.preparesearch (Index). Settypes (Type). AddFields (Fields). Setquery (QueryBuilder). SetSize ( SizeLimit). Setexplain (True). get ();} Else{ sr = client.preparesearch (Index). Settypes (Type). Setquery (QueryBuilder). SetSize ( SizeLimit). Setexplain (True). get ();} Gets the Data way Jsonarray array = new jsonarray (); for (Searchhit hit:sr.gethits (). GetHits ()) { if (fields != null) {set<entry<string, searchhitfield>> Fieldentry = hit.getfields (). EntrySet (); jsonobject json = new jsonobject (); for (Entry<string, searchhitfield> entry:fieldentry) { json.put (Entry.getvalue (). GetName (), entry.getvalue (). GetValue ());} Array.add (JSON);} Else{ array.add (Hit.getsourceasstring ());}}
TIP: If we set the AddFields () method, but we do not use the Hit.getfields () method, and get the data directly using the Hit.getsource () or hit.getsourceasstring () methods, All the data will be null.
This article is from the "Do not forget Beginner's mind" blog, please be sure to keep this source http://zh9526.blog.51cto.com/3051209/1770531
[Java] ES get Data part field value