• Springboot 配置使用 Elasticsearch


    一、安装Elasticsearch

    1、Windows安装

    Windows安装比较简单,ES官网Download Elasticsearch | Elastic下载压缩包,解压出来, bin 目录下有个elasticsearch.bat,双击,就运行起来了。
    在这里插入图片描述
    然后在浏览器输入localhost:9200验证,成功会返回下面的图片。
    在这里插入图片描述

    二、开始写代码

    我的springboot版本是2.7.5,ES是7.17.3

    官方文档
    项目结构如下:
    在这里插入图片描述

    1、引入依赖

    
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-data-elasticsearchartifactId>
            dependency>
            
            <dependency>
                <groupId>jakarta.jsongroupId>
                <artifactId>jakarta.json-apiartifactId>
                <version>2.0.1version>
            dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    2、配置文件

    两种方式
    (1)yaml配置文件

    server:
      port: 8081
    spring:
      elasticsearch:
        # elasticsearch地址
        uris: localhost:9200
        connection-timeout: 30000
        socket-timeout: 50000
        socket-keep-alive: false
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    (2)api 方式

    会覆盖掉yml

    import co.elastic.clients.elasticsearch.ElasticsearchClient;
    import co.elastic.clients.json.jackson.JacksonJsonpMapper;
    import co.elastic.clients.transport.ElasticsearchTransport;
    import co.elastic.clients.transport.rest_client.RestClientTransport;
    import org.apache.http.HttpHost;
    import org.elasticsearch.client.RestClient;
    import org.springframework.boot.SpringBootConfiguration;
    import org.springframework.context.annotation.Bean;
    
    /**
     * @author 
     */
    @SpringBootConfiguration
    public class ElasticSearchConfig {
        @Bean
        public ElasticsearchClient elasticsearchClient(){
            RestClient client = RestClient.builder(new HttpHost("localhost", 9200))
                    .setRequestConfigCallback(requestConfigBuilder -> requestConfigBuilder
                            .setConnectTimeout(30000)
                            .setSocketTimeout(50000))
                    .build();
            ElasticsearchTransport transport = new RestClientTransport(client, new JacksonJsonpMapper());
            return new ElasticsearchClient(transport);
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    3、新建 User 实体类

    id这个字段一定要有,作为主键索引,这个@Document里面的indexName就相当于mysql里面的表名,在elasticsearch里面叫索引。

    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    import org.springframework.data.annotation.Id;
    import org.springframework.data.elasticsearch.annotations.Document;
    
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    @Document(indexName = "user")
    public class User {
        @Id
        private String id;
        private String name;
        private String sex;
        private Integer age;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    4、新建 UserRepository

    import com.example.demo.document.User;
    import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
    
    /**
     * @author 
     */
    public interface UserRepository extends ElasticsearchRepository<User, String> {
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    5、新建 Controller

    import com.example.demo.document.User;
    import com.example.demo.repository.UserRepository;
    import org.springframework.data.domain.Page;
    import org.springframework.data.domain.PageRequest;
    import org.springframework.data.domain.Pageable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    @RequestMapping("/user")
    public class UserController {
        private final UserRepository userRepository;
    
        public UserController(UserRepository userRepository){
            this.userRepository = userRepository;
        }
    
        /**
         * 添加
         */
        @RequestMapping("/insert")
        public String insert() {
            User user = new User();
            user.setId("1");
            user.setName("徐一杰");
            user.setSex("男");
            user.setAge(22);
            userRepository.save(user);
            return "success";
        }
    
        /**
         * 删除
         */
        @RequestMapping("/delete")
        public String delete() {
            User user = userRepository.findById("1").get();
            userRepository.delete(user);
            return "success";
        }
    
        /**
         * 局部更新
         */
        @RequestMapping("/update")
        public String update() {
            User user = userRepository.findById("1").get();
            user.setName("泡泡");
            userRepository.save(user);
            return "success";
        }
        /**
         * 查询
         */
        @RequestMapping("/get")
        public User get() {
            User user = userRepository.findById("1").get();
            System.out.println(user);
            return user;
        }
    
        @RequestMapping("/getAll")
        public Page<User> getAll() {
            Pageable pageable = PageRequest.of(1,20);
            Page<User> user = userRepository.findAll(pageable);
            System.out.println(user);
            return user;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69

    6、测试

    (1) 启动项目

    可以看到,我们的user索引自动添加到elasticsearch里面了。

    在这里插入图片描述
    (2) 查询索引

    我们用postman请求 http://localhost:8081/user/getAll,可以看到,返回了 user 的信息。

    在这里插入图片描述

  • 相关阅读:
    Docker:本地目录挂载
    dpdk实现dns
    题解 [LuoguP3426][POI2005]SZA-Template
    Linux学习-44-虚拟内存、物理内存和swap分区的作用
    数据库篇--八股文学习第十八天| MySQL和Redis的区别是什么;Redis有什么优缺点?为什么用Redis查询会比较快
    java计算机毕业设计景区在线购票系统源码+系统+mysql数据库+lw文档+部署
    Java-day17(反射)
    自研一个简易版本的OkHTTP
    浏览器访问Nginx系统界面,除了主页 index.html 可以正常访问,其他的页面地址栏中访问都是404
    什么是网络安全?如何让普通人简单的了解网络安全
  • 原文地址:https://blog.csdn.net/qq_28256783/article/details/136139093