How do I operate Elasticsearch using Java? Simple example.

Source: Internet
Author: User

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.

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.