Spring Data ElasticSearch 基本使用
[Lucene 全文检索](()
[ElasticSearch 概述](()
[ElasticSearch 客户端操作](()
[IK 分词器和 ElasticSearch 集成使用](()
[ElasticSearch 集群的概念及搭建过程](()
[使用 Java 语言操作索引库](()
[Spring Data ElasticSearch 基本使用](()
[](()Spring Data ElasticSearch 简介
Spring Data ElasticSearch: Spring Data API 简化了 ElasticSearch 操作,将原始操作 elasticSearch 的客户端 API 进行封装,Spring Data 为 Elasticsearch 项目提供集成搜索引擎。Spring Data Elasticsearch POJO 的关键功能区域为中心的模型与 Elastichsearch 交互文档和轻松地编写一个存储库数据访问层。
[](()环境搭建
项目结构:
1.导入相关 pom.xml 坐标
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>5.6.8</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>5.6.8</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.8.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.8.1</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-elasticsearch</artifactId>
<version>3.0.5.RELEASE</version>
<exclusions>
<exclusion>
<groupId>org.elasticsearch.plugin</groupId>
<artifactId>transport-netty4-client</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.4.RELEASE</version>
</dependency>
2. 创建 applicationContext.xml 配置文件,并进行相关配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:elasticsearch="http://www.springframework.org/schema/data/elasticsearch"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/data/elasticsearch
http://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch-1.0.xsd
">
<elasticsearch:repositories base-package="com.ly.repositories"/>
<elasticsearch:transport-client id="client" cluster-nodes="localhost:9301,localhost:9302,localhost:9303" cluster-name="my-elasticsearch"/>
<bean id="elasticsearchTemplate" class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate">
<constructor-arg name="client" ref="client"></constructor-arg>
</bean>
</beans>
3. 编写并配置实体 Article
基于 spring data elasticsearch 注解配置索引、映射和实体的关系
@Document(indexName="index_blog",type="article")
: indexName:索引名称(必项)、 type:索引类型@Id
:主键的唯一标识@Field(type = FieldType.text,index=true,analyzer="ik_smart",store=true,searchAnalyzer="ik_smart")
type: 数据类型
index:是否设置分词
store:是否存储
analyzer:存储时使用的分词器
searchAnalyze:搜索时使用的分词器
@Document(indexName="index_blog",type = "article")
public class Article {
@Id
@Field(type = FieldType.Long,store = true)
private long id;
@Field(type = FieldType.text,store = true,analyzer = "ik_smart")
private String title;
@Field(type = FieldType.text,store = true,analyzer = "ik_smart")
private String content;
public long getId() {
return id;
}
public void setId(long 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;
}
@Override
public String toString() {
return "Article{" +
"id=" + id +
", title='" + title + ''' +
", content='" + content + ''' +
'}';
}
}
4. 编写 Repository 接口
ArticleRepository 属于 Dao 层(持久层),我们只需要实现其接口,并在 xml 文件中进行相应配置,spring 就会自动帮我们生成对象,在生成这个接口后我们可以不用实现方法而直接使用,因为 Repository 接口需要继承 ElasticsearchRepository<T, ID>接口,在这个里已经封装了大量的基础操作,我们可以很方便的操作 ElasticSearch 的数据。
代码实现:
public interface ArticleRepository extends ElasticsearchRepository<Article,Long> {
}
5.定义测试类
ElasticsearchTemplate 是 Spring 对 ES 的 java api 进行的封装,提供了大量的相关的类来完成各种各样的查询。
/**
@Author: Ly
@Date: 2020-11-25 22:23
*/
//添加相关配置,在 spring 中测试代码
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class SpringDataElasticSearchTest {
@Autowired
private ArticleRepository articleRepository;
@Autowired
private ElasticsearchTemplate template;
@Test
public void createIndex() throws Exception{
//创建索引,并配置映射关系
template.createIndex(Article.class);
//配置映射关系,上面的语句已经配置过索引了,不需要再配置
//template.putMapping(Article.class);
}
}
[](()Spring Data ElasticSearch 的常用操作
1. 添加文档方法测试
@Test
public void addDocument() throws Exception{
//创建 Article 对象
Article article =new Article();
article.setId(1);
article.setTitle("Elasticsearch 简介 1");
评论