新的网站的建设步骤,网站网站怎么搭建,中国住建网的官网,互联网品牌是什么意思文章目录 一#xff0c;谷粒商城实战笔记-126-全文检索-ElasticSearch-整合-测试保存1#xff0c;在Elasticsearch的配置类中增加通用设置2#xff0c;索引数据3#xff0c;验证 一#xff0c;谷粒商城实战笔记-126-全文检索-ElasticSearch-整合-测试保存
1#xff0c;在… 文章目录 一谷粒商城实战笔记-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索引数据 Testpublic 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);}Dataclass User {String name;int age;String gender;String json() {return JSON.toJSONString(this);}}这段Java代码使用了Elasticsearch的REST High-Level Client来索引存储一条文档到Elasticsearch中。 IndexRequest request new IndexRequest(users); 创建一个IndexRequest对象该对象指定要将数据索引到名为users的索引中。 request.id(1); 设置索引请求中的文档ID为1。在Elasticsearch中每个文档都有一个唯一标识符ID用于标识和检索文档。 String jsonString JSON.toJSONString(user); 使用JSON库例如Jackson或fastjson将User对象转换为JSON格式的字符串。这里假设使用的是fastjson库。
4 request.source(jsonString, XContentType.JSON);
将JSON字符串设置为IndexRequest的源数据并指明内容类型为JSON。
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());
}创建GetRequest: 使用 new GetRequest(users, 1) 创建一个 GetRequest 对象其中 users 是索引名称1 是文档的ID。 执行GET请求: 使用 client.get(getRequest, RequestOptions.DEFAULT) 发送GET请求。在这里我们使用了默认的 RequestOptions如果你之前定义了自定义的 RequestOptions你可以将 RequestOptions.DEFAULT 替换为 COMMON_OPTIONS。 处理响应: getResponse.isExists() 检查文档是否存在。getResponse.getSourceAsString() 获取文档的源数据作为字符串。使用JSON库例如Jackson或fastjson将字符串反序列化为 User 对象以便于进一步处理。