SQL to Elasticsearch Java code

Source: Internet
Author: User

Use Elasticsearch as database, because Elasticsearch does not support SQL, you need to convert SQL into code implementation.

1. Query count by a field group by

SELECTCOUNT (fielda) from table WHERE =        and =and >  <  - Group  by Fielda;

The corresponding Java code:

Searchrequestbuilder searchreq = Client.preparesearch ("Sample_index"); Searchreq.settypes ("Sample_types"); Termsbuilder TERMSB= Aggregationbuilders.terms ("My_fielda"). Field ("Fielda"). Size (100); Boolfilterbuilder BF=Filterbuilders.boolfilter (); Termfilterbuilder TF_FIELDC= Filterbuilders.termfilter ("Fieldc", "Hoge"); Termfilterbuilder tf_fieldd= Filterbuilders.termfilter ("Fieldd", "Huga"); Bf.must (TF_FIELDC); Bf.must (TF_FIELDD); Rangefilterbuilder Rangefieldbfilter= Filterbuilders.rangefilter ("Fieldb"). GT (10). LT (100); Searchreq.setquery (Querybuilders.filteredquery (Querybuilders.matchallquery (), FILTERBUILDERS.ANDF Ilter (BF, Rangefieldbfilter)). Addaggregation (TERMSB); SearchResponse Searchres=Searchreq.execute (). Actionget (); Terms fieldaterms= Searchres.getaggregations (). Get ("My_fielda"); for(Terms.bucket filedABucket:fieldATerms.getBuckets ()) {//FieldaString Fieldavalue =Filedabucket.getkey (); //COUNT (Fielda)    LongFieldacount =filedabucket.getdoccount ();}

2. Press a field and date group by and query the sum, time chart, time interval is 1 days.

SELECTSUM fromtableGroup by      DATE (CREATE_AT), Fielda;

The corresponding Java code:

Searchrequestbuilder searchreq = Client.preparesearch ("Sample_index"); Searchreq.settypes ("Sample_types");D Atehistogrambuilder DHB= Aggregationbuilders.datehistogram ("My_datehistogram"). Field ("Create_at"). Interval (DateHistogram.Interval.days (1)); Termsbuilder Termsb_fa= Aggregationbuilders.terms ("My_fielda"). Field ("Fielda"). Size (100); Termsb_fa.subaggregation (Aggregationbuilders.sum ("My_sum_fieldb"). Field ("Fieldb")) );d hb.subaggregation (TERMSB_FA) searchreq.setquery (Querybuilders.matchallquery ()). Addaggregation (DHB); SearchResponse Searchres=Searchreq.execute (). Actionget ();D atehistogram datehist= Searchres.getaggregations (). Get ("My_datehistogram"); for(Datehistogram.bucket dateBucket:dateHist.getBuckets ()) {//DATE (CREATE_AT)String Create_at =Dateentry.getkey (); Terms fieldaterms= Datebucket.getaggregations (). Get ("My_fielda");  for(Terms.bucket filedABucket:fieldATerms.getBuckets ()) {//FieldaString Fieldavalue =Filedabucket.getkey (); //SUM (FIELDB)Sum Sumagg = Filedabucket.getaggregations (). Get ("My_sum_fieldb"); LongSumfieldb = (Long) sumagg.getvalues (); }}

3. Press two field group by and query sum

SELECTSUM (fieldb) from table group       by Fielda, FIELDC;

The corresponding Java code:

Searchrequestbuilder searchreq = Client.preparesearch ("Sample_index"); Searchreq.settypes ("Sample_types"); Termsbuilder Termsb_fa= Aggregationbuilders.terms ("My_fielda"). Field ("Fielda"). Size (100); Termsbuilder Termsb_fc= Aggregationbuilders.terms ("MY_FIELDC"). Field ("FIELDC"). Size (50); Termsb_fc.subaggregation (Aggregationbuilders.sum ("My_sum_fieldb"). Field ("Fieldb")) ); Termsb_fa.subaggregation (TERMSB_FC) searchreq.setquery (Querybuilders.matchallquery ()). AddAggregation ( TERMSB_FA); SearchResponse Searchres=Searchreq.execute (). Actionget (); Terms fieldaterms= Searchres.getaggregations (). Get ("My_fielda"); for(Terms.bucket filedABucket:fieldATerms.getBuckets ()) {//FieldaString Fieldavalue =Filedabucket.getkey (); Terms fieldcterms= Filedabucket.getaggregations (). Get ("MY_FIELDC");  for(Terms.bucket filedCBucket:fieldCTerms.getBuckets ()) {//FIELDCString Fieldcvalue =Filedcbucket.getkey (); //SUM (FIELDB)Sum Sumagg = Filedcbucket.getaggregations (). Get ("My_sum_fieldb"); LongSumfieldb = (Long) sumagg.getvalues (); }}

4. Press a filed group by and query the count sum and average

SELECT COUNT SUM AVG from table     Group by Fielda;

The corresponding Java code:

Searchrequestbuilder searchreq = Client.preparesearch ("Sample_index"); Searchreq.settypes ("Sample_types"); Termsbuilder TERMSB= Aggregationbuilders.terms ("My_fielda"). Field ("Fielda"). Size (100); Termsb.subaggregation (Aggregationbuilders.sum ("My_sum_fieldb"). Field ("Fieldb"))); Termsb.subaggregation (Aggregationbuilders.avg ("My_avg_fieldb"). Field ("Fieldb")) ; Searchreq.setquery (Querybuilders.matchallquery ()). Addaggregation (TERMSB); SearchResponse Searchres=Searchreq.execute (). Actionget (); Terms fieldaterms= Searchres.getaggregations (). Get ("My_fielda"); for(Terms.bucket filedABucket:fieldATerms.getBuckets ()) {//FieldaString Fieldavalue =Filedabucket.getkey (); //COUNT (Fielda)    LongFieldacount =Filedabucket.getdoccount (); //SUM (FIELDB)Sum Sumagg = Filedabucket.getaggregations (). Get ("My_sum_fieldb"); LongSumfieldb = (Long) sumagg.getvalues (); //AVG (FIELDB)AVG Avgagg = Filedabucket.getaggregations (). Get ("My_avg_fieldb"); DoubleAvgfieldb =avgagg.getvalues ();}

SQL to Elasticsearch Java code

Related Article

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.