ElasticSearch java API--创建Client连接

来源:互联网 发布:代工 淘宝店 编辑:程序博客网 时间:2024/04/27 03:55
package Client;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
public class ServerClient {

public Client getClient(){
Settings settings = ImmutableSettings.settingsBuilder().put("client.transport.sniff",true)   
            .put("cluster.name", "李安安").build();//clienttransport.sniff:true 嗅探整个集群;cluster.name:xx 集群名称,对应于/config/elasticsearch.yml 内部配置。
Client client=new TransportClient(settings)
.addTransportAddress(new InetSocketTransportAddress("127.0.0.1",9300));
//.addTransportAddress(new InetSocketTransportAddress("192.168.5.1",9300))  多个连接
return  client;
}

}
原创粉丝点击