Elasticsearch5.0 Java Api (iv)--retrieving Indexes

Source: Internet
Author: User

Test the ability to retrieve the index library

1  packagecom.juyun.test;2 3 Importjava.net.InetAddress;4 5 Importorg.elasticsearch.client.Client;6 Importorg.elasticsearch.common.settings.Settings;7 Importorg.elasticsearch.common.transport.InetSocketTransportAddress;8 Importorg.elasticsearch.index.query.QueryBuilder;9 Importorg.elasticsearch.index.query.QueryBuilders;Ten Importorg.elasticsearch.search.SearchHit; one Importorg.elasticsearch.search.SearchHits; a Importorg.elasticsearch.transport.client.PreBuiltTransportClient; - Importorg.elasticsearch.action.get.GetResponse; - Importorg.elasticsearch.action.get.MultiGetItemResponse; the Importorg.elasticsearch.action.get.MultiGetResponse; - Importorg.elasticsearch.action.search.SearchResponse; -  -  public classElasticsearchget { +  -     Private StaticClient client; +      a     /** at * Test the function of retrieving index library -      * @paramargs -      */ -      public Static voidmain (string[] Args) { -  -         Try { in             //set the cluster name -Settings Settings = Settings.builder (). put ("cluster.name", "elasticsearch"). Build (); to             //Create client +Client =Newprebuilttransportclient (settings) -. addtransportaddress (NewInetsockettransportaddress (inetaddress.getbyname ("192.168.10.125"), 9300)); the              *              $             //multiget ("flow", "data", "avvne1sa3hhivpsln8ln");Panax NotoginsengGetAll ("newindex", "newhttp"); -              the              +              a             //Close Client the client.close (); +  -}Catch(Exception E) { $ e.printstacktrace (); $         } -     } -      the     //get a document with a single ID -      public Static voidget (String indexname,string typename,string id) {WuyiMultigetresponse multigetitemresponses =Client.preparemultiget () the . Add (indexname, TypeName, id) - . Get (); wu  -              for(multigetitemresponse itemresponse:multigetitemresponses) {//traversing result sets aboutGetResponse response =Itemresponse.getresponse (); $                 if(response.isexists ()) {//Verify that the document exists -String json = response.getsourceasstring ();//Get document Source - System.out.println (json); -                 } a             } +     } the      -     //multiple IDs are passed in to get multiple documents from the same index Name/type name $      public Static voidmultiget (String indexname,string typename,string id1,string id2,string id3) { theMultigetresponse multigetitemresponses =Client.preparemultiget () the. Add (indexname, TypeName, id1, id2, id3)//multiple IDs are passed in to get multiple documents from the same index Name/type name the. Add ("another", "type", "foo")//you can get documents from different indexes at the same time the . Get (); -  in              for(multigetitemresponse itemresponse:multigetitemresponses) {//traversing result sets theGetResponse response =Itemresponse.getresponse (); the                 if(response.isexists ()) {//Verify that the document exists aboutString json = response.getsourceasstring ();//Get document Source the System.out.println (json); the                 } the             } +     } -      the     //get documents from different indexes at the same timeBayi      public Static voidmultiget (String indexname1,string indexname2,string indexName3) { theMultigetresponse multigetitemresponses =Client.preparemultiget () the. Add (indexName1, Indexname2,indexname3)//you can get documents from different indexes at the same time - . Get (); -  the              for(multigetitemresponse itemresponse:multigetitemresponses) {//traversing result sets theGetResponse response =Itemresponse.getresponse (); the                 if(response.isexists ()) {//Verify that the document exists theString json = response.getsourceasstring ();//Get document Source - System.out.println (json); the                 } the             } the     }94      the     //querying all data by index and category the      public Static voidgetAll (String indexname,string TypeName) { the         98QueryBuilder QueryBuilder =querybuilders.matchallquery (); aboutSearchResponse SearchResponse =client.preparesearch (indexname). settypes (typeName) - . Setquery (querybuilder)101 . Execute ()102 . Actionget ();103Searchhits hits = Searchresponse.gethits ();//Get search Results104System.out.println ("number of queries to records:" +hits.gettotalhits ()); thesearchhit[] searchhists =hits.gethits ();106         if(searchhists.length>0){107              for(searchhit hit:searchhists) {108 System.out.println (hit.getsource ());109                 //System.out.println (hit.id ()); the             }111         } the     }113      the      the      the     //Todo ...117     //field and content query (word breaker not added)118      public Static voidget () {119          -         //Create querybuilder, You can set a query for a single field, or you can set a query for multiple fields121QueryBuilder QB1 = querybuilders.termquery ("time", "2017-05-08t02:50:47.700z");//querying a document with 2 keywords in the Time field122QueryBuilder qb2= querybuilders.multimatchquery ("2", "time", "inbyte");//query a document that contains 2 keywords in the time or Inbyte field123 124         //executes the query by setting the index, type, and query of the queries through the Client. returns a SearchResponse object theSearchResponse response = Client.preparesearch ("flow"). settypes ("data")). setquery (qb1). execute ()126 . Actionget ();127         //working with query results to get a collection of query results -Searchhits hits =response.gethits ();129         if(hits.totalhits () > 0) { the              for(searchhit hit:hits) {131System.out.println ("score:" +hit.getscore () + ": \ t" +hit.getsource ());//. Get ("title") the             }133}Else {134System.out.println ("found 0 results");135         }136     }137 138}
Elasticsearchget

Elasticsearch5.0 Java Api (iv)--retrieving Indexes

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.