17、全文检索 -- Elasticsearch -- 使用 反应式 RestClient (ReactiveElasticsearchClient)操作 Es 服务器(增、删、查 :索引库和文档)

这篇具有很好参考价值的文章主要介绍了17、全文检索 -- Elasticsearch -- 使用 反应式 RestClient (ReactiveElasticsearchClient)操作 Es 服务器(增、删、查 :索引库和文档)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

使用反应式RestClient (ReactiveElasticsearchClient) 操作 Elasticsearch 服务器(增、删、查 索引库和文档)


反应式 RestClient


Elasticsearch 所提供 RestHighLevelClient 本身提供了 【同步编程】 和 【异步编程】两种模型。

Elasticsearch 官方并未提供反应式的 RestClient :

因此 Spring Data Elasticsearch 额外补充了一个 ReactiveElasticsearchClient,用于提供反应式API支持,

ReactiveElasticsearchClient 相当于 RestHighLevelClient 的反应式版本,因此它们二者的功能基本相似。

ReactiveElasticsearchClient 是基于 WebFlux 的 WebClient 的 ,因此如果要使用反应式的 RestClient,还需要添加 Spring WebFlux 依赖。


反应式 RestClient 的方法

ReactiveElasticsearchClient 的方法与 RestHighLevelClient 的方法大同小异,区别只是 ReactiveElasticsearchClient 的方法所返回的是Mono 或 Flux。


处理配置信息及对 反应式 RestClient 进行定制


所有以 spring.data.elasticsearch.client.reactive.* 开头的属性由 ReactiveElasticsearchRestClientProperties 类负责加载,并由容器中自动配置的 ClientConfiguration 负责处理。

如果觉得上面配置属性还不够,或希望完全控制 ReactiveElasticsearchClient 的配置,则可在容器中配置一个自定义的 ClientConfiguration , 这样 Spring Boot 将不再提供自动配置的 ClientConfiguration。

这样一来,Spring Boot 在自动配置 ReactiveElasticsearchClient 时,就会改为使用自定义的 ClientConfiguration 所提供的配置信息,完全忽略以 spring.data.elasticsearch.client.reactive.* 开头的配置信息。

 ReactiveElasticsearchClient
          ↑
   ClientConfiguration (该Bean可以被替换,这意味着使用自己的 ClientConfiguration 来管理与 Elasticsearch 的反应式API连接)
          ↓ 
 处理 spring.data.elasticsearch.client.reactive.*属性
 ( 如果用自己自定义的ClientConfiguration ,那么就可以自己决定要不要读取这个配置文件的这个开头的属性)
 ( 如果用 springboot 自身的 ClientConfiguration ,则会读取这个配置文件的这个开头的属性)


代码演示:


1、创建项目


reactiveelasticsearchclient,# 全文检索(Solr 和 Elasticsearch),全文检索,elasticsearch,服务器


2、添加依赖


        <!--Elasticsearch 添加 反应式API  -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>

        <!-- 反应式的依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>

3、配置文件


reactiveelasticsearchclient,# 全文检索(Solr 和 Elasticsearch),全文检索,elasticsearch,服务器


上面配置中 spring.data.elasticsearch.client.reactive.use-ssl 属性为 false,这说明这次演示不使用SSL,因此需要关闭 Elasticsearch 的SSL。

配置反应式 RestClient 所用的配置属性与配置 RestHighLevelClient 的配置属性完全不同的,且属性处理类也不相同。
此处用的属性处理类是:ReactiveElasticsearchRestClientProperties


4、测试类 演示 反应式 操作Elasticsearch服务器


这篇是正常的web项目:使用 RESTful 客户端(RestHighLevelClient )操作 Elasticsearch,测试方法是一样的,只不过现在改成了反应式编程,可以根据这篇文章做对比。


1、添加索引库

代码

reactiveelasticsearchclient,# 全文检索(Solr 和 Elasticsearch),全文检索,elasticsearch,服务器


测试结果

reactiveelasticsearchclient,# 全文检索(Solr 和 Elasticsearch),全文检索,elasticsearch,服务器


查看所有索引库:http://localhost:9200/_cat/indices

reactiveelasticsearchclient,# 全文检索(Solr 和 Elasticsearch),全文检索,elasticsearch,服务器


2、删除索引库
代码

reactiveelasticsearchclient,# 全文检索(Solr 和 Elasticsearch),全文检索,elasticsearch,服务器


测试结果

reactiveelasticsearchclient,# 全文检索(Solr 和 Elasticsearch),全文检索,elasticsearch,服务器


查看所有索引库:http://localhost:9200/_cat/indices
“items”, “users” 索引库已经被删除了

