Spring Boot3.x集成ElasticSearch8.x

Spring Boot3.x集成ElasticSearch8.x

  • 版本说明,本demo使用Spring Boot3.2.1 + JDK17 + ElasticSearch8.11.3

  • 前提是已经部署好了自己的ElasticSearch环境,我这里直接用容器默认部署好了,能访问即可

创建Spring Boot项目

  • 导入pom依赖
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
            <version>3.2.1</version>
        </dependency>

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

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
  • 配置文件(yml格式)
spring:
  elasticsearch:
    uris: 139.xxx.xxx.xxx:9200
  • 实体类
import lombok.Builder;
import lombok.Data;
import org.springframework.data.elasticsearch.annotations.Document;

@Data
@Builder
// 这个注解要加上,不然会报索引名必须小写,默认是类名,也就是Product
@Document(indexName = "product")
public class Product {

    private Long id;

    private String name;

    private Integer price;

    private String description;

}
  • repository
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;

public interface ProductElasticsearchRepository extends ElasticsearchRepository<Product, Long> {
	// es的条件查询通过命名会转换为条件,这里就是模糊查询名字的意思,后面是分页参数,具体的命名规则可在官方文档查看
    Iterable<Product> findByNameLike(String name, PageRequest pageRequest);
}

官方文档:Query methods :: Spring Data Elasticsearch

  • controller
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
public class EsController {

    @Resource
    private ProductElasticsearchRepository repository;

    @GetMapping("add/{id}")
    public Product add(@PathVariable Long id) {
        return repository.save(Product.builder()
                .id(id)
                .name("商品" + id)
                .price(99 + id.intValue())
                .description("超级简介咯咯咯" + id).build());
    }

    @GetMapping("findAll")
    public Iterable<Product> findAll() {
        return repository.findAll();
    }

    @GetMapping("findPage/{page}/{size}")
    public Iterable<Product> findPage(@PathVariable("page") Integer page, @PathVariable("size") Integer size) {
        return repository.findAll(PageRequest.of(page, size, Sort.by("id").descending()));
    }

    @GetMapping("findByNameLike/{page}/{size}/{name}")
    public Iterable<Product> findByNameLike(@PathVariable("page") Integer page, @PathVariable("size") Integer size, @PathVariable("name") String name) {
        return repository.findByNameLike(name, PageRequest.of(page, size, Sort.by("id").descending()));
    }
    
}

测试

  • 调用add接口插入一些数据
http://localhost:8080/add/1

  • 然后就可以测试其他的查询方法了
http://localhost:8080/findAll

注意,分页页码默认是从0开始的

http://localhost:8080/findPage/0/5

http://localhost:8080/findByNameLike/0/5/6

  • 至于其他改删的方法就自行调用出来就好了,小demo,所以不会考虑规范性,主打一个图一乐
posted @ 2024-01-16 17:15  -西门-  阅读(538)  评论(0编辑  收藏  举报