记录一下,后续研究RestHighLevelClient:
/**
* 插入数据
* */
@Test
public void index() {
try {
// 1. 初始化
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http")));
// 2. 创建索引
IndexRequest indexRequest = new IndexRequest("posts");
indexRequest.id("1");
String jsonString = "{" +
"\"user\":\"chargedot\"," +
"\"Date\":\"2019-10-25\"," +
"\"message\":\"trying out Elasticsearch\"" +
"}";
indexRequest.source(jsonString, XContentType.JSON);
// 3. 条件设置
indexRequest.routing("routing");
indexRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.WAIT_UNTIL);
indexRequest.setRefreshPolicy("wait_for");
// 4. 监听
IndexResponse indexResponse = client.index(indexRequest, RequestOptions.DEFAULT);
ActionListener<IndexResponse> listener = new ActionListener<IndexResponse>() {
@Override
public void onResponse(IndexResponse indexResponse) {
log.info("indexResponse succeed!");
}
@Override
public void onFailure(Exception e) {
log.info("indexResponse failed!");
}
};
client.close();
} catch (IOException e) {
log.info("IOException", e);
}
}