使用方法
一、把SpringDataElasticSearch整合Spring
- 创建一个TransportClient对象,在容器中单例。
- 创建一个ElasticSearchTemplate模板对象。
- Dao的包扫描器。
二、创建实体类。配置映射关系。
对应索引库中的type配置映射关系。
@Document
: 配置实体类和索引库中的type的映射关系
indexName:索引库的名称
type:索引库中type的名称@Id
:标注id属性@Field
:配置字段的属性
type:字段的数据类型
store:是否存储,默认是false
index:是否索引,默认是true
analyzer:配置分词器
三、dao。
创建一个接口,需要继承ElasticSearchRepository接口。
框架整合
一、添加依赖
<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>
二、添加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">
<!--配置TransportClient对象-->
<elasticsearch:transport-client id="client"
cluster-name="my-elasticsearch"
cluster-nodes="127.0.0.1:9301,127.0.0.1:9302,127.0.0.1:9303"/>
<!--配置ElasticSearchTemplate对象-->
<bean id="template" class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate">
<constructor-arg name="client" ref="client"/>
</bean>
<!--配置dao的包扫描器-->
<elasticsearch:repositories base-package="com.itheima.es.dao"
elasticsearch-template-ref="template"/>
</beans>
实体类的配置
注意:最好使用一个新的索引库
/**
* @Document: 配置实体类和索引库中的type的映射关系
* indexName:索引库的名称
* type:索引库中type的名称
*/
@Document(indexName = "blog2", type = "article")
public class Article {
@Id
@Field(type = FieldType.Long, store = true)
private long id;
@Field(type = FieldType.text, store = true, analyzer = "ik_max_word")
private String title;
@Field(type = FieldType.text, store = true, analyzer = "ik_max_word")
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 + '\'' +
'}';
}
}
索引库管理
首先,创建dao,继承ElasticsearchRepository接口
public interface ArticleDao extends ElasticsearchRepository<Article, Long> {
}
创建索引库
createIndex
方法
参数类型:
索引库名称或实体类的类型
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class SpringDataESTest {
@Autowired
private ElasticsearchTemplate template;
@Test
public void createIndex() {
//template.createIndex("hello");
template.createIndex(Article.class);
}
}
设置mapping
putMaping
方法,需要实体类作为参数
因为刚才新建了一个空的索引库hello,所以创建实体类article2,映射hello的type信息
/**
* @Document: 配置实体类和索引库中的type的映射关系
* indexName:索引库的名称
* type:索引库中type的名称
*/
@Document(indexName = "hello", type = "article")
public class Article {
@Id
@Field(type = FieldType.Long, store = true)
private long id;
@Field(type = FieldType.text, store = true, analyzer = "ik_max_word")
private String title;
@Field(type = FieldType.text, store = true, analyzer = "ik_max_word")
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 + '\'' +
'}';
}
}
给hello索引库设置mapping信息
@Test
public void putMaping() {
template.putMapping(Article2.class);
}
删除索引库
deleteIndex
方法,以索引库名称为参数
@Test
public void deleteIndex() {
template.deleteIndex("hello");
}
文档管理
直接使用dao对象的方法实现增删改查
首先,注入dao
@Autowired
private ArticleDao articleDao;
添加/修改文档
save
方法
注意:框架会自动将article的id和_id保持一致!
@Test
public void addDocument() {
for (int i = 0; i < 100; i++) {
//创建Article对象
Article article = new Article();
article.setId(i);
article.setTitle("Lucene介绍与入门使用 - 高压锅里的小白" + i);
article.setContent("apache软件基金会4 jakarta项目组的一个子项目,是一" + i);
//保存到ES
articleDao.save(article);
}
}
删除文档
deleteById
方法,根据id
删除文档
@Test
public void deleteDocument() {
articleDao.deleteById(1l);
}
查询文档
一、根据id查询
直接使用dao的findById
方法查询
注意:Optional 类是一个可以为null的容器对象。如果值存在则isPresent()方法会返回true,调用get()方法会返回该对象。
@Test
public void findById() {
Optional<Article> optional = articleDao.findById(1l);
if (optional.isPresent()) {
Article article = optional.get();
System.out.println(article);
}
}
二、查询全部文档,且分页
findAll
方法,默认不分页
@Test
public void findAll() {
Page<Article> page = articleDao.findAll(PageRequest.of(0, 5));
long totalElements = page.getTotalElements();
System.out.println(totalElements);
int totalPages = page.getTotalPages();
//取结果列表
List<Article> content = page.getContent();
System.out.println(totalPages);
for (Article article : content) {
System.out.println(article);
}
}
三、使用方法命名规则查询
- 相当于term查询方式。
- 默认是分页的,每页显示10条数据。
- 如果自定义分页,应该使用Pageable参数。
- 类似于模糊查询,类似于sql中的like
修改dao
public interface ArticleDao extends ElasticsearchRepository<Article, Long> {
List<Article> findByTitle(String title);
}
@Test
public void testfindByTitle() {
List<Article> list = articleDao.findByTitle("Lucene");
for (Article article : list) {
System.out.println(article);
}
}
自定义分页,添加Pageable参数
public interface ArticleDao extends ElasticsearchRepository<Article, Long> {
Page<Article> findByTitle(String title, Pageable pageable);
}
@Test
public void testfindByTitle() {
Page<Article> page = articleDao.findByTitle("Lucene介绍与入门使用", PageRequest.of(0, 5));
long totalElements = page.getTotalElements();
System.out.println(totalElements);
int totalPages = page.getTotalPages();
System.out.println(totalPages);
List<Article> content = page.getContent();
for (Article article : content) {
System.out.println(article);
}
}
四、根据QueryString查询
dao中没有对应的方法,需要使用NativeSearchQuery
对象创建查询条件,再使用Template对象执行查询。
@Test
public void testQueryStringQuery() {
//创建一个查询条件
NativeSearchQuery searchQuery = new NativeSearchQueryBuilder()
.withQuery(QueryBuilders.queryStringQuery("lucene是一个全文检索api"))
//设置分页条件
.withPageable(PageRequest.of(0, 20))
.build();
//使用Template对象执行查询
List<Article> articles = template.queryForList(searchQuery, Article.class);
for (Article article : articles) {
System.out.println(article);
}
}