在配置类中继续添加配置
相关配置的官网地址:https://www.elastic.co/guide/en/elasticsearch/client/java-rest/7.4/java-rest-low-usage-requests.html#java-rest-low-usage-request-options
当前配置类:
@Configuration
public class VmElasticsearchConfig {
@Bean
public RestHighLevelClient esRestClient() {
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(
new HttpHost("192.168.23.145", 9200, "http")));
//没有集群不用指定多个
// new HttpHost("localhost", 9201, "http"))
return client;
}
public static final RequestOptions COMMON_OPTIONS;
static {
RequestOptions.Builderbuilder=RequestOptions.DEFAULT.toBuilder();
// builder.addHeader("Authorization", "Bearer " + TOKEN);
// builder.setHttpAsyncResponseConsumerFactory(
// new HttpAsyncResponseConsumerFactory
// .HeapBufferedResponseConsumerFactory(30 * 1024 * 1024 * 1024));
COMMON_OPTIONS = builder.build();
}
}
测试添加:
文档地址:https://www.elastic.co/guide/en/elasticsearch/client/java-rest/7.4/java-rest-high-document-index.html
有多种方式,详情见文档
index测试:
@Test
public void indexData() throws IOException {
IndexRequest indexRequest = new IndexRequest("users");
indexRequest.id("1");//数据的id
// indexRequest.source("userName","zhangsan","age","18"); 第一种方式
User user = new User();
user.setUserName("zhangsan");
user.setAge(18);
user.setGender("男");
String jsonString = JSON.toJSONString(user);
indexRequest.source(jsonString, XContentType.JSON);//数据的内容
//执行操作
IndexResponse index = client.index(indexRequest, VmElasticsearchConfig.COMMON_OPTIONS);
//提取有用的响应数据
System.out.println(index);
}