0.注意事项

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

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

如果版本不适配:2.4.6

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

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

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

1.配置pom.xml文件

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

2.配置application.properties

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

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

1)、编写一个ElasticSearchRepository

package com.fdzang.mblog.repository;

import com.fdzang.mblog.pojo.es.EsBlog;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; public interface EsBlogRepository extends ElasticsearchRepository<EsBlog,String> {
}

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

package com.fdzang.mblog.pojo.es;

import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document; @Document(indexName = "thblog",type = "blog")
public class EsBlog {
@Id // 主键
private String id;
private Long blogId; // Blog 的 id
private String title;
private String summary;
private String content; public String getId() {
return id;
} public void setId(String id) {
this.id = id;
} public Long getBlogId() {
return blogId;
} public void setBlogId(Long blogId) {
this.blogId = blogId;
} public String getTitle() {
return title;
} public void setTitle(String title) {
this.title = title;
} public String getSummary() {
return summary;
} public void setSummary(String summary) {
this.summary = summary;
} public String getContent() {
return content;
} public void setContent(String content) {
this.content = content;
}
}

3)、测试类

    @Autowired
EsBlogRepository esBlogRepository; @Test
public void test02(){
EsBlog esBlog=new EsBlog();
esBlog.setBlogId(10001l);
esBlog.setId("10001");
esBlog.setContent("content");
esBlog.setSummary("summary");
esBlog.setTitle("title");
esBlogRepository.index(esBlog);
}

注: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. Linux系统中五款好用的日志分析工具

    监控网络活动是一项繁琐的工作,但有充分的理由这样做.例如,它允许你查找和调查工作站和连接到网络的设备及服务器上的可疑登录,同时确定管理员滥用了什么.你还可以跟踪软件安装和数据传输,以实时识别潜在问题, ...

  2. 【转载】Sqlserver使用IsNull方法对空字段进行赋值操作

    在Sqlserver的SQL语句查询过程或者编写存储过程以及自定义函数过程中,有时候字段的值为空,如果为空的字段需要赋值一个默认值,可以使用Sqlserver内置系统函数IsNull来给定一个默认值, ...

  3. JavaScript的深浅复制

    JavaScript的深浅复制 为什么有深复制.浅复制? JavaScript中有两种数据类型,基本数据类型如undefined.null.boolean.number.string,另一类是Obje ...

  4. Restful api 返回值重复的问题

    Spring boot全家桶前后端分离的项目,在扩充某一个列表形式的返回值时,发现返回值出现了一批的重复. 正常的数据返回: 数值完全一致只是参数名称区分了大小写,如下图: 推测可能是restful格 ...

  5. Core Animation笔记(变换)

    1.仿射变换 CGAffineTransformMakeScale : CGAffineTransformMakeTranslation CGAffineTransformMakeRotation(C ...

  6. WPF如何设置启动窗口

    在做系统时,我们想在启动时显示自己想显示的界面,和Winform不同的是它有两种方法 1.在App.xaml中 <Application x:Class="WpfApp1.App&qu ...

  7. 前后端分离架构:Web实现前后端分离,前后端解耦

    一.前言 ”前后端分离“已经成为互联网项目开发的业界标杆,通过Tomcat+Ngnix(也可以中间有个Node.js),有效地进行解耦.并且前后端分离会为以后的大型分布式架构.弹性计算架构.微服务架构 ...

  8. VLAN实验3:理解Hybrid接口的应用

    实验环境 实验拓扑图 实验编址 实验步骤1.基本配置按照实验编址为PC配置IP地址,以PC5为例 在PC5与PC1通过ping命令测试,发现通讯正常.(以此为例,其他的我就不一一截图测试了.) 在S1 ...

  9. 2013.4.24 - KDD第六天

    今天早上,中秋给我发了一个压缩包,里面有战德臣的课件,昨天我说我SQL没学好,他说给我发战徳臣课件,然后说我SQL不会的话可以看这个,还有两篇文 章<LDA数学八卦>以及<A Not ...

  10. pandas速查手册(中文版)

    本文翻译自文章:Pandas Cheat Sheet - Python for Data Science 对于数据科学家,无论是数据分析还是数据挖掘来说,Pandas是一个非常重要的Python包.它 ...