直接干货上源码
1、首先是pom 文件
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.shamo.es</groupId>
<artifactId>shamo-es</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>shamoes</name>
<description>shamo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.54</version>
</dependency>
<dependency>
<groupId>com.bbossgroups.plugins</groupId>
<artifactId>bboss-elasticsearch-spring-boot-starter</artifactId>
<version>6.2.5</version>
<exclusions>
<exclusion>
<artifactId>slf4j-log4j12</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
<!--导入db-elasticsearch数据同步依赖包开始-->
<dependency>
<groupId>com.bbossgroups.plugins</groupId>
<artifactId>bboss-elasticsearch-rest-jdbc</artifactId>
<version>6.2.5</version>
<exclusions>
<exclusion>
<artifactId>slf4j-log4j12</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
</plugins>
</build>
</project>
2、springboot 、es配置文件
server.servlet.context-path=/
# 开发环境配置
server.port: 1800
# 日志配置
logging.level.com.anqi.es=error
logging.level.org.springframework=error
logging.level.com.anqi.es.highclient=DEBUG
logging.level.org.bboss=INFO
logging.level.bboss=INFO
#x-pack认证账号和口令
elasticUser=elastic
elasticPassword=changeme
#es服务器地址配置
elasticsearch.rest.hostNames=127.0.0.1:9200
#elasticsearch.rest.hostNames=10.180.211.27:9280,10.180.211.27:9281,10.180.211.27:9282
#动态索引表名称日期格式配置
elasticsearch.dateFormat=yyyy.MM.dd
elasticsearch.timeZone=Asia/Shanghai
elasticsearch.ttl=2d
#在控制台输出脚本调试开关showTemplate,false关闭,true打开,同时log4j至少是info级别
elasticsearch.showTemplate=true
#客户端动态发现es集群节点控制开关
elasticsearch.discoverHost=false
#http链接池配置
http.timeoutConnection = 50000
http.timeoutSocket = 50000
http.connectionRequestTimeout=50000
http.retryTime = 1
http.maxLineLength = -1
http.maxHeaderCount = 200
http.maxTotal = 400
http.defaultMaxPerRoute = 200
# dsl配置文件热加载扫描时间间隔,毫秒为单位,默认5秒扫描一次,<= 0时关闭扫描机制
dslfile.refreshInterval = -1
2、es查询语句放在esmapper/sj.xml下
<properties>
<property name="searchDatas">
<![CDATA[
{
"query": {
"bool": {
"should": [
{
"match": {
"name": #[keywords]
}
},
{
"match": {
"info.keyword": #[keywords]
}
}
]
}
}
,"size":1000,
"highlight": {
"pre_tags": [
"<mark>"
],
"post_tags": [
"</mark>"
],
"fields": {
"*": {}
},
"fragment_size": 2147483647
}
}
]]>
</property>
</properties>
3、
EsService
@Service
public class EsService {
private Logger logger = LoggerFactory.getLogger(EsService.class);
@Autowired
private BBossESStarter bbossESStarter;
private String mappath = "esmapper/sj.xml";
public String saveDocEntity(String index, DocBean docBean){
ClientInterface clientUtil = bbossESStarter.getRestClient();
String response = clientUtil.addDocument( index , docBean,"refresh=true");
return response;
}
public DocSearchResult searchDoc(String docIndex, String keywords) {
List<DocBean> doclist = new ArrayList<>();
ClientInterface clientUtil = bbossESStarter.getConfigRestClient(mappath);
Map<String,Object> params = new HashMap<String,Object>();
params.put("keywords",keywords);
//Execute the query
ESDatas<DocESBaseData> esDatas =
clientUtil.searchList(docIndex+"/_search",
"searchDatas",
params,
DocESBaseData.class);
List<DocESBaseData> list = esDatas.getDatas();
if(list!=null){
for (DocESBaseData doc:list){
DocBean docBean = new DocBean();
String highlightname= doc.getHighlight().get("name").get(0).toString();
docBean.setId(doc.getId());
docBean.setName(doc.getName());
docBean.setSrc(doc.getSrc());
docBean.setUpdate_date(doc.getUpdate_date());
docBean.setHighlightname(highlightname);
doclist.add(docBean);
}
}
// String json = clientUtil.executeRequest("demo/_search",//demo as the index table, _search as the search action
// "searchDatas",//DSL statement name defined in esmapper/demo.xml
// params);//Query parameters
// String json = com.frameworkset.util.SimpleStringUtil.object2json(demos);
//Gets the total number of records
long totalSize = esDatas.getTotalSize();
DocSearchResult docSearchResult = new DocSearchResult();
docSearchResult.setDocBean(doclist);
docSearchResult.setTotalSize(totalSize);
return docSearchResult;
}
}
4、DocSearchResult
public class DocSearchResult {
private List<DocBean> docBean;
private long totalSize;
public List<DocBean> getDocBean() {
return docBean;
}
public void setDocBean(List<DocBean> docBean) {
this.docBean = docBean;
}
public long getTotalSize() {
return totalSize;
}
public void setTotalSize(long totalSize) {
this.totalSize = totalSize;
}
}
5、DocESBaseData
public class DocESBaseData extends ESBaseData {
public String id;
public String src;
public String name;
public String info;
public String update_date;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getSrc() {
return src;
}
public void setSrc(String src) {
this.src = src;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
public String getUpdate_date() {
return update_date;
}
public void setUpdate_date(String update_date) {
this.update_date = update_date;
}
}
6、DocBean
public class DocBean {
public String id;
public String src;
public String name;
public String info;
public String update_date;
public String highlightname;
public String getName() {
return name;
}
public String getHighlightname() {
return highlightname;
}
public void setHighlightname(String highlightname) {
this.highlightname = highlightname;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getSrc() {
return src;
}
public void setSrc(String src) {
this.src = src;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
public String getUpdate_date() {
return update_date;
}
public void setUpdate_date(String update_date) {
this.update_date = update_date;
}
}
7、DocSearchConfigController
@Controller
@RequestMapping(value = "/api/doc/config")
@PropertySource("classpath:application.properties")
public class DocSearchConfigController {
@Value("${doc.index}")
private String docIndex;
@Autowired
EsService esService;
@RequestMapping(value = "/get/detail")
@ResponseBody
public DocSearchResult godetail(@RequestParam("keywords") String keywords ){
DocSearchResult docSearchResult=null;
try{
if (keywords!=null&&keywords.length()>0 ){
docSearchResult= esService.searchDoc(this.docIndex,keywords);
}
} catch(Exception e){
e.printStackTrace();
}
return docSearchResult;
}