In this paper, the source code making: laughed told https://github.com/cicadasmile/spring-boot-baseCopy the code

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.

Setting up the operating environment

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:
      # the default elasticsearch
      cluster-name: elasticsearch
      # 9200 as Http protocol, mainly used for external communication
      As Tcp protocol, jars communicate with each other through Tcp protocolCluster nodes: 192.168.72.130:9300Copy the code

3. Entity class configuration

With the @Document annotation in the Document configuration, by default all attributes in the entity are indexed and segmented. Shards = 5 default number of partitions replicas = 1 default number of backups in each partition refreshInterval = “1s” refreshInterval IndexStoreType = “fs” Index file storage type

@Document(indexName = "requestlogindex".type = "requestlog"@id private Long Id; @id private Long Id; @id private Long Id; @id private Long Id; 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.

@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("Zhang"+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() {// full text search keyword /* String queryString="Zhang"; QueryStringQueryBuilder builder = new QueryStringQueryBuilder(queryString); requestLogRepository.search(builder) ; QueryBuilder = querybuilder.boolquery () //. Must (querybuilder.matchquery ("userName.keyword"."Through a"Must (QueryBuilders. MatchQuery ("userName"."Zhang"Must (QueryBuilders. MatchQuery ("orderNo"."20190613736278243"));
        returnrequestLogRepository.search(builder) ; }}Copy the code

Source code address

Making address: given a smile cloud address: https://github.com/cicadasmile code laughed told https://gitee.com/cicadasmileCopy the code