forked from zhoustone/middle-ware-parent
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SpringBoot2.0 整合 ElasticSearch框架,实现高性能搜索引擎
- Loading branch information
cicadasmile
committed
Jul 9, 2019
1 parent
e8aaec8
commit a60dcb7
Showing
11 changed files
with
367 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?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"> | ||
<parent> | ||
<artifactId>middle-ware-parent</artifactId> | ||
<groupId>com.boot.parent</groupId> | ||
<version>1.0-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>com.elastic.search</groupId> | ||
<artifactId>ware-elastic-search</artifactId> | ||
<packaging>jar</packaging> | ||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
<version>${spring-boot.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-data-elasticsearch</artifactId> | ||
<version>${spring-boot.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-test</artifactId> | ||
<version>${spring-boot.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-aop</artifactId> | ||
<version>${spring-boot.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-configuration-processor</artifactId> | ||
<version>${spring-boot.version}</version> | ||
<optional>true</optional> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-context-support</artifactId> | ||
<version>${spring.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>joda-time</groupId> | ||
<artifactId>joda-time</artifactId> | ||
<version>${joda.time.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.alibaba</groupId> | ||
<artifactId>fastjson</artifactId> | ||
<version>${fastjson.version}</version> | ||
</dependency> | ||
</dependencies> | ||
</project> |
11 changes: 11 additions & 0 deletions
11
ware-elastic-search/src/main/java/com/elastic/search/ElasticApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.elastic.search; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
@SpringBootApplication | ||
public class ElasticApplication { | ||
public static void main(String[] args) { | ||
SpringApplication.run(ElasticApplication.class,args) ; | ||
} | ||
} |
84 changes: 84 additions & 0 deletions
84
ware-elastic-search/src/main/java/com/elastic/search/controller/RequestLogController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package com.elastic.search.controller; | ||
|
||
import com.elastic.search.model.RequestLog; | ||
import com.elastic.search.service.RequestLogService; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import javax.annotation.Resource; | ||
import java.util.Optional; | ||
|
||
@RestController | ||
public class RequestLogController { | ||
|
||
@Resource | ||
private RequestLogService requestLogService ; | ||
|
||
/** | ||
* 数据入库 Elastic | ||
*/ | ||
@RequestMapping("/insert") | ||
public String insert (){ | ||
return requestLogService.esInsert(10) ; | ||
} | ||
|
||
/** | ||
* 查询全部 Elastic | ||
*/ | ||
@RequestMapping("/findAll") | ||
public Iterable<RequestLog> findAll (){ | ||
return requestLogService.esFindAll() ; | ||
} | ||
|
||
/** | ||
* 根据ID修改 | ||
* ID:1560406811693 | ||
*/ | ||
@RequestMapping("/updateById/{id}") | ||
public String updateById (@PathVariable Long id){ | ||
RequestLog requestLog = new RequestLog() ; | ||
requestLog.setId(id); | ||
requestLog.setUserName("updateName"); | ||
return requestLogService.esUpdateById(requestLog) ; | ||
} | ||
|
||
/** | ||
* 根据ID查询 | ||
*/ | ||
@RequestMapping("/selectById/{id}") | ||
public RequestLog selectById (@PathVariable Long id){ | ||
Optional<RequestLog> requestLog = requestLogService.esSelectById(id) ; | ||
return requestLog.get() ; | ||
} | ||
|
||
/** | ||
* 根据指定条件排序 | ||
*/ | ||
@RequestMapping("/selectOrder") | ||
public Iterable<RequestLog> selectOrder (){ | ||
return requestLogService.esFindOrder() ; | ||
} | ||
|
||
/** | ||
* 多条件排序 | ||
*/ | ||
@RequestMapping("/selectOrders") | ||
public Iterable<RequestLog> selectOrders (){ | ||
return requestLogService.esFindOrders() ; | ||
} | ||
|
||
/** | ||
* 多条件 + 范围 搜索 | ||
*/ | ||
@RequestMapping("/search") | ||
public Iterable<RequestLog> search (){ | ||
return requestLogService.search() ; | ||
} | ||
|
||
/** | ||
* 分页查询 | ||
*/ | ||
public void findPage (){ | ||
|
||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
ware-elastic-search/src/main/java/com/elastic/search/model/RequestLog.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package com.elastic.search.model; | ||
|
||
import org.springframework.data.annotation.Id; | ||
import org.springframework.data.elasticsearch.annotations.Document; | ||
|
||
/** | ||
* 点开 Document 可以看到配置内容 | ||
* 加上了@Document注解之后,默认情况下这个实体中所有的属性都会被建立索引、并且分词。 | ||
* indexName索引名称 理解为数据库名 限定小写 | ||
* type 理解为数据库的表名称 | ||
* shards = 5 默认分区数 | ||
* replicas = 1 每个分区默认的备份数 | ||
* refreshInterval = "1s" 刷新间隔 | ||
* indexStoreType = "fs" 索引文件存储类型 | ||
*/ | ||
@Document(indexName = "requestlogindex",type = "requestlog") | ||
public class RequestLog { | ||
//Id注解Elasticsearch里相应于该列就是主键,查询时可以使用主键查询 | ||
@Id | ||
private Long id; | ||
private String orderNo; | ||
private String userId; | ||
private String userName; | ||
private String createTime; | ||
public Long getId() { | ||
return id; | ||
} | ||
public void setId(Long id) { | ||
this.id = id; | ||
} | ||
public String getOrderNo() { | ||
return orderNo; | ||
} | ||
public void setOrderNo(String orderNo) { | ||
this.orderNo = orderNo == null ? null : orderNo.trim(); | ||
} | ||
public String getUserId() { | ||
return userId; | ||
} | ||
public void setUserId(String userId) { | ||
this.userId = userId == null ? null : userId.trim(); | ||
} | ||
public String getUserName() { | ||
return userName; | ||
} | ||
public void setUserName(String userName) { | ||
this.userName = userName == null ? null : userName.trim(); | ||
} | ||
public String getCreateTime() { | ||
return createTime; | ||
} | ||
public void setCreateTime(String createTime) { | ||
this.createTime = createTime; | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
ware-elastic-search/src/main/java/com/elastic/search/repository/RequestLogRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.elastic.search.repository; | ||
|
||
import com.elastic.search.model.RequestLog; | ||
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; | ||
|
||
public interface RequestLogRepository extends ElasticsearchRepository<RequestLog,Long> { | ||
} |
15 changes: 15 additions & 0 deletions
15
ware-elastic-search/src/main/java/com/elastic/search/service/RequestLogService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.elastic.search.service; | ||
|
||
import com.elastic.search.model.RequestLog; | ||
|
||
import java.util.Optional; | ||
|
||
public interface RequestLogService { | ||
String esInsert (Integer num) ; | ||
Iterable<RequestLog> esFindAll () ; | ||
String esUpdateById (RequestLog requestLog) ; | ||
Optional<RequestLog> esSelectById (Long id) ; | ||
Iterable<RequestLog> esFindOrder () ; | ||
Iterable<RequestLog> esFindOrders () ; | ||
Iterable<RequestLog> search () ; | ||
} |
91 changes: 91 additions & 0 deletions
91
ware-elastic-search/src/main/java/com/elastic/search/service/impl/RequestLogServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package com.elastic.search.service.impl; | ||
|
||
import com.elastic.search.model.RequestLog; | ||
import com.elastic.search.repository.RequestLogRepository; | ||
import com.elastic.search.service.RequestLogService; | ||
import com.elastic.search.util.DateUtil; | ||
import org.elasticsearch.index.query.*; | ||
import org.springframework.data.domain.Sort; | ||
import org.springframework.stereotype.Service; | ||
import javax.annotation.Resource; | ||
import java.util.ArrayList; | ||
import java.util.Date; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
@Service | ||
public class RequestLogServiceImpl implements RequestLogService { | ||
|
||
@Resource | ||
private RequestLogRepository requestLogRepository ; | ||
|
||
@Override | ||
public String esInsert(Integer num) { | ||
for (int i = 0 ; i < num ; i++){ | ||
RequestLog requestLog = new RequestLog() ; | ||
requestLog.setId(System.currentTimeMillis()); | ||
requestLog.setOrderNo(DateUtil.formatDate(new Date(),DateUtil.DATE_FORMAT_02)+System.currentTimeMillis()); | ||
requestLog.setUserId("userId"+i); | ||
requestLog.setUserName("张三"+i); | ||
requestLog.setCreateTime(DateUtil.formatDate(new Date(),DateUtil.DATE_FORMAT_01)); | ||
requestLogRepository.save(requestLog) ; | ||
} | ||
return "success" ; | ||
} | ||
|
||
@Override | ||
public Iterable<RequestLog> esFindAll (){ | ||
return requestLogRepository.findAll() ; | ||
} | ||
|
||
@Override | ||
public String esUpdateById(RequestLog requestLog) { | ||
requestLogRepository.save(requestLog); | ||
return "success" ; | ||
} | ||
|
||
@Override | ||
public Optional<RequestLog> esSelectById(Long id) { | ||
return requestLogRepository.findById(id) ; | ||
} | ||
|
||
@Override | ||
public Iterable<RequestLog> esFindOrder() { | ||
// 用户名倒序 | ||
// Sort sort = new Sort(Sort.Direction.DESC,"userName.keyword") ; | ||
// 创建时间正序 | ||
Sort sort = new Sort(Sort.Direction.ASC,"createTime.keyword") ; | ||
return requestLogRepository.findAll(sort) ; | ||
} | ||
|
||
@Override | ||
public Iterable<RequestLog> esFindOrders() { | ||
List<Sort.Order> sortList = new ArrayList<>() ; | ||
Sort.Order sort1 = new Sort.Order(Sort.Direction.ASC,"createTime.keyword") ; | ||
Sort.Order sort2 = new Sort.Order(Sort.Direction.DESC,"userName.keyword") ; | ||
sortList.add(sort1) ; | ||
sortList.add(sort2) ; | ||
Sort orders = Sort.by(sortList) ; | ||
return requestLogRepository.findAll(orders) ; | ||
} | ||
|
||
@Override | ||
public Iterable<RequestLog> search() { | ||
// 全文搜索关键字 | ||
/* | ||
String queryString="张三"; | ||
QueryStringQueryBuilder builder = new QueryStringQueryBuilder(queryString); | ||
requestLogRepository.search(builder) ; | ||
*/ | ||
|
||
/* | ||
* 多条件查询 | ||
*/ | ||
QueryBuilder builder = QueryBuilders.boolQuery() | ||
// .must(QueryBuilders.matchQuery("userName.keyword", "历张")) 搜索不到 | ||
.must(QueryBuilders.matchQuery("userName", "张三")) // 可以搜索 | ||
.must(QueryBuilders.matchQuery("orderNo", "20190613736278243")); | ||
return requestLogRepository.search(builder) ; | ||
} | ||
|
||
} |
22 changes: 22 additions & 0 deletions
22
ware-elastic-search/src/main/java/com/elastic/search/util/DateUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.elastic.search.util; | ||
|
||
import org.joda.time.DateTime; | ||
import java.util.Date; | ||
|
||
/** | ||
* 基于 JODA 组件的时间工具类 | ||
*/ | ||
public class DateUtil { | ||
|
||
private DateUtil (){} | ||
|
||
public static final String DATE_FORMAT_01 = "yyyy-MM-dd HH:mm:ss" ; | ||
public static final String DATE_FORMAT_02 = "yyyyMMdd" ; | ||
/** | ||
* 指定格式获取时间 | ||
*/ | ||
public static String formatDate (Date date,String dateFormat){ | ||
DateTime dateTime = new DateTime(date) ; | ||
return dateTime.toString(dateFormat) ; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Tomcat | ||
server: | ||
tomcat: | ||
uri-encoding: UTF-8 | ||
max-threads: 1000 | ||
min-spare-threads: 30 | ||
port: 7007 | ||
connection-timeout: 5000ms | ||
|
||
spring: | ||
application: | ||
name: ware-elastic-search | ||
data: | ||
elasticsearch: | ||
# 默认 elasticsearch | ||
cluster-name: elasticsearch | ||
# 9200作为Http协议,主要用于外部通讯 | ||
# 9300作为Tcp协议,jar之间就是通过tcp协议通讯 | ||
cluster-nodes: 192.168.72.130:9300 |