I. Installation and introduction

ElasticSearch is a Lucene-based search server. It provides a distributed multi – user – capable full – text search engine based on RESTful Web interface. Elasticsearch, developed in Java and released as open source under the Apache license, is a popular enterprise-level search engine.

Environment set up

Linux system: build ElasticSearch middleware under centos7, common interface demonstration

Integration with SpringBoot2

1. Core dependencies

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId>  <version>${spring-boot.version}</version></dependency>Copy the code

2. Configuration files

Spring: application: name: ware-elastic-search data: elasticSearch: Elasticsearch # 9200 is used to communicate with external nodes using Http. Jars communicate with each other using Tcp, cluster-Nodes: 192.168.72.139:9300Copy the code

3. Entity class configuration

Document configuration

With the @Document annotation, by default all attributes in the entity are indexed and segmented.IndexName indicates the indexNameType indicates the table name of the databaseShards = 5 Number of default partitionsReplicas = 1 Specifies the default number of backups for each partitionRefreshInterval = "1s" refreshIntervalIndexStoreType = "fs" Index file storage typeCopy the code

Source configuration

@document (indexName = "requestLogIndex ",type =" requestLog ")public class requestlog { You can query @ID private Long Id using the primary key. private String orderNo; private String userId; private String userName; private String createTime; }Copy the code

4. Data interaction layer

Implement ElasticsearchRepository interface.

public interface RequestLogRepository extends ElasticsearchRepository<RequestLog,Long> {}Copy the code

5. Demonstration cases

Data increase, modify, query, sort, multi – condition query.

@Servicepublic 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 = querybuilder.boolquery () //. Must (querybuilder.matchquery ("userName. Keyword ", Must (QueryBuilders. MatchQuery ("userName", "matchQuery ")) // Must (QueryBuilders. MatchQuery ("orderNo", "20190613736278243")); return requestLogRepository.search(builder) ; }}Copy the code

Source code address

Making address: given a smile cloud address: https://github.com/cicadasmile/middle-ware-parent code laughed told https://gitee.com/cicadasmile/middle-ware-parentCopy the code