Online API:
Https://www.elastic.co/guide/en/elasticsearch/client/java-api/2.4/transport-client.html
Tutorial:
Http://blog.java1234.com/blog/articles/345.html
Attention:
different versions of the ES API differ significantly, the introduction of the jar package version must be consistent with production. tools and methods of use can refer to the spare Parts system project: source See GitHub
Tools and methods of use can refer to the spare Parts system project: source See GitHub
To introduce a jar package:
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>2.4.0</version>
</dependency>
Write Test class:
Packagecom.sxt.es.test;Importjava.net.InetAddress;Importjava.util.Date;ImportOrg.elasticsearch.action.index.IndexResponse;Importorg.elasticsearch.client.transport.TransportClient;Importorg.elasticsearch.common.transport.InetSocketTransportAddress;Importorg.elasticsearch.common.xcontent.XContentFactory; Public classTestes {Private StaticString host= "192.168.136.131";//Server Address Private Static intport=9300;//Port Public Static voidMain (string[] args)throwsException {transportclient client=Transportclient.builder (). Build (). Addtransportaddress (Newinetsockettransportaddress (Inetaddress.getbyname (testes.host), testes.port)); Indexresponse Response=client.prepareindex ("Twitter", "tweet", "1"). SetSource (Xcontentfactory.jsonbuilder (). StartObject () . Field ("User", "Kimchy"). Field ("Postdate",NewDate ()). Field ("Message", "trying out Elasticsearch"). EndObject ()). get (); System.out.println ("Index Name:" +Response.getindex ()); System.out.println ("Type:" +Response.gettype ()); System.out.println ("Document ID:" +response.getid ());//first time use is 1Client.close (); }}
How do I operate Elasticsearch using Java? Simple example.