欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

SpringBoot整合ElasticSearch

程序员文章站 2024-03-05 15:30:55
...

SpringBoot整合ElasticSearch实现增删改查

前言

ElasticSearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。前几天我接触了ElasticSearch,然后花了两三天的时间弄明白了SpringBoot整合ElasticSearch。

功能简述

使用SpringBoot整合ElasticSearch,实现增删改查。(ElasticSearch入门,我这里先不做介绍)

具体实现

1.启动elasticsearch

2.新建项目,pom文件中加入elasticsearch依赖,完整pom如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.elasticsearch</groupId>
    <artifactId>study</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>study</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

编写配置文件(application.properties),配置文件如下:

## 端口号
server.port=8080

## es地址
spring.data.elasticsearch.cluster-nodes=127.0.0.1:9300

新建一个商品实体类GoodsInfo,其中需要注意:
indexName:索引名称 可以理解为数据库名 必须为小写 不然会报org.elasticsearch.indices.InvalidIndexNameException异常
type:类型 可以理解为表名

package com.elasticsearch.study.entity;

import org.springframework.data.elasticsearch.annotations.Document;

import java.io.Serializable;

@Document(indexName = "testgoods",type = "goods")
public class GoodsInfo implements Serializable {
    private Long id;
    private String name;
    private String description;

    public GoodsInfo(long currentTimeMillis) {
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public GoodsInfo(Long id,String name,String description){
        this.id=id;
        this.name=name;
        this.description=description;
    }

    public GoodsInfo(){

    }


}

创建GoodsRepository,继承ElasticsearchRepository,代码如下:

package com.elasticsearch.study.repository;

import com.elasticsearch.study.entity.GoodsInfo;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;

public interface GoodsRepository extends ElasticsearchRepository<GoodsInfo,Long> {
}

最后新建一个controller进行测试,增删改查

package com.elasticsearch.study.controller;

import com.elasticsearch.study.entity.GoodsInfo;
import com.elasticsearch.study.repository.GoodsRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;



@RestController
public class GoodsController {

    @Autowired
    private GoodsRepository goodsRepository;

    /**添加数据**/
    @GetMapping("save")
    public String save(){
        GoodsInfo goodsInfo=new GoodsInfo(System.currentTimeMillis(),
                "商品"+System.currentTimeMillis(),"这是一个测试商品");
        goodsRepository.save(goodsInfo);
        return "success";
    }

    /**删除数据**/
    @GetMapping("delete")
    public String delete(Long id){
        goodsRepository.deleteById(id);
        return "success";
    }

    /**查找指定id的数据**/
    @GetMapping("getOne")
    public GoodsInfo getOne(Long id){
        GoodsInfo goodsInfo=goodsRepository.findById(id).get();
        return goodsInfo;
    }

    //http://localhost:8888/update?id=1525417362754&name=修改&description=修改
    /**修改数据**/
    @GetMapping("update")
    public String update(Long id,String name,String description){
        GoodsInfo goodsInfo = new GoodsInfo(id,
                name,description);
        goodsRepository.save(goodsInfo);
        return "success";
    }



}

测试方法,打开postman,使用Get请求:
首先请求http://localhost:8080/save 增加数据
http://localhost:8080/delete?id=id号 删除数据
http://localhost:8080/getOne?id=id号 查找指定id号的数据
http://localhost:8080/update?id=1525417362754&name=修改&description=修改 修改指定id的数据信息

今天就分享到这里了,希望可以帮助到你