其实这种博客网上一大片,为啥还要写出来这篇博客?
网上的例子都是基于elasticsearch2.x版本的,并不是5.x版本,而且还有好多是错的,拿过来根本不能直接用来测试,还有就是spring-data没有对应的5.x版本,出于对方面考虑,所以就用spring boot 2.x来做一个demo,分享出来。如果有错误,欢迎指出。
具体的代码网址githup:https://github.com/growup818/springboot-es-search
实战:
ES数据配置类
package org.githup.es.config;
import java.net.InetAddress;
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 org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
/**
* 数据配置,进行初始化操作
*
* @author sdc
*
*/
@Configuration
public class ESConfiguration implements FactoryBean<TransportClient>, InitializingBean, DisposableBean {
private static final Logger logger = LoggerFactory.getLogger(ESConfiguration.class);
/**
* es集群地址
*/
@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;
private TransportClient client;
@Override
public void destroy() throws Exception {
try {
logger.info("Closing elasticSearch client");
if (client != null) {
client.close();
}
} catch (final Exception e) {
logger.error("Error closing ElasticSearch client: ", e);
}
}
@Override
public TransportClient getObject() throws Exception {
return client;
}
@Override
public Class<TransportClient> getObjectType() {
return TransportClient.class;
}
@Override
public boolean isSingleton() {
return false;
}
@Override
public void afterPropertiesSet() throws Exception {
try {
// 配置信息
Settings esSetting = Settings.builder().put("cluster.name", clusterName).put("client.transport.sniff", true)// 增加嗅探机制,找到ES集群
.put("thread_pool.search.size", Integer.parseInt(poolSize))// 增加线程池个数,暂时设为5
.build();
client = new PreBuiltTransportClient(esSetting);
InetSocketTransportAddress inetSocketTransportAddress = new InetSocketTransportAddress(InetAddress.getByName(hostName), Integer.valueOf(port));
client.addTransportAddresses(inetSocketTransportAddress);
} catch (Exception e) {
logger.error("elasticsearch TransportClient create error!!!", e);
}
}
}
dao层,数据层,增删改查进行简单数据封装
package org.githup.es.dao;
import java.util.Map;
import java.util.UUID;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse;
import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequest;
import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequestBuilder;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.alibaba.fastjson.JSONObject;
/**
* ES的操作数据类
*
* 备注:对es的一些操作做了一些封装,抽出来一些操作,就是传统的dao层,数据服务
*
* @author sdc
*
*/
@Component
public class ESRepository {
private static final Logger log = LoggerFactory.getLogger(ESRepository.class);
@Autowired
private TransportClient client;
/**
* 创建索引
*
* @param index
* @return
*/
public boolean buildIndex(String index) {
if (!isIndexExist(index)) {
log.info("Index is not exits!");
}
CreateIndexResponse buildIndexresponse = client.admin().indices().prepareCreate(index).execute().actionGet();
log.info(" 创建索引的标志: " + buildIndexresponse.isAcknowledged());
return buildIndexresponse.isAcknowledged();
}
/**
* 删除索引
*
* @param index
* @return
*/
public boolean deleteIndex(String index) {
if (!isIndexExist(index)) {
log.info(" 索引不存在 !!!!!!");
}
DeleteIndexResponse diResponse = client.admin().indices().prepareDelete(index).execute().actionGet();
if (diResponse.isAcknowledged()) {
log.info("删除索引**成功** index->>>>>>>" + index);
} else {
log.info("删除索引**失败** index->>>>> " + index);
}
return diResponse.isAcknowledged();
}
/**
* 查询数据
* @param index 索引<----->关系型数据库
* @param type 类型<----->关系型数据表
* @param id 数据ID<----->id
* @return
*/
public Map<String, Object> searchDataByParam(String index, String type, String id) {
if(index == null || type == null || id == null) {
log.info(" 无法查询数据,缺唯一值!!!!!!! ");
return null;
}
//来获取查询数据信息
GetRequestBuilder getRequestBuilder = client.prepareGet(index, type, id);
GetResponse getResponse = getRequestBuilder.execute().actionGet();
//这里也有指定的时间获取返回值的信息,如有特殊需求可以
return getResponse.getSource();
}
/**
* 更新数据
*
* @param data 添加的数据类型 json格式的
* @param index 索引<----->关系型数据库
* @param type 类型<----->关系型数据表
* @param id 数据ID<----->id
* @return
*/
public void updateDataById(JSONObject data, String index, String type, String id) {
if(index == null || type == null || id == null) {
log.info(" 无法更新数据,缺唯一值!!!!!!! ");
return;
}
//更新步骤
UpdateRequest up = new UpdateRequest();
up.index(index).type(type).id(id).doc(data);
//获取响应信息
//.actionGet(timeoutMillis),也可以用这个方法,当过了一定的时间还没得到返回值的时候,就自动返回。
UpdateResponse response = client.update(up).actionGet();
log.info("更新数据状态信息,status{}", response.status().getStatus());
}
/**
* 添加数据
*
* @param data 添加的数据类型 json格式的
* @param index 索引<----->关系型数据库
* @param type 类型<----->关系型数据表
* @param id 数据ID<----->id
* @return
*/
public String addTargetDataALL(JSONObject data, String index, String type, String id) {
//判断一下次id是否为空,为空的话就设置一个id
if(id == null) {
id = UUID.randomUUID().toString();
}
//正式添加数据进去
IndexResponse response = client.prepareIndex(index, type, id).setSource(data).get();
log.info("addTargetDataALL 添加数据的状态:{}", response.status().getStatus());
return response.getId();
}
/**
* 通过ID删除数据
*
* @param index 索引,类似数据库
* @param type 类型,类似表
* @param id 数据ID
*/
public void delDataById(String index, String type, String id) {
if(index == null || type == null || id == null) {
log.info(" 无法删除数据,缺唯一值!!!!!!! ");
return;
}
//开始删除数据
DeleteResponse response = client.prepareDelete(index, type, id).execute().actionGet();
log.info("删除数据状态,status-->>>>{},", response.status().getStatus());
}
/**
* 判断索引是否存在
*
* @param index
* @return
*/
public boolean isIndexExist(String index) {
IndicesExistsResponse iep = client.admin().indices().exists(new IndicesExistsRequest(index)).actionGet();
if (iep.isExists()) {
log.info("此索引 [" + index + "] 已经在ES集群里存在");
} else {
log.info(" 没有此索引 [" + index + "] ");
}
return iep.isExists();
}
}
service层,进行接口数据封装:
package org.githup.es.service;
import java.util.Map;
import com.alibaba.fastjson.JSONObject;
/**
* ES服务端
*
* @author sdc
*
*/
public interface ESSearchService {
/**
* 构建索引
* @param index
* @return
*/
public boolean buildIndex(String index);
/**
* 删除索引
* @param index
* @return
*/
public boolean delIndex(String index);
/**
* 查询数据
* @param index 索引<----->关系型数据库
* @param type 类型<----->关系型数据表
* @param id 数据ID<----->id
* @return
*/
public Map<String, Object> searchDataByParam(String index, String type, String id);
/**
* 更新数据
*
* @param data 添加的数据类型 json格式的
* @param index 索引<----->关系型数据库
* @param type 类型<----->关系型数据表
* @param id 数据ID<----->id
* @return
*/
public void updateDataById(JSONObject data, String index, String type, String id);
/**
* 添加数据
*
* @param data 添加的数据类型 json格式的
* @param index 索引<----->关系型数据库
* @param type 类型<----->关系型数据表
* @param id 数据ID<----->id
* @return
*/
public String addTargetDataALL(JSONObject data, String index, String type, String id);
/**
* 通过ID删除数据
*
* @param index 索引,类似数据库
* @param type 类型,类似表
* @param id 数据ID
*/
public void delDataById(String index, String type, String id);
/**
* 判断索引是否存在
*
* @param index
* @return
*/
public boolean isIndexExist(String index);
}
package org.githup.es.service.impl;
import java.util.Map;
import org.githup.es.dao.ESRepository;
import org.githup.es.service.ESSearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSONObject;
/**
* ES具体实现类
*
* 备注:抽出ES的分类信息
*
* @author sdc
*
*/
@Service
public class ESSearchServiceImpl implements ESSearchService{
@Autowired
private ESRepository eSRepository;
@Override
public boolean buildIndex(String index) {
return eSRepository.buildIndex(index);
}
@Override
public boolean delIndex(String index) {
return eSRepository.deleteIndex(index);
}
@Override
public Map<String, Object> searchDataByParam(String index, String type, String id) {
// TODO Auto-generated method stub
return eSRepository.searchDataByParam(index, type, id);
}
@Override
public void updateDataById(JSONObject data, String index, String type, String id) {
// TODO Auto-generated method stub
eSRepository.updateDataById(data, index, type, id);
}
@Override
public String addTargetDataALL(JSONObject data, String index, String type, String id) {
// TODO Auto-generated method stub
return eSRepository.addTargetDataALL(data, index, type, id);
}
@Override
public void delDataById(String index, String type, String id) {
// TODO Auto-generated method stub
eSRepository.delDataById(index, type, id);
}
@Override
public boolean isIndexExist(String index) {
// TODO Auto-generated method stub
return eSRepository.isIndexExist(index);
}
}
maven环境:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.githup.es</groupId>
<artifactId>springboot-es-sample-search</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spring-boot-es</name>
<description>搜索服务的实现类</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<elasticsearch.version>5.5.3</elasticsearch.version>
<log4j2.version>2.6.2</log4j2.version>
<fastjson.version>1.2.31</fastjson.version>
<commons.lang3.version>3.4</commons.lang3.version>
</properties>
<repositories>
<repository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</repository>
</repositories>
<!-- elasticsearch 5.x 依赖 -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons.lang3.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
</project>
具体的代码网址githup:https://github.com/growup818/springboot-es-search
可以下载下来,熟悉springboot的小伙伴可以很快进行demo检测。
原文地址:http://blog.51cto.com/shangdc/2096226
时间: 2024-10-10 09:25:06