官方网站: https://spring.io/projects/spring-data-elasticsearch

对应 Elasticsearch7.6.2,Spring boot2.3.x 一般可以兼容 Elasticsearch7.x

一、环境搭建

SpringBoot工程依赖坐标:

<?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 http://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.3.6.RELEASE</version>
<relativePath/>
</parent> <groupId>cn.cloud9</groupId>
<artifactId>Spring-Data-ElasticSearch</artifactId>
<version>1.0-SNAPSHOT</version> <properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties> <dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
</dependency>
</dependencies> </project>

  

application.properties配置文件,追加ES的连接地址

# es 服务地址
elasticsearch.host=127.0.0.1
# es 服务端口
elasticsearch.port=9200
# 配置日志级别,开启 debug 日志
logging.level.com.atguigu.es=debug

启动类代码:

package cn.cloud9;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; /**
* @projectName: SpringDataElasticSearch
* @author: cloud9
* @date: 2022年09月06日 09:31
* @version: 1.0
*/
@SpringBootApplication
public class SpringDataEsApplication { public static void main(String[] args) {
SpringApplication.run(SpringDataEsApplication.class, args);
}
}

  

文档PO类代码:

package cn.cloud9.po;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
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; /**
* @projectName: SpringDataElasticSearch
* @author: Cloud9
* @date: 2022年09月06日 09:33
* @version: 1.0
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
@Document(indexName = "shopping", shards = 3, replicas = 1)
public class Product {
//必须有 id,这里的 id 是全局唯一的标识,等同于 es 中的"_id"
@Id
private Long id;//商品唯一标识
@Field(type = FieldType.Text, analyzer = "ik_max_word")
private String title;//商品名称
@Field(type = FieldType.Keyword)
private String category;//分类名称
@Field(type = FieldType.Double)
private Double price;//商品价格
@Field(type = FieldType.Keyword, index = false)
private String images;//图片地址
}

  

DAO接口继承SpringData的EsRepository接口

package cn.cloud9.dao;

import cn.cloud9.po.Product;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Repository; /**
* @projectName: SpringDataElasticSearch
* @author: Cloud9
* @date: 2022年09月06日 09:37
* @version: 1.0
*/
@Repository
public interface ProductDAO extends ElasticsearchRepository<Product, Long> {
}

  

二、测试操作:

默认读取文档PO类加载到ES中,如果没有索引将默认创建

package cn.cloud9;

import cn.cloud9.po.Product;
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.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.test.context.junit4.SpringRunner; /**
* @projectName: SpringDataElasticSearch
* @author: cloud9
* @date: 2022年09月06日 09:38
* @version: 1.0
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class IndexTest {
/* - - - - - 操作索引 - - - - - */
//注入 ElasticsearchRestTemplate
@Autowired
private ElasticsearchRestTemplate esRestTemplate;
//创建索引并增加映射配置
@Test
public void createIndex(){
//创建索引,系统初始化会自动创建索引
System.out.println("创建索引");
}
@Test
public void deleteIndex(){
//创建索引,系统初始化会自动创建索引
boolean flg = esRestTemplate.deleteIndex(Product.class);
System.out.println("删除索引 = " + flg);
}
/* - - - - - 操作索引 - - - - - */ }

  

文档CRUD操作:

package cn.cloud9;

