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