zoukankan      html  css  js  c++  java
  • springboot2.2.2集成6.5 Elasticsearch

    1.0POM文件

    <!-- spring-boot -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
                <exclusions>
                    <exclusion>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-starter-tomcat</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-undertow</artifactId>
                <scope>compile</scope>
            </dependency>
           
            <!-- elasticSearch -->
            <!-- es核心jar包 -->
            <dependency>
                <groupId>org.elasticsearch</groupId>
                <artifactId>elasticsearch</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.data</groupId>
                <artifactId>spring-data-elasticsearch</artifactId>
            </dependency>

    2.YML文件

    spring:
      profiles:
        active: dev-win
      redis:
        database: 0
        host: xxx
        lettuce:
          pool:
            max-active: 10
            max-idle: 10
            max-wait: 5000ms
            min-idle: 5
        password: ''
        port: 6379
        timeout: 3000
      session:
        redis:
          flush-mode: on_save
          namespace: clearing
        store-type: redis
        timeout: 86400
      data:
        elasticsearch:
          cluster-nodes: 192.168.1.234:9300
          cluster-name: docker-cluster
          repositories:
            enabled: true

    3.domain

    @Document(indexName = "test-boot",type = "goods",shards = 1,replicas = 0, refreshInterval = "-1")
    @Data
    public class Elastic implements Serializable {
        @Id
        private String id;
        @Field
        private String name;
        @Field
        private String content;
        @Field
        private String price;
    }

    4.Dao

    @Component
    public interface ESDao extends ElasticsearchRepository<Elastic, String> {
        Elastic queryEmployeeById(String id);
    }

    5.controller

    @RestController
    @RequestMapping("/es")
    public class ElasticSearchController {
    
        @Autowired
        private ESDao er;
    
        //增加
        @RequestMapping("/add/{id}")
        public String add(@PathVariable("id")String id){
    
            Elastic employee=new Elastic();
            employee.setId(id);
            employee.setName("Y.S.K");
            employee.setContent("ooo");
            employee.setPrice("26");
            er.save(employee);
    
            System.err.println("add a obj");
            return "success";
        }
    
        //删除
        @RequestMapping("/delete")
        public String delete(){
            Elastic employee=new Elastic();
            employee.setId("1");
            er.delete(employee);
    
            return "success";
        }
    
        //局部更新
        @RequestMapping("/update")
        public String update(){
    
            Elastic employee=er.queryEmployeeById("1");
            employee.setName("哈哈");
            er.save(employee);
    
            System.err.println("update a obj");
    
            return "success";
        }
    
        //查询
        @RequestMapping("/query/{id}")
        public Elastic query(@PathVariable("id")String id){
    
            Elastic accountInfo=er.queryEmployeeById(id);
            System.err.println(JSONUtil.parseObj(accountInfo));
    
            return accountInfo;
        }
    
    }
  • 相关阅读:
    关于meta便签详解
    移动端等分比显示导航状态
    css3单选 复选按钮--代码分享
    css-样式重构-代码分享
    代码分享h5-sessionStorage,提示app下载代码块
    微信浏览器打开 点击下载app 无需提示使用浏览器打开--代码分享
    js 判断IOS版本号
    二进制,八进制,十进制,十六进制之间的转换
    JS组件系列——Bootstrap文件上传组件:bootstrap fileinput
    Bootstrap文件上传插件File Input的使用
  • 原文地址:https://www.cnblogs.com/djq-jone/p/13355689.html
Copyright © 2011-2022 走看看