目录
1. Spring Data ElasticSearch简介
(1)什么是Spring Data
定义 | 目的 | 功能 |
---|---|---|
Spring Data是一个用于简化数据库访问,并支持云服务的开源框架。 | 使得对数据的访问变得方便快捷,并支持map-reduce框架和云计算数据服务 | 极大的简化JPA的写法,可以在几乎不用写实现 的情况下,实现对数据的访问和操作。除了CRUD外,还包括如分页、排序等一些常用的功能。 |
(2)什么是Spring Data ElasticSearch
Spring Data ElasticSearch 基于 spring data API 简化 elasticSearch操作,将原始操作elasticSearch的客户端API 进行封装
。Spring Data为Elasticsearch项目提供集成搜索引擎。Spring Data Elasticsearch POJO的关键功能区域 为中心的模型与Elastichsearch交互文档和轻松地编写一个存储库数据访问层。
Spring Data ElasticSearch官网
2. Spring Data ElasticSearch常用操作
(1)整体步骤
(2)具体实现
① 导入es相关jar包
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<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>org.apache.logging.log4j</groupId>
<artifactId>log4j-to-slf4j</artifactId>
<version>2.9.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.24</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.21</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.12</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>
</dependencies>
</project>
② es相关配置
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
">
<!--elastic客户端对象的配置-->
<elasticsearch:transport-client id="esClient" cluster-name="my-elasticsearch"
cluster-nodes="127.0.0.1:9301,127.0.0.1:9302,127.0.0.1:9303"/>
<!--配置包扫描器,扫描dao接口-->
<elasticsearch:repositories base-package="com.dynamic.es.repositories"/>
<!--es的模板对象-->
<bean id="elasticsearchTemplate"
class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate">
<constructor-arg name="client" ref="esClient"/>
</bean>
</beans>
③ 创建实体
Article.java
//indexName :创建的索引的名称,type :索引的类型
@Document(indexName = "spes",type ="article")
public class Article {
//主键的唯一标识
@Id
// type: 数据类型 store:是否存储 (index:是否设置分词 analyzer:存储时使用的分词器 searchAnalyze:搜索时使用的分词器)
@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;
//此处省略get和set方法和序列化
}
④ 自定义接口
ArticleRepository.java
public interface ArticleRepository extends ElasticsearchRepository<Article,Long> {
//根据标题查询
List<Article> findByTitle(String title);
//根据标题/内容查询
List<Article> findByTitleOrContent(String title, String content);
//根据标题/内容查询(含分页)
List<Article> findByTitleOrContent(String title, String content, Pageable pageable);
}
⑤ 测试方法
SpringDataElasticSearchText .java
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class SpringDataElasticSearchText {
@Autowired
private ArticleRepository articleRepository;
@Autowired
private ElasticsearchTemplate template;
//创建索引
@Test
public void createIndex() throws Exception{
// 创建索引,并配置映射关系
template.createIndex(Article.class);
}
//==========================增==========================
//把文档内容写入索引库
@Test
public void addDocument() throws Exception {
for (int i = 10; i <= 20; i++) {
// 创建一个Article对象
Article article = new Article();
article.setId(i);
article.setTitle("涂山红红"+i);
article.setContent("涂山红红"+i);
// 把文档内容写入索引库
articleRepository.save(article);
}
}
//==========================删==========================
//根据Id删除某条数据
@Test
public void deleteDocumentById() throws Exception{
articleRepository.deleteById(2l);
}
//==========================查(调用自定义的接口)==========================
//根据标题查询某条数据
@Test
public void testFindByTitle() throws Exception{
List<Article> list = articleRepository.findByTitle("涂山红红");
list.stream().forEach(a-> System.out.println(a));
}
//根据标题/内容查询某条数据(含分页)
@Test
public void testFindByTitleOrContext() throws Exception{
PageRequest pageable = PageRequest.of(0,15);
articleRepository.findByTitleOrContent("斗罗大陆","涂山红红",pageable)
.forEach(a-> System.out.println(a));
}
//==========================查(调用封装的方法)==========================
//查询所有内容
@Test
public void findAll() throws Exception{
Iterable<Article> articles = articleRepository.findAll();
articles.forEach(a-> System.out.println(a));
}
//根据id查询某条数据
@Test
public void testFindById() throws Exception{
Optional<Article> optional = articleRepository.findById(1l);
Article article = optional.get();
System.out.println(article);
}
//自定义查询
@Test
public void testNativeSearchQuery() throws Exception {
// 创建一个查询对象
NativeSearchQuery query = new NativeSearchQueryBuilder()
.withQuery(QueryBuilders.queryStringQuery("斗罗大陆赶紧更新吧").defaultField("title"))
.withPageable(PageRequest.of(0,15))
.build();
// 执行查询
List<Article> articleList = template.queryForList(query, Article.class);
articleList.forEach(a-> System.out.println(a));
}
}
来源:oschina
链接:https://my.oschina.net/u/4389078/blog/4566089