0.注意事项

SpringDataElasticSearch可能和远程的ElasticSearch版本不匹配,会宝座

版本适配说明:https://github.com/spring-projects/spring-data-elasticsearch

如果版本不适配:2.4.6

  1)、升级SpringBoot版本(不推荐)

  2)、安装对应版本的ES(推荐)

我这儿是又下了一个ES,docker pull 很快的

1.配置pom.xml文件

  1. <!--SpringBoot默认使用SpringData ElasticSearch模块进行操作-->
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
  5. </dependency>

2.配置application.properties

  1. spring.data.elasticsearch.cluster-name=elasticsearch
  2. spring.data.elasticsearch.cluster-nodes=tanghu.tk:9301

3.通过实现ElasticSearchRepository接口操作ES

1)、编写一个ElasticSearchRepository

  1. package com.fdzang.mblog.repository;
  2.  
  3. import com.fdzang.mblog.pojo.es.EsBlog;
  4. import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
  5.  
  6. public interface EsBlogRepository extends ElasticsearchRepository<EsBlog,String> {
  7. }

2)、给实体类加上@Document注解,指定index跟type

  1. package com.fdzang.mblog.pojo.es;
  2.  
  3. import org.springframework.data.annotation.Id;
  4. import org.springframework.data.elasticsearch.annotations.Document;
  5.  
  6. @Document(indexName = "thblog",type = "blog")
  7. public class EsBlog {
  8. @Id // 主键
  9. private String id;
  10. private Long blogId; // Blog 的 id
  11. private String title;
  12. private String summary;
  13. private String content;
  14.  
  15. public String getId() {
  16. return id;
  17. }
  18.  
  19. public void setId(String id) {
  20. this.id = id;
  21. }
  22.  
  23. public Long getBlogId() {
  24. return blogId;
  25. }
  26.  
  27. public void setBlogId(Long blogId) {
  28. this.blogId = blogId;
  29. }
  30.  
  31. public String getTitle() {
  32. return title;
  33. }
  34.  
  35. public void setTitle(String title) {
  36. this.title = title;
  37. }
  38.  
  39. public String getSummary() {
  40. return summary;
  41. }
  42.  
  43. public void setSummary(String summary) {
  44. this.summary = summary;
  45. }
  46.  
  47. public String getContent() {
  48. return content;
  49. }
  50.  
  51. public void setContent(String content) {
  52. this.content = content;
  53. }
  54. }

3)、测试类

  1. @Autowired
  2. EsBlogRepository esBlogRepository;
  3.  
  4. @Test
  5. public void test02(){
  6. EsBlog esBlog=new EsBlog();
  7. esBlog.setBlogId(10001l);
  8. esBlog.setId("10001");
  9. esBlog.setContent("content");
  10. esBlog.setSummary("summary");
  11. esBlog.setTitle("title");
  12. esBlogRepository.index(esBlog);
  13. }

注:ElasticSearchRepository也支持自定义方法(遵循Repository的方法命名规则)

  同时也支持@Query注解

文档地址:https://docs.spring.io/spring-data/elasticsearch/docs/3.0.10.RELEASE/reference/html/

