ELK SpringData框架 Springboot集成elasticSearch
Posted Dragon Wu
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ELK SpringData框架 Springboot集成elasticSearch相关的知识,希望对你有一定的参考价值。
目录
一、SpringData框架基本介绍
二、集成案例
1、引入依赖
<?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>
<version>2.3.2.RELEASE</version>
<artifactId>spring-boot-starter-parent</artifactId>
<relativePath/>
</parent>
<groupId>com.dragonwu</groupId>
<artifactId>ES-spring</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>
2、添加配置
yam配置:
elasticsearch:
#es服务地址
host: 127.0.0.1
#es服务端口
port: 9200
#配置日志级别,开启debug日志
logging:
level:
com:
dragonwu:
es: debug
配置类:
package com.dragonwu.es.config;
import lombok.Data;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.config.AbstractElasticsearchConfiguration;
/**
* @author DragonWu
* @date 2022-09-18 15:52
**/
@ConfigurationProperties(prefix = "elasticsearch")
@Configuration
@Data
public class ElasticSearchConfig extends AbstractElasticsearchConfiguration
private String host;
private Integer port;
@Override
public RestHighLevelClient elasticsearchClient()
RestClientBuilder builder= RestClient.builder(new HttpHost(host,port));
return new RestHighLevelClient(builder);
3、对应的实体类和操作
实体类:
package com.dragonwu.es.domain;
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;
/**
* @author DragonWu
* @date 2022-09-18 15:45
**/
@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
@Document(indexName = "product",shards = 3,replicas = 1)
public class Product
@Id
private Long id;//商品唯一标识
@Field(type= FieldType.Text)
private String title;//商品名称
@Field(type=FieldType.Keyword)
private String catalog;//商品类别
@Field(type=FieldType.Double)
private Double price;//商品价格
//keyword表示该字段不能被拆分,index=false表示该字段不能被当主键查询
@Field(type = FieldType.Keyword,index = false)
private String image;//图片地址
数据操作层:
package com.dragonwu.es.mapper;
import com.dragonwu.es.domain.Product;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Repository;
/**
* @author DragonWu
* @date 2022-09-18 16:03
**/
@Repository
public interface ProductDao extends ElasticsearchRepository<Product,Long>
4、添加单元测试
package com.dragonwu.es;
import com.dragonwu.es.domain.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;
/**
* @author DragonWu
* @date 2022-09-18 16:10
**/
@RunWith(SpringRunner.class)
@SpringBootTest
public class ElasticSearchSpringbootTest
@Autowired
private ElasticsearchRestTemplate elasticsearchRestTemplate;
@Test
public void createIndex()
//创建索引,系统初始化会自动创建索引
System.out.println("创建索引");
@Test
public void deleteIndex()
boolean flg=elasticsearchRestTemplate.deleteIndex(Product.class);
System.out.println("删除索引="+flg);
目录结构:
运行索引创建的单元测试:
再次通过ApiPost查看索引
可以看到product索引已创建。
再运行删除的单元测试:
查看索引
此时可以看到对应的product索引已经被删除了。
5、文档操作
上面我们对索引进行了操作,下面对文档进行操作。
package com.dragonwu.es;
import com.dragonwu.es.domain.Product;
import com.dragonwu.es.mapper.ProductDao;
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;
/**
* @author DragonWu
* @date 2022-09-18 16:32
**/
@SpringBootTest
@RunWith(SpringRunner.class)
public class ElasticSearchDaoTest
@Autowired
private ProductDao productDao;
/*
* 新增
*/
@Test
public void save()
Product product=new Product();
product.setId(2L);
product.setTitle("华为手机");
product.setCatalog("手机");
product.setPrice(2999.0);
product.setImage("/upload/file/test.jpg");
productDao.save(product);
@Test
public void findById()
Product product=productDao.findById(2L).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(2L);
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) i);
product.setTitle("["+i+"]小米手机");
product.setCatalog("手机");
product.setPrice(1999.0+i);
product.setImage("/upload/file/img.jpg");
productList.add(product);
productDao.saveAll(productList);
@Test
public void findByPageable()
//设置排序
Sort sort = Sort.by(Sort.Direction.DESC, "id");
int from=0;//当前页
int size=5;//每页显示多少条
//设置查询分页
PageRequest pageRequest=PageRequest.of(from,size,sort);
//分页查询
Page<Product> productPage=productDao.findAll(pageRequest);
for(Product product:productPage.getContent())
System.out.println(product);
添加文档的单元测试
更新同样使用save方法,只要id相同即代表更新;其他单元测试都已放到代码了,直接测试即可。
6、文档的各种查询
package com.dragonwu.es;
import com.dragonwu.es.domain.Product;
import com.dragonwu.es.mapper.ProductDao;
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;
/**
* @author DragonWu
* @date 2022-09-18 17:54
**/
@SpringBootTest
@RunWith(SpringRunner.class)
public class ElasticSearchDocSearchTest
@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);
集成到此。
以上是关于ELK SpringData框架 Springboot集成elasticSearch的主要内容,如果未能解决你的问题,请参考以下文章