reactiveelasticsearchclient,# 全文检索(Solr 和 Elasticsearch),全文检索,elasticsearch,服务器


3、查询所有索引库
代码

reactiveelasticsearchclient,# 全文检索(Solr 和 Elasticsearch),全文检索,elasticsearch,服务器


测试结果

reactiveelasticsearchclient,# 全文检索(Solr 和 Elasticsearch),全文检索,elasticsearch,服务器


查看所有索引库:http://localhost:9200/_cat/indices
结果跟 postman 一样,表示代码查询索引库的数据正确

reactiveelasticsearchclient,# 全文检索(Solr 和 Elasticsearch),全文检索,elasticsearch,服务器


4、往索引库添加文档
代码

reactiveelasticsearchclient,# 全文检索(Solr 和 Elasticsearch),全文检索,elasticsearch,服务器


测试结果

reactiveelasticsearchclient,# 全文检索(Solr 和 Elasticsearch),全文检索,elasticsearch,服务器


查询指定 index 的全部文档:http://localhost:9200/books/_search

reactiveelasticsearchclient,# 全文检索(Solr 和 Elasticsearch),全文检索,elasticsearch,服务器


5、根据文档的 id 获取文档
代码

reactiveelasticsearchclient,# 全文检索(Solr 和 Elasticsearch),全文检索,elasticsearch,服务器


测试结果

成功

reactiveelasticsearchclient,# 全文检索(Solr 和 Elasticsearch),全文检索,elasticsearch,服务器


6、根据关键字和通配符查询文档
代码

reactiveelasticsearchclient,# 全文检索(Solr 和 Elasticsearch),全文检索,elasticsearch,服务器


测试结果

成功

reactiveelasticsearchclient,# 全文检索(Solr 和 Elasticsearch),全文检索,elasticsearch,服务器


7、根据文档的 id 删除文档
代码

reactiveelasticsearchclient,# 全文检索(Solr 和 Elasticsearch),全文检索,elasticsearch,服务器


测试结果

reactiveelasticsearchclient,# 全文检索(Solr 和 Elasticsearch),全文检索,elasticsearch,服务器


查询指定 index 的全部文档:http://localhost:9200/books/_search

如图:id 为 3 和 4 的文档被成功删除了

reactiveelasticsearchclient,# 全文检索(Solr 和 Elasticsearch),全文检索,elasticsearch,服务器文章来源地址https://www.toymoban.com/news/detail-861231.html



完整代码


application.properties 配置文件



# 配置Elasticsearch服务器的地址
spring.data.elasticsearch.client.reactive.endpoints=127.0.0.1:9200
# 配置不使用SSL
spring.data.elasticsearch.client.reactive.use-ssl=false
# 连接超时时间
spring.data.elasticsearch.client.reactive.socket-timeout=10s
# 配置连接Elasticsearch服务器的用户名、密码
spring.data.elasticsearch.client.reactive.username=elastic
spring.data.elasticsearch.client.reactive.password=123456


ReactiveclientElasticsearchTest 测试类


package cn.ljh.reactiveclient;

import lombok.SneakyThrows;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.get.GetResult;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.ValueSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.elasticsearch.client.reactive.ReactiveElasticsearchClient;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.Arrays;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE)
class ReactiveclientElasticsearchTest
{

    @Autowired
    private ReactiveElasticsearchClient reactiveEsClient;


    //以下方法皆是以反应式编程测试


    //反应式创建三个索引库
    @ParameterizedTest //参数测试
    @SneakyThrows
    @ValueSource(strings = {"books", "items", "users"})
    public void testCreateIndex(String indexName)
    {

        //指定分词器创建索引库的json格式的数据,每一行用双引号包起来,然后里面的每个双引号前面用反斜杠\转义
        String json = "{" +
                "\"settings\": {" +
                "    \"analysis\": {" +
                "      \"analyzer\": {" +
                "        \"default\": {" +
                "           \"tokenizer\": \"ik_max_word\"" +
                "        }" +
                "      }" +
                "    }" +
                "  }" +
                "}";

        CreateIndexRequest request = new CreateIndexRequest(indexName)
                //参数1:指定创建索引库时要传入的参数  ; 参数2:指定传入内容的类型
                .source(json, XContentType.JSON);

        //调用反应式方法,返回 Mono
        Mono<Boolean> resp = reactiveEsClient.indices().createIndex(request);

        //以同步的方式直接获取结果
        resp.blockOptional().ifPresent(System.err::println);

    }


