SpringBoot整合ElasticSearch:基于SpringDataElasticSearch
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的更多相关文章
- springboot整合elasticsearch(基于es7.2和官方high level client)
前言 最近写的一个个人项目(传送门:全终端云书签)中需要用到全文检索功能,目前 mysql,es 都可以做全文检索,mysql 胜在配置方便很快就能搞定上线(参考这里),不考虑上手难度,es 在全文检 ...
- SpringBoot整合ElasticSearch实现多版本的兼容
前言 在上一篇学习SpringBoot中,整合了Mybatis.Druid和PageHelper并实现了多数据源的操作.本篇主要是介绍和使用目前最火的搜索引擎ElastiSearch,并和Spring ...
- ElasticSearch(2)---SpringBoot整合ElasticSearch
SpringBoot整合ElasticSearch 一.基于spring-boot-starter-data-elasticsearch整合 开发环境:springboot版本:2.0.1,elast ...
- 😊SpringBoot 整合 Elasticsearch (超详细).md
SpringBoot 整合 Elasticsearch (超详细) 注意: 1.环境搭建 安装es Elasticsearch 6.4.3 下载链接 为了方便,环境使用Windows 配置 解压后配置 ...
- springboot整合elasticsearch入门例子
springboot整合elasticsearch入门例子 https://blog.csdn.net/tianyaleixiaowu/article/details/72833940 Elastic ...
- Springboot整合elasticsearch以及接口开发
Springboot整合elasticsearch以及接口开发 搭建elasticsearch集群 搭建过程略(我这里用的是elasticsearch5.5.2版本) 写入测试数据 新建索引book( ...
- SpringBoot整合Elasticsearch详细步骤以及代码示例(附源码)
准备工作 环境准备 JAVA版本 java version "1.8.0_121" Java(TM) SE Runtime Environment (build 1.8.0_121 ...
- Springboot整合Elasticsearch报错availableProcessors is already set to [4], rejecting [4]
Springboot整合Elasticsearch报错 今天使用SpringBoot整合Elasticsearch时候,相关的配置完成后,启动项目就报错了. nested exception is j ...
- Springboot整合ElasticSearch进行简单的测试及用Kibana进行查看
一.前言 搜索引擎还是在电商项目.百度.还有技术博客中广泛应用,使用最多的还是ElasticSearch,Solr在大数据量下检索性能不如ElasticSearch.今天和大家一起搭建一下,小编是看完 ...
- SpringBoot整合elasticsearch
在这一篇文章开始之前,你需要先安装一个ElasticSearch,如果你是mac或者linux可以参考https://www.jianshu.com/p/e47b451375ea,如果是windows ...
随机推荐
- C# 连接SQLServer数据库自动生成model类代码
Program.cs using System; using System.Collections.Generic; using System.Linq; using System.Threading ...
- 7.1 为什么bulk使用奇特的json格式?
回顾: bulk的语法要求:每个json串都不能换行,不同json串之间,必须换行 为什么不用标准json数组呢?如: [ { "create":{.. ...
- iOS - Base64转图片&&图片转Base64
记录一个小功能 app传base64位上去,服务器拿到后转图片保存,当app请求拿回用户图片时,服务器再把图片转base64字符串返回给app,app再转图片 // 64base字符串转图片 - (U ...
- centos下安装nginx(转载)
http://blog.csdn.net/u010246789/article/details/51501710 有声明,不能转载,所以,就把地址弄了过来
- Coldfusion Sql查询分组输出
<cfoutput query="myQry" group="date"> #date# <cfoutput> #detail# < ...
- Vivado中备份设计好的block design
参考链接 https://blog.csdn.net/dimples_song/article/details/81391615 前言 为了不每次都重新生成block design,避免重复劳动. 可 ...
- Oracle 多表插入
多表插入 作用:一条INSERT语句可以完成向多张表的插入任务(Multitable insert).有两种形式:insert all与insert first,准备测试环境:1.创建表T并初始化测试 ...
- Flask-SQLAlchemy操作指南
Flask-SQLAlchemy官方文档 from flask_sqlalchemy import SQLAlchemy app = Flask(__name__)app.config['SQLALC ...
- 开发一个代码的自动生成器,使用Jfinal4.3+Swagger+Sql
-- 所有表名select column_name 列名, data_type 字段类型, column_comment 字段注释 from information_schema.columns ...
- java 实现 单链表
class Node{ public int val; public Node next; public Node(int val){ this.val=val; } } class LinkList ...