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

}
复制代码

 

posted @   远启  阅读(1689)  评论(1编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示