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

}
相关推荐
鱼鳞_7 分钟前
苍穹外卖-Day01(开发环境搭建)
java·spring boot·spring·maven
学不思则罔10 分钟前
SpringBoot启动失败排查指南
spring boot·后端·部署
夕除29 分钟前
spring boot 5
数据库·spring boot·后端
yoyo_zzm1 小时前
ThinkPHP1.X核心特性解析
数据库·spring boot·nginx
hexu_blog1 小时前
前端vue后端java+springboot如何实现pdf,word,excel之间的相互转换
java·前端·vue.js·spring boot·文档转换
逸Y 仙X1 小时前
文章二十九:ElasticSearch分桶聚合
android·大数据·elasticsearch·搜索引擎·全文检索
倒流时光三十年2 小时前
第9篇 消息不丢:三端协同防丢失方案
spring boot·kafka
Devin~Y2 小时前
大厂Java面试实录:Spring Boot/WebFlux、JVM调优、Redis/Kafka、Spring Cloud 与 RAG/Agent 追问
java·jvm·spring boot·maven·mybatis·jpa·spring webflux
一轮弯弯的明月2 小时前
Spring AOP编程
java·开发语言·spring boot·笔记·spring aop·学习心得
Boop_wu2 小时前
[Java项目] Spring Boot + WebSocket 实现网页在线聊天室|完整项目架构与实战讲解
spring boot·websocket·java-ee·mybatis