您的位置:首页 > 科技 > IT业 > Es-SpringData

Es-SpringData

2024/11/14 15:23:06 来源:https://blog.csdn.net/weixin_45773937/article/details/140017138  浏览:    关键词:Es-SpringData

String-Data

  1. 创建项目,设置包名com.cn.es
  2. 引入pom依赖
<?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><groupId>com.cn.es</groupId><artifactId>es-spring</artifactId><version>1.0</version><parent><artifactId>spring-boot-starter-parent</artifactId><groupId>org.springframework.boot</groupId><version>2.3.6.RELEASE</version><relativePath/></parent><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties><dependencies><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-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-test</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot</artifactId></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-autoconfigure</artifactId><version>2.7.13</version></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency></dependencies></project>

创建application.properties

#esIP地址
elasticsearch.host=127.0.0.1
#es端口号码
elasticsearch.port=9200
#设置日志级别为debug
logging.level.com.cn.es=debug

创建启动类

package com.cn.es;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class SpringDataEsMainApplication {public static void main(String[] args) {SpringApplication.run(SpringDataEsMainApplication.class,args);}}

创建ES的配置类

package com.cn.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;@Data
@ConfigurationProperties(prefix = "elasticsearch")
@Configuration
public class ElasticsearchConfig extends AbstractElasticsearchConfiguration {private String host ;private Integer port ;@Overridepublic RestHighLevelClient elasticsearchClient() {RestClientBuilder builder = RestClient.builder(new HttpHost(host, port));RestHighLevelClient restHighLevelClient = new RestHighLevelClient(builder);return restHighLevelClient;}
}

创建实体类

package com.cn.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;@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
@Document(indexName = "product",shards = 3,replicas = 1)
public class Product {@Idprivate Long id;//商品唯一标识@Field(type = FieldType.Text)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层

package com.cn.es.dao;import com.cn.es.domain.Product;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Repository;@Repository
public interface ProductDao extends ElasticsearchRepository<Product,Long> {}

创建test类查询:

package com.cn.es.test;import com.cn.es.dao.ProductDao;
import com.cn.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.test.context.junit4.SpringRunner;import java.util.ArrayList;
import java.util.List;@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringDataProductDaoTest {@Autowiredprivate ProductDao productDao;/*** 添加任务*/@Testpublic void save () {Product product = new Product();product.setId(2L);product.setTitle("华为手机");product.setCategory("手机");product.setPrice(2999.0);product.setImages("http://hw.jpg");productDao.save(product);}//相同id就是更新@Testpublic void update () {Product product = new Product();product.setId(2L);product.setTitle("小米手机");product.setCategory("手机");product.setPrice(29999.0);product.setImages("http://xm.jpg");productDao.save(product);}@Testpublic void findById(){Product product = productDao.findById(2L).get();System.out.println(product);}@Testpublic void findAll(){Iterable<Product> products = productDao.findAll();for (Product product : products){System.out.println(product);}}@Testpublic void delete(){Product product=new Product();product.setId(1L);productDao.delete(product);}//批量新增@Testpublic 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.xm.jpg");productList.add(product);}}/**
term查询
search(termQueryBuilder)调用搜索方法,参数查询构建器对象
*/@Test
public void termQuery(){TermQueryBuilder termQueryBuilder=QueryBuilders.termsQuery("title","小米");Iterable<Product> pros = productDao.search(termQueryBuilder);for(Product product:pros){System.out,printLn(product);}}/**
term查询,分页查询
*/
@Test
public void termQueryByPage(){int currentPage=0;int pageSize=5;//设置查询分页PageRequest pageRequest = PageRequest.of(currentPage,pageSize);TermQueryBuilder termQueryBuilder = QueryBuilders.termsQuery("category","小米");Iterable<Product> pros = productDao.search(termQueryBuilder,pageRequest);for(Product product:pros){System.out,println(product);}}}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com