• JSD-2204-Elasticsearch-SpringData-酷鲨商城概述-Day07


    1.操作Elasticsearch

    项目csmall-finish项目中

    node文件夹下共享了ES文档,命令都在里面,可以测试

    所有的代码都在"ES文档"中, 笔记略

    下面我们要学习使用java代码来操作ES

    2.SpringBoot 操作 Elasticsearch

    2.1Spring Data简介

    原生状态下,我们使用JDBC连接数据库,因为代码过于繁琐,所以改为使用Mybatis框架

    在ES的原生状态下,我们java代码需要使用socket访问ES,但是也是过于繁琐,我们可以使用SpringData框架简化

    Spring Data是Spring提供的一套连接各种第三方数据源的框架集

    我们需要使用的是其中连接ES的Spring Data Elasticseatrch

    官方网站:Spring Data

    官网中列出了SpringData支持连接操作的数据源列表

    下面我们就按照SpringDataElasticsearch的步骤对ES进行操作

    2.2添加依赖和配置

    就使用我们之前创建的search模块来操作ES

    pom文件添加依赖

    1. "1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    4. <modelVersion>4.0.0modelVersion>
    5. <parent>
    6. <groupId>cn.tedugroupId>
    7. <artifactId>csmallartifactId>
    8. <version>0.0.1-SNAPSHOTversion>
    9. <relativePath/>
    10. parent>
    11. <groupId>cn.tedugroupId>
    12. <artifactId>searchartifactId>
    13. <version>0.0.1-SNAPSHOTversion>
    14. <name>searchname>
    15. <description>Demo project for Spring Bootdescription>
    16. <dependencies>
    17. <dependency>
    18. <groupId>org.springframework.bootgroupId>
    19. <artifactId>spring-boot-starterartifactId>
    20. dependency>
    21. <dependency>
    22. <groupId>org.springframework.bootgroupId>
    23. <artifactId>spring-boot-starter-data-elasticsearchartifactId>
    24. dependency>
    25. <dependency>
    26. <groupId>org.springframework.bootgroupId>
    27. <artifactId>spring-boot-starter-testartifactId>
    28. dependency>
    29. dependencies>
    30. project>

    application.properties添加配置

    1. # 设置ES所在的ip地址端口号
    2. spring.elasticsearch.rest.uris=http://localhost:9200
    3. # 设置日志门槛,用于显示ES的运行信息
    4. logging.level.cn.tedu.search=debug
    5. # SpringDataElasticsearch底层有一个专门输出运行状态的类,也要设置
    6. logging.le
  • 相关阅读:
    LeetCode每日一题——672. 灯泡开关 Ⅱ
    让 K8s 更简单!8款你不得不知的 AI 工具-Part 1
    Spring MVC 入门指南
    扎克伯格说,Llama3-8B还是太大了,量化、剪枝、蒸馏准备上!
    ACM新手入门之杭电150题使用指南及C语言学习推荐
    vue3项目应用font awesome6
    聊一聊作为高并发系统基石之一的缓存,会用很简单,用好才是技术活
    C进阶-自定义类型:结构体、枚举、联合
    Mybatis改成Mybatis-plus需要更改地方
    移动端js总结
  • 原文地址:https://blog.csdn.net/TheNewSystrm/article/details/126549718