文章目录
一、Spring Data简介
Spring Data是一个用于简化数据库访问,并支持云服务的开源框架。其主要目标是使得对数据的访问变得方便快捷,并支持map-reduce框架和云计算数据服务。 Spring Data可以极大的简化JPA的写法,可以在几乎不用写实现的情况下,实现对数据的访问和操作。除了CRUD外,还包括如分页、排序等一些常用的功能。
二、Spring Data ElasticSearch简介
Spring Data ElasticSearch 基于 spring data API 简化 elasticSearch操作,将原始操作elasticSearch的客户端API 进行封装 。Spring Data为Elasticsearch项目提供集成搜索引擎。Spring Data Elasticsearch POJO的关键功能区域为中心的模型与Elastichsearch交互文档和轻松地编写一个存储库数据访问层。
三、基础入门案例
1.导入Spring Data ElasticSearch坐标
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.16.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<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>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.启动器配置文件
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
spring:
data:
elasticsearch:
cluster-name: my-elasticsearch
cluster-nodes: 192.168.220.100:9300
3.编写实体Article
@Document(indexName = "lxs_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;
。。。
}
4.编写Dao
方法命名规则查询的基本语法findBy + 属性 + 关键词 + 连接符 + 属性 + 关键词。。。
方法中使用like才会分词
package com.lxs.esdemo2.dao;
import com.lxs.esdemo2.domain.Article;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import java.util.List;
public interface ArticleDao extends ElasticsearchRepository<Article, Long> {
public List<Article> findByTitleLike(String title);
public List<Article> findByTitle(String title);
public List<Article> findByTitleLikeOrContent(String title, String content);
public List<Article> findByTitleOrContent(String title, String content, Pageable pageable);
}
5.创建测试类
package com.lxs.esdemo2;
import com.lxs.esdemo2.dao.ArticleDao;
import com.lxs.esdemo2.domain.Article;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.terms.StringTerms;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage;
import org.springframework.data.elasticsearch.core.query.FetchSourceFilter;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest
public class EsDemo2ApplicationTests {
@Autowired
private ArticleDao dao;
@Autowired
private ElasticsearchTemplate template;
@Test
public void createIndex() {
// template.createIndex(Article.class);
//配置mapping
template.putMapping(Article.class);
}
@Test
public void addDocument() throws Exception {
for (int i = 1; i <= 20; i++) {
//创建一个Article对象
Article article = new Article();
article.setId(new Long(i));
article.setTitle("女护士路遇昏迷男子跪地抢救:救人是职责更是本能" + i);
article.setContent("这是一个美丽的女护士妹妹" + i);
//把文档写入索引库
dao.save(article);
}
}
@Test
public void deleteDocumentById() throws Exception {
dao.deleteById(3l);
//全部删除
// articleRepository.deleteAll();
}
@Test
public void findAll() {
dao.findAll().forEach(System.out :: println);
}
@Test
public void findById() {
System.out.println(dao.findById(1l));
}
@Test
public void findByTitle() {
dao.findByTitleLike("美丽女护士").forEach(System.out :: println);
}
@Test
public void findByTitleOrContent() {
dao.findByTitleLikeOrContent("美丽女护士", "男护士").forEach(System.out :: println);
}
@Test
public void findByTitlePage() {
Pageable pageable = PageRequest.of(1, 5);
dao.findByTitleOrContent("女护士","女护士", pageable).forEach(System.out :: println);
}
@Test
public void testNativeSearchQuery() throws Exception {
NativeSearchQuery query = new NativeSearchQueryBuilder()
.withQuery(QueryBuilders.queryStringQuery("女护士").defaultField("title"))
.withPageable(PageRequest.of(1, 5))
.build();
template.queryForList(query, Article.class).forEach(System.out :: println);
}
}