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

本文涉及的产品
检索分析服务 Elasticsearch 版,2核4GB开发者规格 1个月
简介: 《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的时候使用的是低级别客户端,所以企业开发需要更换成高级别的客户端模式。

相关实践学习
使用阿里云Elasticsearch体验信息检索加速
通过创建登录阿里云Elasticsearch集群,使用DataWorks将MySQL数据同步至Elasticsearch,体验多条件检索效果,简单展示数据同步和信息检索加速的过程和操作。
ElasticSearch 入门精讲
ElasticSearch是一个开源的、基于Lucene的、分布式、高扩展、高实时的搜索与数据分析引擎。根据DB-Engines的排名显示,Elasticsearch是最受欢迎的企业搜索引擎,其次是Apache Solr(也是基于Lucene)。 ElasticSearch的实现原理主要分为以下几个步骤: 用户将数据提交到Elastic Search 数据库中 通过分词控制器去将对应的语句分词,将其权重和分词结果一并存入数据 当用户搜索数据时候,再根据权重将结果排名、打分 将返回结果呈现给用户 Elasticsearch可以用于搜索各种文档。它提供可扩展的搜索,具有接近实时的搜索,并支持多租户。
相关文章
|
1月前
|
存储 搜索推荐 Java
|
2月前
|
Java
【极问系列】springBoot集成elasticsearch出现Unable to parse response body for Response
【极问系列】springBoot集成elasticsearch出现Unable to parse response body for Response
|
29天前
|
Java 测试技术 Maven
SpringBoot集成Elasticsearch
SpringBoot集成Elasticsearch
24 0
|
1月前
|
消息中间件 Java 关系型数据库
【二十】springboot整合ElasticSearch实战(万字篇)
【二十】springboot整合ElasticSearch实战(万字篇)
212 47
|
2月前
|
Java Windows
【极光系列】springBoot集成elasticsearch
【极光系列】springBoot集成elasticsearch
|
2月前
|
Java Maven
【SpringBoot专题_02】springboot集成Swagger详细教程
【SpringBoot专题_02】springboot集成Swagger详细教程
|
2月前
|
XML 监控 druid
【Java专题_02】springboot+mybatis+pagehelper分页插件+druid数据源详细教程
【Java专题_02】springboot+mybatis+pagehelper分页插件+druid数据源详细教程
|
2月前
|
Java Docker 容器
springboot整合后台框架(三)整合elasticsearch
springboot整合后台框架(三)整合elasticsearch
36 0
|
18天前
|
数据可视化 索引
elasticsearch head、kibana 安装和使用
elasticsearch head、kibana 安装和使用
|
30天前
|
存储 负载均衡 索引
linux7安装elasticsearch-7.4.0集群配置
linux7安装elasticsearch-7.4.0集群配置
113 0