当前位置: 首页 > news >正文

谷粒商城实战笔记-126-全文检索-ElasticSearch-整合-测试保存

文章目录

  • 一,谷粒商城实战笔记-126-全文检索-ElasticSearch-整合-测试保存
    • 1,在Elasticsearch的配置类中增加通用设置
    • 2,索引数据
    • 3,验证

一,谷粒商城实战笔记-126-全文检索-ElasticSearch-整合-测试保存

1,在Elasticsearch的配置类中增加通用设置

在这里插入图片描述

public static final RequestOptions COMMON_OPTIONS;static {RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder();// builder.addHeader("Authorization", "Bearer " + TOKEN);// builder.setHttpAsyncResponseConsumerFactory(//         new HttpAsyncResponseConsumerFactory//                 .HeapBufferedResponseConsumerFactory(30 * 1024 * 1024 * 1024));COMMON_OPTIONS = builder.build();}

这段代码的主要作用是:

  • 配置客户端请求时携带的认证信息(如认证令牌)。
  • 定义了客户端处理大文件响应的方式,通过设置一个较大的缓冲区大小。

这些配置选项将在后续使用Elasticsearch客户端执行请求时被应用,比如在后面代码片段中 restHighLevelClient.index(request, COMMON_OPTIONS),其中 COMMON_OPTIONS 被用作请求选项传递给客户端。这样,每次发送请求时都会自动包含这些设置,无需每次都手动配置。

2,索引数据

	public void indexData() throws IOException {IndexRequest request = new IndexRequest("users");request.id("1");   //数据的idUser user = new User();user.setName("zhangsan");user.setAge(18);user.setGender("男");String jsonString = JSON.toJSONString(user);request.source(jsonString, XContentType.JSON);IndexResponse index = restHighLevelClient.index(request, GulimallElasticSearchConfig.COMMON_OPTIONS);System.out.println(index);}class User {String name;int age;String gender;String json() {return JSON.toJSONString(this);}}

这段Java代码使用了Elasticsearch的REST High-Level Client来索引(存储)一条文档到Elasticsearch中。

  1. IndexRequest request = new IndexRequest("users");

    • 创建一个IndexRequest对象,该对象指定要将数据索引到名为"users"的索引中。
  2. request.id("1");

    • 设置索引请求中的文档ID为"1"。在Elasticsearch中,每个文档都有一个唯一标识符(ID),用于标识和检索文档。
  3. String jsonString = JSON.toJSONString(user);

    • 使用JSON库(例如Jackson或fastjson)将User对象转换为JSON格式的字符串。这里假设使用的是fastjson库。

4 request.source(jsonString, XContentType.JSON);

  • 将JSON字符串设置为IndexRequest的源数据,并指明内容类型为JSON。
  1. IndexResponse index = restHighLevelClient.index(request, GulimallElasticSearchConfig.COMMON_OPTIONS);
    • 使用REST High-Level Client执行索引操作。restHighLevelClient是Elasticsearch客户端实例,通过它发送索引请求。GulimallElasticSearchConfig.COMMON_OPTIONS是一个配置选项,通常用于设置请求的超时和其他参数。

这段代码执行完成后,会在Elasticsearch的users索引中创建一个ID为1的文档,包含用户的名字、年龄和性别信息。

3,验证

保存完成后,Kibana中可以查询到这条文档。

GET users/_search

在这里插入图片描述
也可以使用Java Api进行验证。

import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.ElasticsearchException;// 假设 restHighLevelClient 已经初始化好了
RestHighLevelClient client = restHighLevelClient;// 创建一个GetRequest
GetRequest getRequest = new GetRequest("users", "1");// 执行GET请求
try {GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT);// 检查文档是否存在if (getResponse.isExists()) {// 获取文档的源数据String jsonString = getResponse.getSourceAsString();System.out.println("Document source: " + jsonString);// 如果你想解析JSON并获取特定字段,可以使用JSON库User user = JSON.parseObject(jsonString, User.class);System.out.println("Name: " + user.getName());System.out.println("Age: " + user.getAge());System.out.println("Gender: " + user.getGender());} else {System.out.println("Document not found.");}
} catch (ElasticsearchException e) {System.err.println("Error getting document: " + e.getMessage());
} catch (IOException e) {System.err.println("IO error getting document: " + e.getMessage());
}

要验证文档是否已成功保存到Elasticsearch中,你可以使用Elasticsearch客户端执行GET请求来检索该文档。以下是一个简单的示例,说明如何使用REST High-Level Client检索刚刚索引的文档。

首先,你需要确保你有客户端实例 restHighLevelClient 可用,并且已经定义了 COMMON_OPTIONS。然后,你可以使用以下代码来获取文档:

import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.ElasticsearchException;// 假设 restHighLevelClient 已经初始化好了
RestHighLevelClient client = restHighLevelClient;// 创建一个GetRequest
GetRequest getRequest = new GetRequest("users", "1");// 执行GET请求
try {GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT);// 检查文档是否存在if (getResponse.isExists()) {// 获取文档的源数据String jsonString = getResponse.getSourceAsString();System.out.println("Document source: " + jsonString);// 如果你想解析JSON并获取特定字段,可以使用JSON库User user = JSON.parseObject(jsonString, User.class);System.out.println("Name: " + user.getName());System.out.println("Age: " + user.getAge());System.out.println("Gender: " + user.getGender());} else {System.out.println("Document not found.");}
} catch (ElasticsearchException e) {System.err.println("Error getting document: " + e.getMessage());
} catch (IOException e) {System.err.println("IO error getting document: " + e.getMessage());
}
  1. 创建GetRequest:

    • 使用 new GetRequest("users", "1") 创建一个 GetRequest 对象,其中 "users" 是索引名称,"1" 是文档的ID。
  2. 执行GET请求:

    • 使用 client.get(getRequest, RequestOptions.DEFAULT) 发送GET请求。在这里我们使用了默认的 RequestOptions,如果你之前定义了自定义的 RequestOptions,你可以将 RequestOptions.DEFAULT 替换为 COMMON_OPTIONS
  3. 处理响应:

    • getResponse.isExists() 检查文档是否存在。
    • getResponse.getSourceAsString() 获取文档的源数据作为字符串。
    • 使用JSON库(例如Jackson或fastjson)将字符串反序列化为 User 对象,以便于进一步处理。
