1. Elasticsearch provides two built-in client 1.1 node clients for Java users: The node client joins the cluster with no data node (none), in other words, it does not store any data itself. But it knows exactly where the data is in the cluster and is able to forward requests directly to the corresponding nodes. Transport clients (Transport client): This lighter-weight transport client is able to send requests to a remote cluster. It does not join the cluster itself, but simply forwards the request to the nodes in the cluster. All two Java clients interact with the cluster via port 9300, using the Elasticsearch Transport protocol (Elasticsearch Transport Protocol). The nodes in the cluster also communicate through 9300 ports. If this port is not open, your node will not be able to form a cluster. The Elasticsearch version of the +java client must be the same as the other nodes in the cluster, otherwise they may not be recognized by each other.
1.2 Connecting clusters via Transportclient (recommended)
Public voidBefore11 ()throwsException {//Create the client, using the default cluster name, "ElasticSearch"//client = Transportclient.builder (). Build (). Addtransportaddress (New Inetsockettransportaddress (Inetaddress.getbyname ("www.wenbronk.com"), 9300)); //specifying cluster configuration information through the setting object, configuring the cluster nameSettings Settings = Settings.settingsbuilder (). Put ("Cluster.name", "Elasticsearch_wenbronk")//set the cluster name//. Put ("Client.transport.sniff", True)//Open sniffer, after opening will not connect, reason unknown//. Put ("Network.host", "192.168.50.37"). put ("Client.transport.ignore_cluster_name",true)//ignore the cluster name verification, open the cluster name is not able to connect//. Put ("Client.transport.nodes_sampler_interval", 5)//Error,//. Put ("Client.transport.ping_timeout", 5)//error, ping wait time,. Build (); Client=Transportclient.builder (). settings (Settings). Build (). Addtransportaddress (NewInetsockettransportaddress (NewInetsocketaddress ("192.168.50.37", 9300))); //Default 5s//how long to open the connection, default 5sSYSTEM.OUT.PRINTLN ("Success Connect"); }
2. Using QueryBuilder for querying
Using QueryBuilder
2.1 Termquery ("key", obj) exactly matches
2.1 Termsquery ("Key", Obj1, obj2 ...) matches multiple values at once
2.3 Matchquery ("key", OBJ) single match, field does not support wildcard characters, prefix with advanced features
2.4 Multimatchquery ("text", "Field1", "Field2"); Match multiple fields, field has wildcard characters.
2.5 matchallquery (); Match All Files
@Test Public voidTestquerybuilder () {//QueryBuilder QueryBuilder = querybuilders.termquery ("User", "Kimchy");QueryBuilder QueryBuilder = querybuilders.termquery ("User", "Kimchy", "Wenbronk", "Vini"); Querybuilders.termsquery ("User",NewArraylist<string> (). Add ("Kimchy"));//QueryBuilder QueryBuilder = querybuilders.matchquery ("User", "Kimchy");//QueryBuilder QueryBuilder = querybuilders.multimatchquery ("Kimchy", "User", "message", "gender");QueryBuilder QueryBuilder =Querybuilders.matchallquery (); Searchfunction (QueryBuilder); }
2.6 Query Traversal
Private voidsearchfunction (QueryBuilder querybuilder) {searchresponse response= Client.preparesearch ("Twitter"). Setsearchtype (Searchtype.dfs_query_then_fetch). Setscroll (NewTimeValue (60000) . Setquery (QueryBuilder). SetSize (100). Execute (). Actionget (); while(true) {Response=Client.preparesearchscroll (Response.getscrollid ()). Setscroll (NewTimeValue (60000) . Execute (). Actionget (); for(Searchhit hit:response.getHits ()) {Iterator<entry<string, object>> iterator =Hit.getsource (). EntrySet (). iterator (); while(Iterator.hasnext ()) {Entry<string, object> next =Iterator.next (); System.out.println (Next.getkey ()+ ": " +Next.getvalue ()); if(Response.gethits (). hits (). length = = 0) { Break; } } } Break; }//Testresponse (response);}
2.7 Combination Query
Must (Querybuilders): and
Mustnot (querybuilders): not
Should:: OR
@Test Public void TestQueryBuilder2 () { = querybuilders.boolquery () . Must (Querybuilders.termquery (" User "," Kimchy ")) . Mustnot (Querybuilders.termquery (" message "," Nihao ")). should ( Querybuilders.termquery ("gender", "Male")); Searchfunction (QueryBuilder); }
Elasticsearch Learning Java Operations 1