Elasticsearch构建的通用搜索服务模块
在各种系统(ERP,CRM,电商。。。)中,客户通过“搜索框”、分类检索,进行内容的查找都是必不可少的一个需求,虽然项目不同,对搜索功能的定义都是大同小异,所以我结合以前的项目对搜索功能的需求打造一个通用的搜索服务模块。这个模块应当至少包括三个功能:搜索框搜索、分类检索、标签查找(定位)。
环境准备:
- 一台Linux服务器
- Elasticsearch 6.2.2
- Kibana
- SpringBoot
- 一个关系数据库:Mysql
ElasticSearch的自动补全、纠错、关键词高亮实现请参考我的另一篇文章
汉字转拼音
各模块设计
搜索框工作流程
分类检索关系数据库表结构
SpringBoot下的JPA分页查询示例
条件搜索分页查询
码代码
package CommodityManage.services;
import CommodityManage.entities.Goods;
import CommodityManage.repositories.GoodsDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
@Service
public class GoodsService {
@Autowired
private GoodsDao goodsDao;
private Page<Goods> goodsPage;
private Pageable pageable;
public List<Goods> listGoodsByPage(Example<Goods> goodsExample, int start, int max){
Pageable pageable=new PageRequest(start,max);
List<Goods> results= goodsDao.findAll(goodsExample,pageable).getContent();
Long totalElements= goodsDao.findAll(goodsExample,pageable).getTotalElements();
System.out.println("The Total Elements is :"+totalElements);
int totalPages= goodsDao.findAll(goodsExample,pageable).getTotalPages();
System.out.println("the total pages is "+totalPages);
return results;
}
public GoodsService(GoodsDao gd){
this.goodsDao=gd;
}
/**
* 第一页
* @param example
* @param max
* @return
*/
public List<Goods> listGoodsByFirstPage(Goods example,int max){
if (example == null) {
example=new Goods();
}
Example<Goods> goodsExample = Example.of(example);
Pageable pageable=new PageRequest(0,max);
if(this.goodsPage!=null) {
this.goodsPage = this.goodsDao.findAll(goodsExample, pageable.first());
this.pageable= this.goodsPage.getPageable();
}
else{
this.goodsPage = this.goodsDao.findAll(goodsExample, pageable.first());
this.pageable= this.goodsPage.getPageable();
}
List<Goods> results= this.goodsPage.getContent();
return results;
}
/**
* 下一页
* @param example
* @param max
* @return
*/
public List<Goods> listGoodsByNextPage(Goods example,int max){
if (example == null) {
example=new Goods();
}
Example<Goods> goodsExample = Example.of(example);
if(this.goodsPage!=null) {
if (this.goodsPage.hasNext()) {
this.goodsPage = goodsDao.findAll(goodsExample, this.goodsPage.nextPageable());
List<Goods> results = this.goodsPage.getContent();
return results;
}
}
else{
return this.listGoodsByFirstPage(example, max);
}
return null;
}
/**
* 上一页
* @param example
* @param max
* @return
*/
public List<Goods> listGoodsByPreviousPage(Goods example,int max){
if (example == null) {
example=new Goods();
}
Example<Goods> goodsExample = Example.of(example);
if(this.goodsPage!=null) {
if (this.goodsPage.hasPrevious()) {
this.goodsPage = goodsDao.findAll(goodsExample, this.goodsPage.previousPageable());
List<Goods> results = this.goodsPage.getContent();
return results;
}
}
else{
return this.listGoodsByFirstPage(example, max);
}
return null;
}
/**
* 最后一页
* @param example
* @param max
* @return
*/
public List<Goods> listGoodsByLastPage(Goods example,int max){
if (example == null) {
example=new Goods();
}
Example<Goods> goodsExample = Example.of(example);
if(!this.goodsPage.isEmpty()) {
this.goodsPage = goodsDao.findAll(goodsExample,new PageRequest(this.goodsPage.getTotalPages()-1,max));
List<Goods> results = this.goodsPage.getContent();
return results;
}
return null;
}
/**
* 跳转到指定页
* @param example
* @param start
* @param max
* @return
*/
public List<Goods> listGoodsByJumpPage(Goods example,int start,int max){
if (example == null) {
example=new Goods();
}
Example<Goods> goodsExample = Example.of(example);
Pageable pageable=new PageRequest(start,max);
if ( pageable.isUnpaged()) {
return null;
}
if(this.goodsPage!=null) {
this.goodsPage = this.goodsDao.findAll(goodsExample, pageable);
}
else{
return this.listGoodsByFirstPage(example, max);
}
List<Goods> results= this.goodsPage.getContent();
return results;
}
/**
* 总页数
* @return
*/
public int getTotalPages(){
return this.goodsPage.getTotalPages();
}
/**
* 元素数
* @return
*/
public long getTotalElements(){
return this.goodsPage.getTotalElements();
}
}