Package Com.rodge.elasticSearch.firstDemo; Import java.net.InetAddress; Import java.net.UnknownHostException; Import Org.elasticsearch.action.get.GetResponse; Import org.elasticsearch.client.transport.TransportClient; Import org.elasticsearch.common.settings.Settings; Import org.elasticsearch.common.transport.InetSocketTransportAddress; Import org.elasticsearch.transport.client.PreBuiltTransportClient; Import Org.junit.Test; public class Esfirstdemo { @SuppressWarnings ("Unchecked") Public Transportclient Client () throws Unknownhostexception { ES address, the address is 192.168.253.129 (here the address cannot be added "/http"), where the port is ES TCP port is 9300, Instead of the previous http9200 port, if ES has more than one node, you can create multiple nodes and then add in the client Note that the port number here is not 9200, but 9300. The 9200 port is used to make HTTP REST API to access Elasticsearch, while Port 9300 is the default port on which the transport layer listens. inetsockettransportaddress node = new Inetsockettransportaddress ( Inetaddress.getbyname ("192.168.253.129"), 9300); The Configuration class for ES Settings Settings = Settings.builder (). Put ("Cluster.name", "Wali"). Build (); Transportclient is the core class of ES launcher, and the subsequent development of ES is centered around the transportclient. Transportclient client = new prebuilttransportclient (settings); Client.addtransportaddress (node); return client; } @Test public void Get () throws Unknownhostexception { GetResponse result = Client (). Prepareget ("book", "Novel", "1"). get (); System.out.println (Result.getsource (). toString ()); } } |