es访问地址:http://192.168.231.40:9200/
es安装地址:https://blog.csdn.net/zerokissingthefire/article/details/100104607
es集成地址:https://www.cnblogs.com/zhoutao825638/p/10382046.html
-----------------------配置类-----------------------------------------------------------------------------
import org.apache.log4j.Logger;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import java.net.InetAddress;
/**
-
@Author xuconghui
-
@Date 2020/10/13 16:38
-
@Version 1.0
**/
public class ElasticSearchFactory {
private static final Logger log = Logger.getLogger(ElasticSearchFactory.class);
private static final String ES_CLUTER_NAME = “dunhuang-application”;
private static final int ES_CONNECT_PORT = 9300;
private static TransportClient client=null;
private ElasticSearchFactory(){}private static final class ElasticSearchFactoryInner{
private static final ElasticSearchFactory elasticSearchFactory = new ElasticSearchFactory();
}public static ElasticSearchFactory get(){
return ElasticSearchFactoryInner.elasticSearchFactory;
}
//es连接配置
public TransportClient buildElasticSearchFactoryConfig() {
try {
//注意这里的端口是TCP端口9300,而非HTTP接口9200
InetSocketTransportAddress node = new InetSocketTransportAddress(InetAddress.getByName(“192.168.231.40”),ES_CONNECT_PORT);
//机器名称,可以首页查询,这个不能出现错误,否则无法连接ES
Settings settings=Settings.builder().put(“cluster.name”,ES_CLUTER_NAME).build();
//.addTransportAddress()可以配置多个地址
client = new PreBuiltTransportClient(settings).addTransportAddress(node);
return client;
} catch (Exception e) {
log.error(“ElasticSearchFactory.buildElasticSearchFactoryConfig es连接异常…”);
}
return null;
}
void closeElasticSearch(){
if(client!=null){
client.close();
}
}
}
--------------------------------业务类----------------------------------------------------------------------------------
import org.apache.log4j.Logger;
import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import java.io.IOException;
import java.util.List;
import java.util.Map;
/**
-
@Author xuconghui
-
@Date 2020/10/13 17:38
-
@Version 1.0 es增删改查工具类
**/
public class ElasticSearchUtil {
private static final Logger log = Logger.getLogger(ElasticSearchUtil.class);
private static final String ES_INDEX = “tao”;
private static final String ES_TYPE = “book”;/**
- 插入一条数据到es
*/
public static String addSingleToES(Map<String, Object> reqData,String idKey) {
if (null == reqData||StringUtil.isEmpty(idKey)) {
return “”;
}
IndexResponse indexResponse = ElasticSearchFactory.get().buildElasticSearchFactoryConfig().prepareIndex(ES_INDEX, ES_TYPE).setSource(getJsonReqData(reqData)).setId(String.valueOf(reqData.get(idKey))).get();
ElasticSearchFactory.get().closeElasticSearch();
return indexResponse.getId();
}
/**
- 批量插入多条数据
/
public static void addMoreToES(List<Map<String, Object>> reqdataList,String idKey) {
if(null==reqdataList||reqdataList.size()==0|| StringUtil.isEmpty(idKey)){
return;
}
TransportClient transportClient = ElasticSearchFactory.get().buildElasticSearchFactoryConfig();
BulkRequestBuilder bulkRequest = transportClient.prepareBulk();
for (Map<String, Object> map : reqdataList) {
bulkRequest.add(transportClient.prepareIndex(ES_INDEX, ES_TYPE).setSource(getJsonReqData(map)).setId(String.valueOf(map.get(idKey))));
}
bulkRequest.execute().actionGet();
ElasticSearchFactory.get().closeElasticSearch();
}
/* - 更新一条数据
- */
public static void updateDataById(Map<String, Object> reqData,String idKey){
}
private static XContentBuilder getJsonReqData(Map<String, Object> reqData) {
XContentBuilder xContentBuilder = null;
try {
xContentBuilder = XContentFactory.jsonBuilder().startObject();
for (String key : reqData.keySet()) {
xContentBuilder.field(key, reqData.get(key));
}
xContentBuilder.endObject();
} catch (IOException e) {
log.error(“ElasticSearchUtil.getJsonReqData reqData 转换json异常”);
}
return xContentBuilder;
}
} - 插入一条数据到es