Spring Boot 集成spring-boot-starter-data-elasticsearch

第一步,添加Maven依赖
复制代码
        <!--es-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>
第二步,配置yml
复制代码
spring:
  elasticsearch:
    uris: http://localhost:9200
第三步,创建索引实体

@Document 指定索引名称

@Field 字段对应类型

复制代码
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;

@Document(indexName = "documents")
public class Document {
    @Id
    private String id;

    @Field(type = FieldType.Text)
    private String title;

    @Field(type = FieldType.Text)
    private String content;

    // Getters and Setters
    public String getId() {
        return id;
    }

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

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }
}
第四步,创建仓库
复制代码
import org.apache.hertzbeat.manager.pojo.dto.Document;
import org.springframework.data.elasticsearch.annotations.Query;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface DocumentRepository extends ElasticsearchRepository<Document, String> {
    // 自定义查询方法
    List<Document> findByTitle(String title);

    @Query("{\"match\": {\"name\": \"?0\"}}")
    List<Document> findByName(String name);
}

第五步,调用

复制代码
import lombok.extern.slf4j.Slf4j;
import org.apache.hertzbeat.manager.dao.master.DocumentRepository;
import org.apache.hertzbeat.manager.pojo.dto.Document;
import org.apache.hertzbeat.manager.service.DocumentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.Criteria;
import org.springframework.data.elasticsearch.core.query.CriteriaQuery;
import org.springframework.data.elasticsearch.core.query.Query;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

@Slf4j
@Service
public class DocumentServiceImpl implements DocumentService {

    @Autowired
    private DocumentRepository documentRepository;

    @Override
    public Document saveDocument(Document document) {
        return documentRepository.save(document);
    }

    @Override
    public Iterable<Document> getAllDocuments() {
        return  documentRepository.findAll();
    }

    @Override
    public Document getDocumentById(String id) {
        return documentRepository.findById(id).orElse(null);
    }

    @Override
    public List<Document> findByTitle(String title) {
        return documentRepository.findByTitle(title);
    }

    @Override
    public void deleteDocumentById(String id) {
        documentRepository.deleteById(id);
    }

}
相关推荐
我登哥MVP20 分钟前
【SpringMVC笔记】 - 8 - 文件上传与下载
java·spring boot·spring·servlet·tomcat·maven
MY_TEUCK1 小时前
从零开始:使用Sealos Devbox快速搭建云原生开发环境
人工智能·spring boot·ai·云原生·aigc
倒霉蛋小马2 小时前
SpringBoot3中配置Knife4j
java·spring boot·后端
NotFound4862 小时前
实战分享怎样实现Spring Boot 中基于 WebClient 的 SSE 流式接口操作
java·spring boot·后端
不吃香菜学java10 小时前
Redis的java客户端
java·开发语言·spring boot·redis·缓存
橙露11 小时前
SpringBoot 整合 MinIO:分布式文件存储上传下载
spring boot·分布式·后端
小眼哥12 小时前
SpringBoot整合Vue代码生成exe运行程序以及windows安装包
vue.js·windows·spring boot
shark222222215 小时前
能懂!基于Springboot的用户增删查改(三层设计模式)
spring boot·后端·设计模式
IGAn CTOU15 小时前
王炸级更新!Spring Boot 3.4 正式发布,新特性真香!
java·spring boot·后端
K3v17 小时前
【git】删除本地以及远端已经合并到master的分支
大数据·git·elasticsearch