目录
- "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.0modelVersion>
-
- <parent>
- <groupId>org.springframework.bootgroupId>
- <version>2.3.2.RELEASEversion>
- <artifactId>spring-boot-starter-parentartifactId>
- <relativePath/>
- parent>
-
- <groupId>com.dragonwugroupId>
- <artifactId>ES-springartifactId>
- <version>1.0-SNAPSHOTversion>
-
- <properties>
- <maven.compiler.source>8maven.compiler.source>
- <maven.compiler.target>8maven.compiler.target>
- properties>
- <dependencies>
- <dependency>
- <groupId>org.projectlombokgroupId>
- <artifactId>lombokartifactId>
- dependency>
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-data-elasticsearchartifactId>
- dependency>
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-devtoolsartifactId>
- <scope>runtimescope>
- <optional>trueoptional>
- dependency>
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-testartifactId>
- <scope>testscope>
- dependency>
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-testartifactId>
- dependency>
- <dependency>
- <groupId>junitgroupId>
- <artifactId>junitartifactId>
- dependency>
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-testartifactId>
- dependency>
- dependencies>
-
- project>
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);
- }
- }
实体类:
- 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
{ - }
- 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索引已经被删除了。
上面我们对索引进行了操作,下面对文档进行操作。
- 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
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
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
productPage=productDao.findAll(pageRequest); - for(Product product:productPage.getContent()){
- System.out.println(product);
- }
- }
-
- }
添加文档的单元测试
更新同样使用save方法,只要id相同即代表更新;其他单元测试都已放到代码了,直接测试即可。
- 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
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
products = - productDao.search(termQueryBuilder,pageRequest);
- for (Product product : products) {
- System.out.println(product);
- }
- }
- }
集成到此。