    //删除索引库
    @SneakyThrows
    @ParameterizedTest
    @ValueSource(strings = {"items", "users"})
    public void testDeleteIndex(String indexName)
    {
        //删除索引的请求数据
        DeleteIndexRequest request = new DeleteIndexRequest(indexName);

        //客户端调用操作索引的方法,然后再调用删除的方法
        Mono<Boolean> resp = reactiveEsClient.indices().deleteIndex(request);

        //以同步的方式直接获取结果
        resp.blockOptional().ifPresent(System.err::println);
    }


    //查询所有的索引库
    @SneakyThrows
    @Test //这个测试不需要参数,直接用这个@Test注解即可
    public void testGetIndex()
    {
        //参数 "*" : 表示匹配所有的索引库
        GetIndexRequest request = new GetIndexRequest("*");

        //用反应式的rest客户端的方法来查询
        Mono<GetIndexResponse> monoResp = reactiveEsClient.indices().getIndex(request);

        //以同步的方式直接获取结果
        monoResp.blockOptional().ifPresent(resp ->
        {
            //返回的索引库是一个String类型的数组
            String[] indices = resp.getIndices();
            //把数组转成字符串
            String s = Arrays.toString(indices);
            System.err.println(s);
        });

    }










    //往索引库添加文档

    @ParameterizedTest
    @SneakyThrows
    //测试参数有多个值 ,用这个注解
    @CsvSource({
            "1,火影忍者,旋涡鸣人成长为第七代火影的故事,150",
            "2,家庭教师,废材纲成长为十代首领的热血事迹,200",
            "3,七龙珠,赛亚人孙悟空来到地球后的热血升级之旅,300",
            "4,七龙珠Z,超级赛亚人贝吉塔来到地球后的热闹景象,400"
    })
    public void testSaveDocument(Integer id, String title, String description, Double price)
    {
        //表明向 books 索引库添加文档
        IndexRequest request = new IndexRequest("books")
                .id(id + "")
                .source(
                        "title", title,
                        "description", description,
                        "price", price
                );

        Mono<IndexResponse> resp = reactiveEsClient.index(request);

        //以同步的方式直接获取结果
        resp.blockOptional().ifPresent(System.err::println);

    }










    //根据文档的id获取文档
    @SneakyThrows
    @ParameterizedTest
    @ValueSource(ints = {1, 3})
    public void testGetDocumentById(Integer id)
    {
        //表明从 books 索引库获取文档
        GetRequest request = new GetRequest("books")
                //表明根据指定的文档的id获取文档
                .id(id + "");

        Mono<GetResult> resp = reactiveEsClient.get(request);

        //以同步的方式直接获取结果
        resp.blockOptional().ifPresent(System.err::println);

    }






    //根据条件查询文档(普通关键字查询和通配符查询)

    @SneakyThrows
    @ParameterizedTest
    @CsvSource({
            "description,热*",
            "description,成长"
    })
    public void testSearchDocument(String field, String term)
    {
        // 构建查询条件的类
        SearchSourceBuilder builder = new SearchSourceBuilder();

        // 通过 SearchSourceBuilder 可以用面向对象的方式来构建查询的 JSON 字符串
        // SearchSourceBuilder 需要传入 QueryBuilders,而 QueryBuilders 用于构建 QueryBuilder
        if (term != null && term.contains("*"))
        {
            //根据字段和通配符关键字查询
            builder.query(QueryBuilders.wildcardQuery(field, term));
        } else
        {
            //根据字段和普通关键字查询
            builder.query(QueryBuilders.matchQuery(field,term));
        }

        //表明从 books 索引库查询文档
        SearchRequest request = new SearchRequest("books")
                // 此处的 builder 参数用于构建查询语法
                .source(builder);

        //客户端调用查询的方法 , 参数1:查询条件语法
        //Flux 表示返回了多个结果
        Flux<SearchHit> resp = reactiveEsClient.search(request);

        //以同步的方式获取Flux中的数据
        resp.toIterable().forEach(System.err::println);

    }








    //根据w文档的 id 删除文档

    @ParameterizedTest
    @SneakyThrows
    @ValueSource(ints = {3,4})
    public void testDeleteDocumentById(Integer id)
    {
        //表明从 books 索引库删除文档
        DeleteRequest request = new DeleteRequest("books")
                //获取指定id的文档
                .id(id+"");

        //rest客户端调用删除文档的方法
        Mono<DeleteResponse> resp = reactiveEsClient.delete(request);

        //以同步的方式直接获取结果
        resp.blockOptional().ifPresent(System.err::println);

    }


}


pom.xml 依赖文件


<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.3</version>
    </parent>

    <groupId>cn.ljh</groupId>
    <artifactId>reactiveclient</artifactId>
    <version>1.0.0</version>
    <name>reactiveclient</name>

    <properties>
        <java.version>11</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>

        <!--Elasticsearch 添加反应式API  -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>

        <!-- 反应式的依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>







