1.ElasticConfiguration类中添加如下配置
private 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();
}
2.测试
下面是使用最常用的方法来保存索引
@Test
public void indexData() throws IOException {
IndexRequest indexRequest = new IndexRequest("users");
// indexRequest.source("name","libai","age",13);
User user = new User();
user.setName("libai");
user.setAge(3000);
user.setSex("m");
String jsonString = JSON.toJSONString(user);
indexRequest.source(jsonString, XContentType.JSON);
IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT);
System.out.println(response);
}
控制台成功打印索引

kibana上查询也查到了


本文介绍如何在Elasticsearch中通过Java客户端创建索引,并演示了如何将自定义对象转换为JSON字符串进行存储。此外,还展示了设置RequestOptions的具体方式。

被折叠的 条评论
为什么被折叠?



