• 从入门到进阶 之 ElasticSearch SpringData 继承篇


    1. 🌹 以上分享 从入门到进阶 之 ElasticSearch SpringData 继承篇,如有问题请指教写。
    2. 🌹🌹 如你对技术也感兴趣,欢迎交流。
    3. 🌹🌹🌹 如有需要,请👍点赞💖收藏🐱‍🏍分享


    Spring Data

            Spring Data 是一个用于简化数据库、非关系型数据库、索引库访问,并支持云服务的开源框架。其主要目标是使得对数据的访问变得方便快捷,并支持 map-reduce 框架和云计算数据服务。 Spring Data 可以极大的简化JPA (Elasticsearch.·.)的写法,在几乎不用写实现的情况下,实现对数据的访问和操作。除了 CRUD 外,还包括如分页、排序等功能

    Spring DataLevel up your Java code and explore what Spring can do for you.icon-default.png?t=N7T8https://spring.io/projects/spring-data

    POM

    1. <dependency>
    2. <groupId>org.projectlombok</groupId>
    3. <artifactId>lombok</artifactId>
    4. </dependency>
    5. <dependency>
    6. <groupId>org.springframework.boot</groupId>
    7. <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
    8. </dependency>
    9. <dependency>
    10. <groupId>org.springframework.boot</groupId>
    11. <artifactId>spring-boot-devtools</artifactId>
    12. <scope>runtime</scope>
    13. </dependency>
    14. <dependency>
    15. <groupId>org.springframework.boot</groupId>
    16. <artifactId>spring-boot-starter-test</artifactId>
    17. <scope>test</scope>
    18. </dependency>
    19. <dependency>
    20. <groupId>junit</groupId>
    21. <artifactId>junit</artifactId>
    22. <scope>test</scope>
    23. </dependency>
    24. <dependency>
    25. <groupId>org.springframework</groupId>
    26. <artifactId>spring-test</artifactId>
    27. </dependency>

    项目结构 

    配置

    1. @Data
    2. @Configuration
    3. @ConfigurationProperties(prefix = "elasticsearch")
    4. public class ElasticSearchConfig extends AbstractElasticsearchConfiguration {
    5. private String host;
    6. private Integer port;
    7. @Override
    8. public RestHighLevelClient elasticsearchClient() {
    9. String scheme = "http";
    10. return new RestHighLevelClient(
    11. RestClient.builder(new HttpHost(host, port, scheme)));
    12. }
    13. }

    实体

    1. @Data
    2. @NoArgsConstructor
    3. @AllArgsConstructor
    4. @ToString
    5. @Document(indexName = "product", shards = 3, replicas = 1)
    6. public class Product {
    7. /**
    8. * 商品唯一标识
    9. */
    10. @Id
    11. private Long id;
    12. /**
    13. * 商品名称
    14. */
    15. @Field(type = FieldType.Text)
    16. private String title;
    17. /**
    18. * 分类名称
    19. */
    20. @Field(type = FieldType.Keyword)
    21. private String category;
    22. /**
    23. * 商品价格
    24. */
    25. @Field(type = FieldType.Double)
    26. private Double price;
    27. /**
    28. * 图片地址
    29. */
    30. @Field(type = FieldType.Keyword, index = false)
    31. private String images;
    32. }

     Dao

     测试类

    1. import com.mcp.es.entity.Product;
    2. import org.junit.jupiter.api.Test;
    3. import org.junit.runner.RunWith;
    4. import org.springframework.beans.factory.annotation.Autowired;
    5. import org.springframework.boot.test.context.SpringBootTest;
    6. import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
    7. import org.springframework.test.context.junit4.SpringRunner;
    8. @SpringBootTest
    9. @RunWith(SpringRunner.class)
    10. class EsSpringApplicationTests {
    11. @Autowired
    12. private ElasticsearchRestTemplate template;
    13. }

    创建索引

    1. // 将根据实体类中的配置参数,自动创建索引
    2. @Test
    3. void createIndex() {
    4. System.out.println("自动创建索引");
    5. }

    索引操作

    文档 

    1. @RunWith(SpringRunner.class)
    2. @SpringBootTest
    3. public class SpringDataEsRepositoryTest {
    4. @Autowired
    5. private ProductRepository repository;
    6. }

     基础操作

    1. @Test
    2. public void save(){
    3. Product product = new Product();
    4. product.setId(1000L);
    5. product.setTitle("华为手机");
    6. product.setCategory("手机");
    7. product.setPrice(2999.0);
    8. product.setImages("http://www.atguigu/hw.jpg");
    9. repository.save(product);
    10. }

    1. //修改
    2. @Test
    3. public void update(){
    4. Product product = new Product();
    5. product.setId(1000L);
    6. product.setTitle("小米 2 手机");
    7. product.setCategory("手机");
    8. product.setPrice(9999.0);
    9. product.setImages("http://www.atguigu/xm.jpg");
    10. repository.save(product);
    11. }

    1. //根据 id 查询
    2. @Test
    3. public void findById(){
    4. Product product = repository.findById(1000L).get();
    5. System.out.println(product);
    6. }

    1. @Test
    2. public void findAll(){
    3. Iterable products = repository.findAll();
    4. for (Product product : products) {
    5. System.out.println(product);
    6. }
    7. }

    1. //删除
    2. @Test
    3. public void delete(){
    4. Product product = new Product();
    5. product.setId(1000L);
    6. repository.delete(product);
    7. }

     

    1. //批量新增
    2. @Test
    3. public void saveAll(){
    4. List<Product> productList = new ArrayList<>();
    5. for (int i = 0; i < 10; i++) {
    6. Product product = new Product();
    7. product.setId(Long.valueOf(i));
    8. product.setTitle("["+i+"]小米手机");
    9. product.setCategory("手机");
    10. product.setPrice(1999.0 + i);
    11. product.setImages("http://www.atguigu/xm.jpg");
    12. productList.add(product);
    13. }
    14. repository.saveAll(productList);
    15. }

    1. //分页查询
    2. @Test
    3. public void findByPageable(){
    4. //设置排序(排序方式,正序还是倒序,排序的 id)
    5. Sort sort = Sort.by(Sort.Direction.DESC,"id");
    6. int currentPage=0;//当前页,第一页从 0 开始, 1 表示第二页
    7. int pageSize = 5;//每页显示多少条
    8. //设置查询分页
    9. PageRequest pageRequest = PageRequest.of(currentPage, pageSize,sort);
    10. //分页查询
    11. Page<Product> productPage = repository.findAll(pageRequest);
    12. for (Product Product : productPage.getContent()) {
    13. System.out.println(Product);
    14. }
    15. }

     

    文档搜索

    1. /**
    2. * term 查询
    3. * search(termQueryBuilder) 调用搜索方法,参数查询构建器对象
    4. */
    5. @Test
    6. public void termQuery(){
    7. TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("title", "小米");
    8. Iterable<Product> products = repository.search(termQueryBuilder);
    9. for (Product product : products) {
    10. System.out.println(product);
    11. }
    12. }
    13. /**
    14. * term 查询加分页
    15. */
    16. @Test
    17. public void termQueryByPage(){
    18. int currentPage= 0 ;
    19. int pageSize = 5;
    20. //设置查询分页
    21. PageRequest pageRequest = PageRequest.of(currentPage, pageSize);
    22. TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("title", "小米");
    23. Iterable<Product> products =
    24. repository.search(termQueryBuilder,pageRequest);
    25. for (Product product : products) {
    26. System.out.println(product);
    27. }
    28. }

  • 相关阅读:
    Java 华为真题-新学校选址
    微信小程序笔记
    阅读Skeleton.css源码,改善睡眠质量(尽管它只有419行代码)
    12-k8s-HPA自动扩缩容
    [Java]SPI扩展功能
    13:大数据与Hadoop|分布式文件系统|分布式Hadoop集群
    船舶单独安装的双频GNSS的PPP解算
    方程组解的情况与向量组相关性转化【线代碎碎念】
    【C++面向对象】7. 类的静态成员
    ThreeJs学习
  • 原文地址:https://blog.csdn.net/qq_32662595/article/details/133913292