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;
    }

}
原文地址:https://www.cnblogs.com/djq-jone/p/13355689.html