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上查询也查到了