[Java] ES get Data part field value

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.