欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 科技 > 名人名企 > Spring Boot 集成spring-boot-starter-data-elasticsearch

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

2025/4/17 0:16:41 来源:https://blog.csdn.net/JavaLLU/article/details/147222661  浏览:    关键词: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 {@Idprivate String id;@Field(type = FieldType.Text)private String title;@Field(type = FieldType.Text)private String content;// Getters and Setterspublic 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 {@Autowiredprivate DocumentRepository documentRepository;@Overridepublic Document saveDocument(Document document) {return documentRepository.save(document);}@Overridepublic Iterable<Document> getAllDocuments() {return  documentRepository.findAll();}@Overridepublic Document getDocumentById(String id) {return documentRepository.findById(id).orElse(null);}@Overridepublic List<Document> findByTitle(String title) {return documentRepository.findByTitle(title);}@Overridepublic void deleteDocumentById(String id) {documentRepository.deleteById(id);}}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词