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
  • 相关阅读:
    ASP.NET 自制时间控件
    ORACLE 函数汇总之单记录函数
    Servers IIS 重启命令
    ASP.NET 两个Dropdownlist控件联动
    ASP.NET datagridview控件表头定义
    python Image 安装
    ssh 不需要密码的链接
    [Redis] redis 相关的博客
    [emacs] python代码折叠
    linux python 链接 oracle
  • 原文地址:https://www.cnblogs.com/lsys/p/11289200.html
Copyright © 2011-2022 走看看