当前位置: 首页 > news >正文

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

}

相关文章:

  • C++ Json-Rpc框架-3项目实现(2)
  • Codeforces Round 1017 (Div. 4)题解
  • 明远智睿SSD2351核心板助力智能监控系统升级
  • 软件架构设计风格
  • 【C++ 】智能指针:内存管理的 “自动导航仪”
  • 最短路径介绍
  • 【论文解读】MSVM-UNet: Multi-Scale Vision Mamba UNet for Medical Image Segmentation
  • 机器学习项目二:帕金森病检测
  • 【C++教程】使用printf语句实现进制转换
  • 前端面试题(八):简述Vue2的响应式原理
  • mysql自动赋值
  • C语言题目自增在前与在后
  • CentOS服务器能ping通却无法yum install:指定镜像源解决
  • 在思科模拟器show IP route 发现Gateway of last resort is not set没有设置最后的通道
  • One-Hot标签编码方法详解
  • 机器学习概述自用笔记(李宏毅)
  • mysql DQL
  • 「小推桌面官方下载」小推桌面TV版-安卓电视版官方免费下载新版
  • 【KWDB创作者计划】_KWDB部署与使用详细版本
  • 批量将多个文件夹转成压缩包,支持设置压缩密码
  • 走访中广核风电基地:701台风机如何乘风化电,点亮3000万人绿色生活
  • 广州海关原党委委员、副关长刘小威被开除党籍
  • 关键词看中国经济“一季报”:稳,开局良好看信心
  • 谁将主导“视觉大脑”?中国AI的下一个超级赛道
  • VR数字沉浸体验又添新节目,泰坦尼克号驶进文旅元宇宙
  • 泽连斯基与特朗普进行简短会谈