到了这里,关于17、全文检索 -- Elasticsearch -- 使用 反应式 RestClient (ReactiveElasticsearchClient)操作 Es 服务器(增、删、查 :索引库和文档)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • elasticsearch全文检索

    传送门 best_fields 传送门 most_fields 当查询多字段包含相同文本以不同方式分词的时候此参数最有用, 传送门 cross_fields phrase和phrase_prefix 传送门 传送门

    2024年02月07日
    浏览(42)
  • ElasticSearch 实战:ElasticSearch文档全文检索

    Elasticsearch 实战:Elasticsearch 文档全文检索 全文检索是 Elasticsearch 的核心功能之一,它允许用户对文本内容进行高效的模糊搜索、词组匹配、同义词处理、停用词过滤等操作。以下是如何进行文档全文检索的详细步骤: **1. **全文匹配查询(Match Query) 最基础的全文检索查询是

    2024年04月11日
    浏览(48)
  • ES(Elasticsearch 全文检索)

    数据量大的时候 索引失效 =查询性能低 功能比较弱 对文档的内容进行分词,对词条创建索引,记录词条所在的文档信息根据词条查询到文档的id 从而查到文档 文档:每一条数据就是一条文档 词条:文档按照语义分成的词语 正向索引 根据文档的id创建索引 查询词条必须先找

    2024年02月05日
    浏览(52)
  • 全文检索-Elasticsearch-整合SpringBoot

    前面记录了 Elasticsearch 全文检索的入门篇和进阶检索。这次我们来讲下 Spring Boot 中如何整合 ES,以及如何在 Spring Cloud 微服务项目中使用 ES 来实现全文检索,来达到商品检索的功能。 检索服务单独作为一个服务,就称作 gulimall-search 模块。 点击 Next 勾选 Spring Web 依赖,点击

    2024年02月08日
    浏览(45)
  • ElasticSearch全文检索原理及过程

            ElasticSearch的搜索引擎中,每个 文档都有一个对应的文档 ID ,文档内容被表示为一系列的集合。例如文档 1 经过分词,提取了 20 个, 每个都会记录它在文档中出现的次数和出现位置 。那么,倒排索引就是 到文档   ID 的映射 ,每个关键

    2023年04月17日
    浏览(40)
  • ElasticSearch:全文检索及倒排索引原理

    首先介绍一下结构化与非结构化数据: 结构化数据将数据具有的特征事先以结构化的形式定义好,数据有固定的格式或有限的长度。典型的结构化数据就是传统关系型数据库的表结构,数据特征直接体现在表结构的字段上,所以根据某一特征做数据检索很直接,速度也比较快

    2024年02月14日
    浏览(36)
  • 7-Elasticsearch组合查询和全文检索

    Elasticsearch组合查询 组合查询–布尔查询 组合查询中的常用的查询方式:布尔查询。 它将多个查询条件组合在一起,并且将查询的结果和结果的评分组合在一起。 布尔查询是把多个子查询组合成一个布尔表达式,所有子查询之间逻辑关系是and,只有当一个文档满足布尔查询

    2024年02月04日
    浏览(40)
  • 全文检索学习之ElasticSearch学习笔记

    在非关系型数据库中,数据是非结构化的,如果直接去查找效率极低,全文检索将非结构化数据中的一部分信息提取出来,重新组织,使其变得有一定结构,然后对此有一定结构的数据进行搜索,从而达到搜索相对较快的目的。索引就是从非结构化数据中提取出的然后重新组

    2023年04月11日
    浏览(49)
  • 九.全文检索ElasticSearch经典入门-ElasticSearch映射修改

    这篇文章的内容是ElasticSearch映射修改,写这篇文章是有水友公司里面遇到了映射修改问题,我这里做了一个整理,希望对你有所帮助。 在ElasticSearch中一旦创建了映射想要进行修改是不被允许的。比如我这里有一个案例 上面创建了索引employee ,同时为其创建映射,指定了id和

    2024年02月05日
    浏览(54)
  • 使用Elasticsearch进行word,excel,PDF的全文检索 windows实现 超完整(ingest-attachment实现)

    首先要明确的一点就是Elasticsearch的版本要和ingest-attachment的版本一致,要不然没办法安装。然后还有一点JAVA版本要在11以上 先说说原理吧,其实就是将文件base64编码,然后再用插件读取文件内容并保存到es中。 安装完jdk之后用cmd查看一下java -version看看是否已经从1.8修改为了

    2024年02月13日
    浏览(35)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包