SpringBoot整合ElasticSearch:基于SpringDataElasticSearch的更多相关文章

  1. springboot整合elasticsearch(基于es7.2和官方high level client)

    前言 最近写的一个个人项目(传送门:全终端云书签)中需要用到全文检索功能,目前 mysql,es 都可以做全文检索,mysql 胜在配置方便很快就能搞定上线(参考这里),不考虑上手难度,es 在全文检 ...

  2. SpringBoot整合ElasticSearch实现多版本的兼容

    前言 在上一篇学习SpringBoot中,整合了Mybatis.Druid和PageHelper并实现了多数据源的操作.本篇主要是介绍和使用目前最火的搜索引擎ElastiSearch,并和Spring ...

  3. ElasticSearch(2)---SpringBoot整合ElasticSearch

    SpringBoot整合ElasticSearch 一.基于spring-boot-starter-data-elasticsearch整合 开发环境:springboot版本:2.0.1,elast ...

  4. 😊SpringBoot 整合 Elasticsearch (超详细).md

    SpringBoot 整合 Elasticsearch (超详细) 注意: 1.环境搭建 安装es Elasticsearch 6.4.3 下载链接 为了方便,环境使用Windows 配置 解压后配置 ...

  5. springboot整合elasticsearch入门例子

    springboot整合elasticsearch入门例子 https://blog.csdn.net/tianyaleixiaowu/article/details/72833940 Elastic ...

  6. Springboot整合elasticsearch以及接口开发

    Springboot整合elasticsearch以及接口开发 搭建elasticsearch集群 搭建过程略(我这里用的是elasticsearch5.5.2版本) 写入测试数据 新建索引book( ...

  7. SpringBoot整合Elasticsearch详细步骤以及代码示例(附源码)

    准备工作 环境准备 JAVA版本 java version "1.8.0_121" Java(TM) SE Runtime Environment (build 1.8.0_121 ...

  8. Springboot整合Elasticsearch报错availableProcessors is already set to [4], rejecting [4]

    Springboot整合Elasticsearch报错 今天使用SpringBoot整合Elasticsearch时候,相关的配置完成后,启动项目就报错了. nested exception is j ...

  9. Springboot整合ElasticSearch进行简单的测试及用Kibana进行查看

    一.前言 搜索引擎还是在电商项目.百度.还有技术博客中广泛应用,使用最多的还是ElasticSearch,Solr在大数据量下检索性能不如ElasticSearch.今天和大家一起搭建一下,小编是看完 ...

  10. SpringBoot整合elasticsearch

    在这一篇文章开始之前,你需要先安装一个ElasticSearch,如果你是mac或者linux可以参考https://www.jianshu.com/p/e47b451375ea,如果是windows ...

随机推荐

  1. ubuntu gcc 降级 适应matlab

    一.安装gcc 4.7 Ubuntu14.04自带的gcc版本是4.8,MATLAB2014a支持的最高版本为4.7x.因此,需要安装gcc4.7,并给gcc降级 在终端执行gcc 4.7的安装命令: ...

  2. oracel 查询语句

    1.以左边为显示表,从其他表查询关联的数据(多表查询) select a.*,b.name from da as a left join db as b on a.id = b.pid left jo ...

  3. spring boot打包,依赖、配置文件分离,拷贝启动脚本

    一.最终打包的目录结构 二.项目结构 三.开始 1.最终打包的目录,可根据自己需要修改. <properties> <mzservice.path>${project.buil ...

  4. 删除链表的倒数第 n 个节点

    难度: 中等 leetcode地址: https://leetcode.com/problems/remove-nth-node-from-end-of-list/description/ 分析: 1 ...

  5. number与string的转换

    // number -> string // toString() /* var num = 10; var res = num.toString(); alert(typeof (num)); ...

  6. requests模块 简单使用

    目录 requests模块 简单使用 Anaconda简单了解 简单了解 requests模块 使用requests模块 爬取搜狗首页源码数据 实现一个简易的网页采集器 解决乱码问题 解决UA检测问题 ...

  7. 用js写的简单的下拉菜单

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. 各种变异绕过XSS过滤器

    各种变异绕过XSS过滤器(Various variations bypass the XSS filter ) 文章来自:https://www.cnblogs.com/iAmSoScArEd/p/1 ...

  9. 分享linux系统more基本命令python源码

    此python源码是linux系统more基本命令的实现. 实现linux中more的基本功能,当more后加一个文件名参数时候,分屏显示按空格换页,按回车换行',在左下角显示百分比; 以处理管道参数 ...

  10. 【大数据技术能力提升_2】numpy学习

    numpy学习 标签(空格分隔): numpy python 数据类型 5种类型:布尔值(bool),整数(int),无符号整数(uint).浮点(float).复数(complex) 支持的原始类型 ...