目录
1、前言
首先说明,springboot和elasticsearch的兼容性并不是很好,主要是因为elasticsearch的版本之间API差异太大。向后兼容性不强,特别是es6和es7,差异巨大!所以大家一定要注意自己服务器es的版本和springboot的依赖坐标版本对应,如我本地es版本为7,所以我导入的依赖坐标如下:
es7后操作模板使用ElasticsearchRestTemplate,已经集成了高级API!
2、集成准备工作
2.1 依赖坐标
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
2.2 数据实体
package com.ydt.springboot.elasticsearch.domain;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
//@Document 文档对象 (索引信息、文档类型 )
/*Spring Data通过注解来声明字段的映射属性,有下面的三个注解:
- `@Document` 作用在类,标记实体类为文档对象,一般有四个属性
- indexName:对应索引库名称
- type:对应在索引库中的类型 在ElasticSearch7.x中取消了type的概念
- shards:分片数量,默认5
- replicas:副本数量,默认1
-