Elasticsearch5.0 Java Api (eight)--common DSL query

Source: Internet
Author: User

Test the common DSL combination query function

1  Packagecom.juyun.test;2 3 Importjava.net.InetAddress;4 5 ImportOrg.elasticsearch.action.search.SearchResponse;6 Importorg.elasticsearch.client.Client;7 Importorg.elasticsearch.common.settings.Settings;8 Importorg.elasticsearch.common.transport.InetSocketTransportAddress;9 ImportOrg.elasticsearch.index.query.QueryBuilder;Ten Importorg.elasticsearch.index.query.QueryBuilders; One ImportOrg.elasticsearch.search.SearchHit; A Importorg.elasticsearch.search.SearchHits; - ImportOrg.elasticsearch.search.aggregations.AggregationBuilder; - Importorg.elasticsearch.search.aggregations.AggregationBuilders; the ImportOrg.elasticsearch.search.aggregations.bucket.histogram.DateHistogramInterval; - ImportOrg.elasticsearch.search.aggregations.metrics.sum.InternalSum; - ImportOrg.elasticsearch.search.sort.SortBuilder; - Importorg.elasticsearch.search.sort.SortBuilders; + ImportOrg.elasticsearch.search.sort.SortOrder; - Importorg.elasticsearch.transport.client.PreBuiltTransportClient; + ImportOrg.joda.time.DateTimeZone; A  at  Public classDslquery { -  -     Private Staticclient client; -      -     //several DSLs used in testing -      Public Static voidMain (string[] args) { in          -         Try { to             //set the cluster name +Settings Settings = Settings.builder (). Put ("Cluster.name", "Elasticsearch"). Build (); -             //Create client theClient =Newprebuilttransportclient (Settings) *. addtransportaddress (NewInetsockettransportaddress (Inetaddress.getbyname ("172.16.0.157"), 9300)); $             Panax Notoginseng             LongStarttime=system.currenttimemillis ();//Get start time -              theCommonquery ("NewIndex", "Newhttp"); +             //CommonQuery2 ("NewIndex", "newhttp"); A              the             LongEndtime=system.currenttimemillis ();//Get end Time +SYSTEM.OUT.PRINTLN ("program Run Time:" + (Endtime-starttime) + "MS"); -              $              $             //Close Client - client.close (); -  the}Catch(Exception e) { - e.printstacktrace ();Wuyi         } the     } -  Wu     /** - * Sort and query all data in descending order of Timestamp About      * @paramIndexName $      * @paramTypeName -      */ -      Public Static voidcommonquery (String indexname, String typeName) { -          ASortbuilder Sortbuilder = Sortbuilders.fieldsort ("@timestamp") +. Order (SORTORDER.DESC). Unmappedtype ("boolean");//Defining sorting Methods theQueryBuilder QueryBuilder =querybuilders.boolquery () -. Must (Querybuilders.querystringquery ("*"). Analyzewildcard (true). Defaultfield ("_all"));//Defining Query Methods $                  theSearchResponse SearchResponse =Client.preparesearch (IndexName). Settypes (TypeName) the . Setquery (QueryBuilder) the . AddSort (Sortbuilder) the. SetSize (500) - . Execute (). Actionget (); in          theSearchhits hits = Searchresponse.gethits ();//Get search Results the          for(Searchhit searchhit:hits) {//Traverse and Print search results as a string About System.out.println (searchhit.getsourceasstring ()); the         } theSystem.out.println ("Number of Queries to records:" +hits.gettotalhits ()); the     } +      -      the     /**Bayi * Aggregate query statistics total the      * @paramIndexName the      * @paramTypeName -      */ -      Public Static voidCommonQuery2 (String indexname, String typeName) { the          theSortbuilder Sortbuilder = Sortbuilders.fieldsort ("@timestamp") the. Order (SORTORDER.DESC). Unmappedtype ("boolean");//Defining sorting Methods theQueryBuilder QueryBuilder =querybuilders.boolquery () -. Must (Querybuilders.querystringquery ("*"). Analyzewildcard (true). Defaultfield ("_all"));//Defining Query Methods the          theAggregationbuilder Aggbuilder1=aggregationbuilders.datehistogram ("Dateagg")//define time interval histogram the. Field ("@timestamp")94. Datehistograminterval (Datehistograminterval.minutes (1)) the. TimeZone (Datetimezone.forid ("+08:00")); the          theAggregationbuilder aggbuilder2=aggregationbuilders.sum ("Sumofout"). Field ("Http.bytes_out");//Aggregated Statistics98Aggregationbuilder aggbuilder3=aggregationbuilders.sum ("Sumofin"). Field ("Http.bytes_in"); About          -SearchResponse SearchResponse =Client.preparesearch (IndexName). Settypes (TypeName)101 . Setquery (QueryBuilder)102 . AddSort (Sortbuilder)103 . Addaggregation (aggBuilder1)104 . Addaggregation (aggBuilder2) the . Addaggregation (AggBuilder3)106. SetSize (500)107 . Execute (). Actionget ();108         109Searchhits hits = Searchresponse.gethits ();//Get search Results the          for(Searchhit searchhit:hits) {//Traverse and Print search results as a string111 System.out.println (searchhit.getsourceasstring ()); the         }113Internalsum sumofout = Searchresponse.getaggregations (). Get ("Sumofout");//get the total value of the traffic theInternalsum Sumofin = Searchresponse.getaggregations (). Get ("Sumofin");//get total incoming traffic the          theSystem.out.println ("Number of Queries to records:" +hits.gettotalhits ());117System.out.println ("Outbyte's total value is:" +Sumofout.getvalue ());118System.out.println ("Inbyte's total value is:" +Sumofin.getvalue ());119     } -}
Dslquery

Elasticsearch5.0 Java Api (eight)--common DSL query

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.