springboot和elasticsearchd的整合网上有很多例子,要不就是elasticsearch太老,要不就是和springboot版本不匹配报错:
org.elasticsearch.transport.NodeDisconnectedException: [][127.0.0.1:9300][cluster:monitor/nodes/liveness] disconnected
所以自己已微服务的形式整理了一下基于springboot2.0和elasticsearch5.6.8的整合实现基本的CRUD,核心包和类在下面列出:
依赖包
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<!-- 统一指定springboot包的相关版本号 -->
<version>2.0.1.RELEASE</version>
<relativePath/>
</parent>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
配置文件:
server:
port: 8085
spring:
application:
name: es-search
data:
elasticsearch:
cluster-nodes: 127.0.0.1:9300
cluster-name: elasticsearch
repositories:
enabled: true
public interface SearchServer {
@RequestMapping(value = "/goodslist")
Response getList(@RequestParam("fieldName") String fieldName,@RequestParam("query") String query);
@RequestMapping(value = "/goodssave")
Response save(@RequestBody GoodsDTO goodsDTO);
@RequestMapping(value = "/goodspage")
Response page(@RequestParam(value = "pageNum",required = false) Integer pageNum,@RequestParam(value = "pageSize",required = false) Integer pageSize,@RequestParam("fieldName") String fieldName,@RequestParam("query") String query);
@RequestMapping(value = "/goodsdel")
Response delete(@RequestBody GoodsDTO goodsDTO);
}
public interface BaseService<T> {
List<T> getList(int type,String fieldName,String query);
T save(T t);
Page<T> page(Integer pageNum, Integer pageSize,int type,String fieldName, String query);
void delete(T t);
}
public abstract class BaseSearchService<T> implements BaseService<T>{
protected abstract BaseRepository getBaseRepository();
/**
*
* @param type 类型 1:精确查询,2:模糊查询
* @param fieldName 查询的字段名称
* @param query 查询条件
* @return
*/
public List<T> getList(int type,String fieldName,String query) {
if(type != SearchType.ac_type && type != SearchType.ob_type){
throw new IllegalArgumentException("非法入参");
}
//模糊匹配
if(type == SearchType.ob_type){
query=matchString(query);
}
WildcardQueryBuilder queryBuilder = QueryBuilders.wildcardQuery(fieldName, query);
Iterable<T> search = getBaseRepository().search(queryBuilder);
List<T> list=new ArrayList<>();
Iterator<T> iterator = search.iterator();
while (iterator.hasNext()){
list.add(iterator.next());
}
return list;
}
/**
* 添加索引(包含更新)
* @param t
* @return
*/
public T save(T t){
return (T) getBaseRepository().save(t);
}
/**
* 分页查询
* @param pageNum 起始页
* @param pageSize
* @param type 类型 1:精确查询,2:模糊查询
* @param fieldName 查询的字段名称
* @param query 查询条件
* @return
*/
public Page<T> page(Integer pageNum, Integer pageSize,int type,String fieldName, String query) {
if(type != SearchType.ac_type && type != SearchType.ob_type){
throw new IllegalArgumentException("非法入参");
}
if(pageNum == null) pageNum = 0;
if(pageSize == null) pageSize=20;
//模糊匹配
if(type == SearchType.ob_type){
query=matchString(query);
}
WildcardQueryBuilder queryBuilder = QueryBuilders.wildcardQuery(fieldName, query);
return getBaseRepository().search(queryBuilder, PageRequest.of(pageNum,pageSize));
}
/**
* 删除
* @param t
*/
public void delete(T t){
getBaseRepository().delete(t);
}
protected String matchString(String str){
return "*".concat(str).concat("*");
}
}
@RestController
@RequestMapping("search/goods")
public class GoodsController implements SearchServer{
@Autowired
private GoodsService goodsService;
@Override
public Response getList(String fieldName,String query){
List<GoodsInfo> list = goodsService.getList(SearchType.ob_type, fieldName, query);
return Response.buildSuccessResponse().setData(list);
}
@Override
public Response save(GoodsDTO goodsDTO) {
GoodsInfo goodsInfo=new GoodsInfo();
BeanUtils.copyProperties(goodsDTO,goodsInfo);
goodsService.save(goodsInfo);
return Response.buildSuccessResponse();
}
@Override
public Response page(Integer pageNum,Integer pageSize,String fieldName, String query){
Page<GoodsInfo> page = goodsService.page(pageNum, pageSize, SearchType.ob_type, fieldName, query);
return Response.buildSuccessResponse().setData(new PageResult<>(page.getTotalElements(),page.getContent()));
}
@Override
public Response delete(GoodsDTO goodsDTO) {
GoodsInfo goodsInfo=new GoodsInfo();
BeanUtils.copyProperties(goodsDTO,goodsInfo);
goodsService.delete(goodsInfo);
return Response.buildSuccessResponse();
}
}
@Service
public class GoodsServiceImpl extends BaseSearchService<GoodsInfo> implements GoodsService {
@Autowired
private GoodsRepository goodsRepository;
@Override
public BaseRepository getBaseRepository() {
return goodsRepository;
}
}
@Component
public interface GoodsRepository extends BaseRepository<GoodsInfo, Long> {
}