本文源碼 GitHub:知了一笑 https://github.com/cicadasmile/spring-boot-base
ElasticSearch是一個基於Lucene的搜索服務器。它提供了一個分佈式多用戶能力的全文搜索引擎,基於RESTful web接口。Elasticsearch是用Java開發的,並做爲Apache許可條款下的開放源碼發佈,是當前流行的企業級搜索引擎。html
搭建運行環境node
Linux系統:centos7下搭建ElasticSearch中間件,經常使用接口演示git
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> <version>${spring-boot.version}</version> </dependency>
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
Document 配置<br/>
加上了@Document註解以後,默認狀況下這個實體中全部的屬性都會被創建索引、而且分詞。<br/>
indexName索引名稱 理解爲數據庫名 限定小寫<br/>
type 理解爲數據庫的表名稱<br/>
shards = 5 默認分區數<br/>
replicas = 1 每一個分區默認的備份數<br/>
refreshInterval = "1s" 刷新間隔<br/>
indexStoreType = "fs" 索引文件存儲類型<br/>github
@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; }
實現ElasticsearchRepository接口。web
public interface RequestLogRepository extends ElasticsearchRepository<RequestLog,Long> { }
數據增長,修改,查詢,排序,多條件查詢。spring
@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) ; } }
GitHub地址:知了一笑 https://github.com/cicadasmile 碼雲地址:知了一笑 https://gitee.com/cicadasmile