Elasticsearch-java Client Connections

Source: Internet
Author: User
Tags server port

Write in front of the words: reading broken million, coded as God
--------------------------------------------------------------------

The simplest connection to the ES server in a Java client

(Demo only for quick and easy use)

1  PackageCom.es.util.elasticsearch;2 3 ImportLombok. Data;4 ImportOrg.apache.log4j.Logger;5 ImportOrg.elasticsearch.action.search.SearchRequestBuilder;6 ImportOrg.elasticsearch.action.search.SearchResponse;7 Importorg.elasticsearch.client.transport.TransportClient;8 Importorg.elasticsearch.common.settings.Settings;9 Importorg.elasticsearch.common.transport.InetSocketTransportAddress;Ten ImportOrg.elasticsearch.common.unit.TimeValue; One Importorg.elasticsearch.transport.client.PreBuiltTransportClient; A Importorg.springframework.stereotype.Component; -  - Importjavax.annotation.PostConstruct; the Importjava.net.InetAddress; - Importjava.net.UnknownHostException; -  - /** +  * @author: Huobaopaocai - * @date: 2018/2/15-10:31 + * @Description: Elasticsearch client A  */ at@Component ("Elasticsearchconstant") - @Data -  Public classElasticsearchconstant { -  -      Public Static FinalLogger LOG = Logger.getlogger (elasticsearchconstant.class); -  in     /** - * ES server address to      */ +     PrivateString host = "127.0.0.1"; -  the     /** * * ES server port $      */Panax Notoginseng     PrivateInteger Port = 9300; -  the     /** + * ES cluster name A      */ the     PrivateString clustername = "Huobaopaocai-es-cluster"; +  -     /** $ * ES index name $      */ -     PrivateString Esindex = "ecommerce"; -  the     /** - * es index under type nameWuyi      */ the     PrivateString Estype = "Product"; -  Wu      PublicTransportclient client =NULL; -  About     PrivateSearchrequestbuilder Requestbuilder; $  -     PrivateLong time_out = 20L; -  - @PostConstruct A      Public voidinit () { +Settings Settings =Settings.builder () the. put ("Cluster.name", clustername) -. put ("Client.transport.sniff",true) $. put ("Client.transport.ping_timeout", "600s") the . Build (); the         Try { theClient =Newprebuilttransportclient (Settings) the. addtransportaddress (Newinetsockettransportaddress (Inetaddress.getbyname (host), Port)); -}Catch(unknownhostexception e) { in Log.error (E, E.getcause ()); the         } the     } About  the     /** the * Initialize Searchrequestbuilder the      * @return +      */ -      PublicSearchrequestbuilder Initrequestbuilder () { theRequestbuilder =Client.preparesearch (Esindex). Settypes (Estype);Bayi         returnRequestbuilder; the     } the  -     /** - * Perform ES operations the      * @paramRequestbuilder the      * @return the      */ the      PublicSearchResponse Execute (searchrequestbuilder requestbuilder) { -SearchResponse SearchResponse =requestbuilder.settimeout (Timevalue.timevalueseconds (time_out)). Execute (). Actionget (); the         returnSearchResponse; the     } the}

--------------------------------------------------------------------

Elasticsearch-java Client Connections

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.