import cn.cloud9.dao.ProductDAO;
import cn.cloud9.po.Product;
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.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.test.context.junit4.SpringRunner; import java.util.ArrayList;
import java.util.List; /**
* @projectName: SpringDataElasticSearch
* @author: Cloud9
* @date: 2022年09月06日 09:48
* @version: 1.0
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class DocumentTest { /* - - - - - 操作文档 - - - - - */
@Autowired
private ProductDAO productDAO; /**
* 新增
*/
@Test
public void save(){
Product product = new Product();
product.setId(2L);
product.setTitle("华为手机");
product.setCategory("手机");
product.setPrice(2999.0);
product.setImages("http://www.atguigu/hw.jpg");
productDAO.save(product);
} //修改
@Test
public void update(){
Product product = new Product();
product.setId(1L);
product.setTitle("小米 2 手机");
product.setCategory("手机");
product.setPrice(9999.0);
product.setImages("http://www.atguigu/xm.jpg");
productDAO.save(product);
}
//根据 id 查询
@Test
public void findById(){
Product product = productDAO.findById(1L).get();
System.out.println(product);
}
//查询所有
@Test
public void findAll(){
Iterable<Product> products = productDAO.findAll();
for (Product product : products) {
System.out.println(product);
}
}
//删除
@Test
public void delete(){
Product product = new Product();
product.setId(1L);
productDAO.delete(product);
}
//批量新增
@Test
public void saveAll(){
List<Product> productList = new ArrayList<>();
for (int i = 0; i < 10; i++) {
Product product = new Product();
product.setId(Long.valueOf(i));
product.setTitle("["+i+"]小米手机");
product.setCategory("手机");
product.setPrice(1999.0+i);
product.setImages("http://www.atguigu/xm.jpg");
productList.add(product);
}
productDAO.saveAll(productList);
}
//分页查询
@Test
public void findByPageable(){
//设置排序(排序方式,正序还是倒序,排序的 id)
Sort sort = Sort.by(Sort.Direction.DESC,"id");
int currentPage=0;//当前页,第一页从 0 开始,1 表示第二页
int pageSize = 5;//每页显示多少条
//设置查询分页
PageRequest pageRequest = PageRequest.of(currentPage, pageSize,sort);
//分页查询
Page<Product> productPage = productDAO.findAll(pageRequest);
for (Product Product : productPage.getContent()) {
System.out.println(Product);
}
} /* - - - - - 操作文档 - - - - - */ }

 

文档查询操作:

package cn.cloud9;

import cn.cloud9.dao.ProductDAO;
import cn.cloud9.po.Product;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
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.test.context.junit4.SpringRunner; /**
* @projectName: SpringDataElasticSearch
* @author: cloud9
* @date: 2022年09月06日 09:50
* @version: 1.0
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class DocumentQueryTest {
@Autowired
private ProductDAO productDAO;
/**
* term 查询
* search(termQueryBuilder) 调用搜索方法,参数查询构建器对象
*/
@Test
public void termQuery(){
TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("title", " 小米");
Iterable<Product> products = productDAO.search(termQueryBuilder);
for (Product product : products) {
System.out.println(product);
}
} /**
* term 查询加分页
*/
@Test
public void termQueryByPage(){
int currentPage= 0 ;
int pageSize = 5;
//设置查询分页
PageRequest pageRequest = PageRequest.of(currentPage, pageSize);
TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("title", " 小米");
Iterable<Product> products = productDAO.search(termQueryBuilder,pageRequest);
for (Product product : products) {
System.out.println(product);
}
}
}

  

  

