一、Spring Data?
Spring Data是⼀个⽤于简化数据库访问,并⽀持云服务的开源框架。其主要⽬标是使得对数据的访问变
得⽅便快捷,并⽀持map-reduce框架和云计算数据服务。 Spring Data可以极⼤的简化JPA的写法,可
以在⼏乎不⽤写实现的情况下,实现对数据的访问和操作。除了CRUD外,还包括如分页、排序等⼀些
常⽤的功能。官网入口:
Spring Data的官⽹
Spring Data常⽤的功能模块如下:
二、Spring Data ElasticSearch
Spring Data ElasticSearch 基于 spring data API 简化 elasticSearch操作,将原始操作elasticSearch的客
户端API 进⾏封装 。Spring Data为Elasticsearch项⽬提供集成搜索引擎。Spring Data Elasticsearch
POJO的关键功能区域为中⼼的模型与Elastichsearch交互⽂档和轻松地编写⼀个存储库数据访问层。
官网入口:
官⽅⽹站文章来源:https://www.toymoban.com/news/detail-418619.html
三、⼊门案例
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.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.alisinngai</groupId>
<artifactId>es_demo2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>es_demo2</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
2)启动器配置⽂件
package com.alisinngai.dv;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class EsDemo2Application {
public static void main(String[] args) {
SpringApplication.run(EsDemo2Application.class, args);
}
}
# 配置文件
spring:
data:
elasticsearch:
cluster-name: my-elasticsearch
cluster-nodes: 127.0.0.1:9300
3) 编写实体Article
package com.alisinngai.dv.domain;
import lombok.Data;
import lombok.EqualsAndHashCode;
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;
@Data
@Document(indexName = "es_blog",type = "article")
@EqualsAndHashCode
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 + 属性 + 关键词 + 连接符
文章来源地址https://www.toymoban.com/news/detail-418619.html
package com.alisinngai.dv.repositories;
import com.alisinngai.dv.domain.Article;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.repository.ElasticsearchCrudRepository;
import java.util.List;
public interface ArticleRepository extends ElasticsearchCrudRepository<Article,Long> {
// ⽅法命名规则查询的基本语法findBy + 属性 + 关键词 + 连接符(属性 + 关键词 + 连接符)
List<Article> findByTitleOrContent(String title,String content);
List<Article> findByTitle(String title);
List<Article> findByTitleOrContent(String title, String content, Pageable pageable);
List<Article> findByTitleLike(String title);
}
6)创建测试类
package com.alisinngai.dv;
import com.alisinngai.dv.domain.Article;
import com.alisinngai.dv.repositories.ArticleRepository;
import org.elasticsearch.index.query.QueryBuilders;
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.domain.Sort;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.test.context.junit4.SpringRunner;
import javax.swing.*;
import java.io.Reader;
import java.util.List;
@SpringBootTest
@RunWith(SpringRunner.class)
public class EsDemo2ApplicationTests {
@Autowired
private ArticleRepository articleRepository;
@Autowired
private ElasticsearchTemplate template;
@Test
public void contextLoads() {
}
@Test
public void createIndex() throws Exception {
//配置映射关系
template.putMapping(Article.class);
//创建索引,并配置映射关系
template.createIndex(Article.class);
}
@Test
public void deleteIndex() {
template.deleteIndex("es_blog");
}
//添加测试数据
@Test
public void addDocument() throws Exception {
for (int i = 0; i < 100; i++) {
//创建对象
Article article = new Article();
article.setId(i+1);
article.setTitle("女护士路遇昏迷男子跪地抢救: 救人是职责更是本能" + i);
article.setContent("这是一个美丽的女护士妹妹 " + i);
//把文档写入索引库
articleRepository.save(article);
}
}
@Test
public void deleteById(){
articleRepository.deleteById(4L);
}
@Test
public void findAll(){
Iterable<Article> all = articleRepository.findAll();
all.forEach(System.out :: println);
}
/***
* 关键字中有like则表示分词,否则不分词,列如:
* findByTitleLike() : 分词
* findByTitle() : 不分词
*/
@Test
public void findByTitle(){
List<Article> list = articleRepository.findByTitle("女护士");
list.forEach(System.out :: println);
}
@Test
public void findByTitleLike(){
List<Article> list = articleRepository.findByTitleLike("美丽的女护士");
list.forEach(System.out :: println);
}
@Test
public void findByTitleOrContent(){
List<Article> byTitleOrContent = articleRepository.findByTitleOrContent("女护士", "女护士");
byTitleOrContent.forEach(System.out :: println);
}
@Test
public void findByTitleOrContentPage(){
PageRequest of = PageRequest.of(0, 10);
List<Article> list = articleRepository.findByTitleOrContent("女护士", "女护士", of);
list.forEach(System.out :: println);
}
//原生的dsl语句
@Test
public void testNativeSearchQuery(){
NativeSearchQuery nativeSearchQuery = new NativeSearchQueryBuilder()
.withQuery(QueryBuilders.queryStringQuery("女护士").defaultField("title"))
.withPageable(PageRequest.of(0, 15))
.build();
List<Article> list = template.queryForList(nativeSearchQuery, Article.class);
list.forEach(res -> System.out.println(res));
}
}
到了这里,关于Spring Data ElasticSearch简介的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!