zoukankan      html  css  js  c++  java
  • Springboot 集成 ElasticSearch 踩坑

    这里只涉及到基础使用

    1. 导包
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
    </dependency>
    
    1. 配置application.yml
    server:
      port: 8069
    spring:
      data:
        elasticsearch:
          cluster-nodes: 127.0.0.1:9300 #配置java访问elasticsearch端口
          repositories:
            enabled: true
          cluster-name: elasticsearch #需与elasticsearch配置文件的名字一致,也可以不配置
    
    1. 创建普通的entity
    @Data
    @Document(indexName = "索引名称(只能小写)",type = "类型名")
    public class Study{
        private Integer id;
        private String name;
        private String sex;
        private Integer age; 
    }
    
    
    1. 创建仓库 StudyRepository 并继承 ElasticsearchCrudRepository<Study, Integer>
    public interface StudyRepository extends ElasticsearchCrudRepository<Study, Integer> {
    }
    
    1. 创建service和实现类,以及Controller
    public interface StudyService {
    
        Study save(Study study);
    
        void delete(Integer id);
    
        Study update(Study study);
    
        Study get(Integer id);
    
        Page<Study> getAll(String name,Pageable pageable);
    }
    
    
    @Service
    @RequiredArgsConstructor
    public class StudyServiceImpl implements StudyService {
    
        private final StudyRepository studyRepository;
    
        private final ElasticsearchTemplate elasticsearchTemplate;
    
        @Override
        public Study save(Study study) {
            return studyRepository.save(study);
        }
    
        @Override
        public void delete(Integer id) {
            studyRepository.deleteById(id);
        }
    
        @Override
        public Study update(Study study) {
            Optional<Study> studyOptional = studyRepository.findById(study.getId());
            studyOptional.ifPresent(s -> {
                studyRepository.save(study);
            });
            return study;
        }
    
        @Override
        public Study get(Integer id) {
            return studyRepository.findById(id).orElse(null);
        }
    	/**
    	 * 根据name查询(使用template进行复杂查询)
    	 */
        @Override
        public Page<Study> getAll(String name, Pageable pageable) {
            NativeSearchQueryBuilder query = new NativeSearchQueryBuilder().withPageable(pageable);
            if (!StringUtils.isEmpty(name)) {
                query.withQuery(QueryBuilders.matchQuery("name", name));
            }
            NativeSearchQuery buildQuery = query.build();
            List<Study> studies = elasticsearchTemplate.queryForList(buildQuery , Study.class);
            return new PageImpl<>(studies);
        }
    
    

    注意

    1. 目前ElasticSearch没有7.2的jar包~~使用7.2会报找不到nodes,建议降低版本~

    2. ElasticSearch提供的可视化工具 Kibana 的版本必须与你安装的ElasticSearch版本一一对应,不然无法启动~

    参考:

    1. https://segmentfault.com/a/1190000018625101
  • 相关阅读:
    动态规划:DAG-嵌套矩形
    动态规划:LCIS
    动态规划&字符串:最长公共子串
    动态规划:LCS
    动态规划:状压DP-斯坦纳树
    动态规划:数位DP
    JavaScript 正则表达式
    JavaScript 类型转换
    JavaScript typeof, null, 和 undefined
    JavaScript if...Else 语句
  • 原文地址:https://www.cnblogs.com/lsys/p/11289200.html
Copyright © 2011-2022 走看看