【ElasticSearch】04 Spring-Data-ElasticSearch的更多相关文章

  1. SprignBoot整合Spring Data Elasticsearch

    一.原生java整合elasticsearch的API地址 https://www.elastic.co/guide/en/elasticsearch/client/java-api/6.2/java ...

  2. Spring Data ElasticSearch的使用

    1.什么是Spring Data Spring Data是一个用于简化数据库访问,并支持云服务的开源框架.其主要目标是使得对数据的访问变得方便快捷,并支持map-reduce框架和云计算数据服务. S ...

  3. elasticsearch系列七:ES Java客户端-Elasticsearch Java client(ES Client 简介、Java REST Client、Java Client、Spring Data Elasticsearch)

    一.ES Client 简介 1. ES是一个服务,采用C/S结构 2. 回顾 ES的架构 3. ES支持的客户端连接方式 3.1 REST API ,端口 9200 这种连接方式对应于架构图中的RE ...

  4. elasticsearch之hello(spring data整合)

    1.书写pom.xml文件 <dependencies> <dependency> <groupId>org.springframework.data</gr ...

  5. Elasticsearch基本用法(2)--Spring Data Elasticsearch

    Spring Data Elasticsearch是Spring Data项目下的一个子模块. 查看 Spring Data的官网:http://projects.spring.io/spring-d ...

  6. Elasticsearch Java client(ES Client 简介、Java REST Client、Java Client、Spring Data Elasticsearch)

    elasticsearch系列七:ES Java客户端-Elasticsearch Java client(ES Client 简介.Java REST Client.Java Client.Spri ...

  7. spring data elasticsearch 使用

    很久之前就安装了elasticsearch,一直没用java用过,最近看了一下spring data系列的elasticsearch,这里写一篇心得. 如果尚未安装elasticsearch,可以 参 ...

  8. Spring Boot + Spring Data + Elasticsearch实例

    Spring Boot + Spring Data + Elasticsearch实例 学习了:https://blog.csdn.net/huangshulang1234/article/detai ...

  9. elasticsearch 拼音+ik分词,spring data elasticsearch 拼音分词

    elasticsearch 自定义分词器 安装拼音分词器.ik分词器 拼音分词器: https://github.com/medcl/elasticsearch-analysis-pinyin/rel ...

  10. How to provide highlighting with Spring data elasticsearch

    How to provide highlighting with Spring data elasticsearch @Test public void shouldReturnHighlighted ...

随机推荐

  1. itest(爱测试) 开源接口测试,敏捷测试管理平台10.0.0GA 发布

    一:itest work 简介 itest work 开源敏捷测试管理,包含极简的任务管理,测试管理,缺陷管理,测试环境管理,接口测试,接口Mock,还有压测 ,又有丰富的统计分析,8合1工作站.可按 ...

  2. kettle从入门到精通 第六十三课 ETL之kettle kettle调用python脚本的两种方法

    kettle中不能直接调用python脚本,可以通过shell脚本和http进行调用pyton服务. 一.shell脚本调用python脚本 1.下面是一段简单的无参python脚本 import o ...

  3. 任意树遍历,可以使用 goto 跳记号标注的

    先顺序进入到最后一个根的根部,完后扫描同级 同级扫描完用 goto跳代码改层数到倒数地二层 之后操作就是倒着往上搜索的,有难度,但是还是能做到的嘛 用 lisit 好像不需要别的,全用 list 连接 ...

  4. sse 与 编译器自动优化

    direct x 形式的矩阵和向量计算代码在编译的时候是自动汇编为 sse汇编的 何时使用手写sse 指令呢,当你的应用程序需要写一些物理运算时候 可以使用自己编写的sse计算函数来为 3维运算加速 ...

  5. GO语言 GOLANG 上传微信电子小票图片

    GO语言 GOLANG 上传微信电子小票图片.GO HTTP POST 图片文件.GO 上传图片文件.multipart/form-data.image/jpeg.image/png. GO 环境: ...

  6. rsync备份任务练习

    06-备份任务实战 今天的任务主要以实际备份任务入手,完成综合练习,完成对rsync的综合运用. 先看需求 再讲解 再次动手实践 客户端需求 客户端需求: 1.客户端每天凌晨1点在服务器本地打包备份( ...

  7. 《软件性能测试分析与调优实践之路》第二版-手稿节选-Mysql数据库性能定位与分析

    在做MySQL数据的性能定位前,需要先知道MySQL查询时数据库内部的执行过程.只有弄清SQL的执行过程,才能对执行过程中的每一步的性能做定位分析.如图6-2-1所示. 图6-2-1 从图中可以看到, ...

  8. 行为型模式(Behavioer Pattern)

    行为型设计模式 行为型模式定义了系统中对象之间的交互与通信,研究系统在运行时对象之间的相互通信与协作,进一步明确对象的职责,包括对系统中较为复杂的流程的控制. 在软件系统运行时对象并不是孤立存在的,它 ...

  9. Oracle 数据库 命令行安装

    Oracle 数据库 命令行安装 1. 准备工作 关闭 防火墙,关闭 SElinux 2. 安装相关依赖包 yum -y install binutils compat-libcap1 compat- ...

  10. Windows记录登录日志

    有的时候,我们希望系统记录登录的日志,以便查看有无他人动过自己的电脑. 步骤 1.在windows中搜索并打开"组策略". 2.点击计算机配置-->Windows设置--&g ...