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

}
相关推荐
J_liaty8 小时前
Spring Boot拦截器与过滤器深度解析
java·spring boot·后端·interceptor·filter
极客先躯9 小时前
如何自动提取Git指定时间段的修改文件?Win/Linux双平台解决方案
linux·git·elasticsearch
lpfasd1239 小时前
Spring Boot 4.0.1 时变更清单
java·spring boot·后端
Elastic 中国社区官方博客11 小时前
Elastic:DevRel 通讯 — 2026 年 1 月
大数据·数据库·人工智能·elasticsearch·搜索引擎·ai·全文检索
千寻技术帮14 小时前
10341_基于Springboot的珠宝销售网站
spring boot·mysql·毕业设计·商城·珠宝商城
Elastic 中国社区官方博客14 小时前
jina-embeddings-v3 现已在 Elastic Inference Service 上可用
大数据·人工智能·elasticsearch·搜索引擎·ai·jina
Elastic 中国社区官方博客14 小时前
使用 jina-embeddings-v3 和 Elasticsearch 进行多语言搜索
大数据·数据库·人工智能·elasticsearch·搜索引擎·全文检索·jina
Albert Edison15 小时前
【Git】多人协作二(不同分支下)
git·elasticsearch·svn·github
一只叫煤球的猫15 小时前
为什么Java里面,Service 层不直接返回 Result 对象?
java·spring boot·面试
小当家.10516 小时前
从零构建项目认知:如何画出一张合格的系统架构图(以供应链系统为例)
java·spring boot·学习·架构·系统架构·供应链·实习