http://www.lryc.cn/news/415206.html

相关文章:

  • flutter开发环境搭建与android studio 安装配置
  • postgresql 字符串 替换
  • 如何强化学习神经网络
  • Hadoop未授权访问漏洞
  • Python中json模块的编码和解码
  • 【Linux】文件变身大作战:Linux下的文件重命名艺术
  • 字节的存储和字符的存储
  • Markdown插入Base64格式的图片,无需图床,稳定保存
  • weblogic 连接gaussdb测试数据源是否联通
  • 如何成为全域运营商?掌握这2种申请方式就够了!
  • @ConfigurationProperties加在方法上
  • 使用CUBEMX配置的USB大容量存储设备主机库获取LUN数量的不严谨代码纠正
  • 合并重叠的区间
  • docker和运维
  • 苍穹外面day13(day10)---订单状态定时处理、来单提醒和客户催单
  • RCE和php文件上传
  • nextjs 实现TodoList网页应用案例
  • U盘格式化了怎么恢复数据?教你U盘恢复妙招
  • 化工厂室内外4G/5G+蓝牙+GPS/北斗RTK人员定位系统解决方案
  • 【知识跨境电商API接口丨python数分实战】国际电商平台用户成交转化分析
  • 【SpringBoot】Java对象级联校验
  • 【Redis 进阶】哨兵 Sentinel(重点理解流程和原理)
  • CSS实现元素hover时背景色拉伸渐变
  • Activity收不到bundle值
  • ZBrush
  • 【多线程-从零开始-贰】线程的构造方法和常见属性
  • 力扣:100379. 新增道路查询后的最短距离 I(Java,BFS)
  • 程序开发的常用设计思想
  • Qt之Gui
  • Linux操作系统之进程信号