《SpringBoot篇》16.SpringBoot整合Elasticsearch超详细教程(二)

本文涉及的产品
Elasticsearch Serverless通用抵扣包,测试体验金 200元
简介: 《SpringBoot篇》16.SpringBoot整合Elasticsearch超详细教程(二)

b.查询文档

这里注意请求时要把参数调整为none,否则会报错。

GET请求 http://localhost:9200/user/_doc/1   #查询单个文档   
GET请求 http://localhost:9200/user/_search   #查询全部文档

image.png


c.条件查询

GET请求 http://localhost:9200/user/_search?q=name:cllb  # q=查询属性名:查询属性值


image.png

d.修改文档(全量更新)

PUT请求 http://localhost:9200/user/_doc/1
文档通过请求参数传递,数据格式json
{
    "name":"ccc",
    "type":"bb",
    "description":"123"
}

image.png


e.修改文档(部分更新)

POST请求 http://localhost:9200/user/_update/1


文档通过请求参数传递,数据格式json
{   
    "doc":{      #部分更新并不是对原始文档进行更新,而是对原始文档对象中的doc属性中的指定属性更新
        "name":"springboot"  #仅更新提供的属性值,未提供的属性值不参与更新操作
    }


image.png

f.删除文档


DELETE请求  http://localhost:9200/books/_doc/1

image.png


4. 整合(早期低级版)


其实和整合Redis,MongoDB,ES都是一样的。

下面就开始springboot整合ES,操作步骤如下:


(1):导入springboot整合ES的starter坐标

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>

(2):进行基础配置

spring:
  elasticsearch:
    rest:
      uris: http://localhost:9200

配置ES服务器地址,端口9200


(3):使用springboot整合ES的专用客户端接口ElasticsearchRestTemplate来进行操作

@SpringBootTest
class Springboot18EsApplicationTests {
    @Autowired
    private ElasticsearchRestTemplate template;
}


(4)连接pojo层

package com.test;
import org.springframework.data.elasticsearch.annotations.Document;
import java.lang.annotation.Documented;
@Document(indexName = "user")
public class User {
    private Integer id;
    private String name;
    private String type;
    private String description;
    public User(Integer id, String name, String type, String description) {
        this.id = id;
        this.name = name;
        this.type = type;
        this.description = description;
    }
    public User() {
    }
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
    @Override
    public String toString() {
        return "Book{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", type='" + type + '\'' +
                ", description='" + description + '\'' +
                '}';
    }
}

(5)连接dao层

package com.test;
import org.elasticsearch.ElasticsearchSecurityException;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
public interface Esresposity extends ElasticsearchRepository<User,Integer> {
}


注: 上述这是ES早期的操作方式,使用的客户端被称为Low Level Client,因为这种操作方式在性能方面略显不足。

于是ES开发了全新的客户端操作方式,称为High Level Client。

高级别客户端与ES版本同步更新,但是springboot最初整合ES的时候使用的是低级别客户端,所以企业开发需要更换成高级别的客户端模式。

相关实践学习
以电商场景为例搭建AI语义搜索应用
本实验旨在通过阿里云Elasticsearch结合阿里云搜索开发工作台AI模型服务,构建一个高效、精准的语义搜索系统,模拟电商场景,深入理解AI搜索技术原理并掌握其实现过程。
ElasticSearch 最新快速入门教程
本课程由千锋教育提供。全文搜索的需求非常大。而开源的解决办法Elasricsearch(Elastic)就是一个非常好的工具。目前是全文搜索引擎的首选。本系列教程由浅入深讲解了在CentOS7系统下如何搭建ElasticSearch,如何使用Kibana实现各种方式的搜索并详细分析了搜索的原理,最后讲解了在Java应用中如何集成ElasticSearch并实现搜索。 &nbsp;
相关文章
|
6月前
|
缓存 NoSQL Java
基于SpringBoot的Redis开发实战教程
Redis在Spring Boot中的应用非常广泛,其高性能和灵活性使其成为构建高效分布式系统的理想选择。通过深入理解本文的内容,您可以更好地利用Redis的特性,为应用程序提供高效的缓存和消息处理能力。
437 79
|
11月前
|
Cloud Native Java C++
Springboot3新特性:开发第一个 GraalVM 本机应用程序(完整教程)
文章介绍如何在Spring Boot 3中利用GraalVM将Java应用程序编译成独立的本机二进制文件,从而提高启动速度、减少内存占用,并实现不依赖JVM运行。
1188 1
Springboot3新特性:开发第一个 GraalVM 本机应用程序(完整教程)
|
5月前
|
安全 Java Linux
Linux安装Elasticsearch详细教程
Linux安装Elasticsearch详细教程
738 1
|
11月前
|
JSON Java 网络架构
elasticsearch学习四:使用springboot整合 rest 进行搭建elasticsearch服务
这篇文章介绍了如何使用Spring Boot整合REST方式来搭建和操作Elasticsearch服务。
307 4
elasticsearch学习四:使用springboot整合 rest 进行搭建elasticsearch服务
|
11月前
|
Java API Apache
Springboot+shiro,完整教程,带你学会shiro
这篇文章提供了一个完整的Apache Shiro与Spring Boot结合使用的教程,包括Shiro的配置、使用以及在非Web和Web环境中进行身份验证和授权的示例。
616 2
Springboot+shiro,完整教程,带你学会shiro
|
11月前
|
前端开发 Java Apache
Springboot整合shiro,带你学会shiro,入门级别教程,由浅入深,完整代码案例,各位项目想加这个模块的人也可以看这个,又或者不会mybatis-plus的也可以看这个
本文详细讲解了如何整合Apache Shiro与Spring Boot项目,包括数据库准备、项目配置、实体类、Mapper、Service、Controller的创建和配置,以及Shiro的配置和使用。
2312 1
Springboot整合shiro,带你学会shiro,入门级别教程,由浅入深,完整代码案例,各位项目想加这个模块的人也可以看这个,又或者不会mybatis-plus的也可以看这个
|
11月前
|
缓存 NoSQL Java
springboot的缓存和redis缓存,入门级别教程
本文介绍了Spring Boot中的缓存机制,包括使用默认的JVM缓存和集成Redis缓存,以及如何配置和使用缓存来提高应用程序性能。
505 1
springboot的缓存和redis缓存,入门级别教程
|
10月前
|
JSON Java API
springboot集成ElasticSearch使用completion实现补全功能
springboot集成ElasticSearch使用completion实现补全功能
145 1
|
11月前
|
Web App开发 JavaScript Java
elasticsearch学习五:springboot整合 rest 操作elasticsearch的 实际案例操作,编写搜索的前后端,爬取京东数据到elasticsearch中。
这篇文章是关于如何使用Spring Boot整合Elasticsearch,并通过REST客户端操作Elasticsearch,实现一个简单的搜索前后端,以及如何爬取京东数据到Elasticsearch的案例教程。
665 0
elasticsearch学习五:springboot整合 rest 操作elasticsearch的 实际案例操作,编写搜索的前后端,爬取京东数据到elasticsearch中。
|
11月前
|
自然语言处理 Java Maven
elasticsearch学习二:使用springboot整合TransportClient 进行搭建elasticsearch服务
这篇博客介绍了如何使用Spring Boot整合TransportClient搭建Elasticsearch服务,包括项目创建、Maven依赖、业务代码和测试示例。
514 0
elasticsearch学习二:使用springboot整合TransportClient 进行搭建elasticsearch服务