Elasticsearch 与Springboot 的简单连接

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_35893120/article/details/83987316

1、主要Elasticsearch 包:

2、application.properties 配置

 

3、 Elasticsearch  java 代码

package com.allen.elasticsearch;

import java.net.InetAddress;

import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

@Configuration
@Component
public class ESconfig {
	
	private Logger logger = LoggerFactory.getLogger(ESconfig.class);
	
	@Value("${elasticsearch.ip}")
	private String hostName;
	
	/**
     * 端口
     */
    @Value("${elasticsearch.port}")
    private String port;
    /**
     * 集群名称
     */
    @Value("${elasticsearch.cluster.name}")
    private String clusterName;

    /**
     * 连接池
     */
    @Value("${elasticsearch.pool}")
    private String poolSize;

    public TransportClient init() {
    	logger.info("初始化开始。。。。。");
        TransportClient transportClient = null;
        try {
            // 配置信息
            Settings esSetting = Settings.builder()
                    .put("client.transport.sniff", true)//增加嗅探机制,找到ES集群
                    .put("thread_pool.search.size", Integer.parseInt(poolSize))//增加线程池个数,暂时设为20
                    .put("cluster.name",clusterName)
                    .build();
            //配置信息Settings自定义,下面设置为EMPTY
            transportClient = new PreBuiltTransportClient(esSetting);
            TransportAddress transportAddress = new TransportAddress(InetAddress.getByName(hostName), Integer.valueOf(port));
            transportClient.addTransportAddresses(transportAddress);


        } catch (Exception e) {
        	e.printStackTrace();
        	logger.error("elasticsearch TransportClient create error!!!", e);
        }

        return transportClient;
    }

}
package com.allen.elasticsearch;

import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/es")
public class ESController {
		
	@Autowired
	private ESconfig esconfig;
	
	
	
	@RequestMapping("/get")
	public void get() throws Exception{
		String index = "chat";
		String type ="msg";
		String id="3";
		
		TransportClient client = esconfig.init();
		GetResponse  result = client.prepareGet(index,type,id).get();
		System.out.println(result.getSourceAsString());
		System.out.println(result.getType());
		System.out.println(result.getVersion());
		System.err.println(result.getIndex());
		System.err.println(result.getId());
		
		client.close();
	}
	
}

猜你喜欢

转载自blog.csdn.net/qq_35893120/article/details/83987316