我正在开发一个Spring Boot程序,它使用RestHighLevelClient将文档插入ElasticSearch。现在我已经为客户端构建了配置,但是,我不知道如何插入/索引。关于ElasticSearch的文档似乎让我感到困惑..
以下是我到目前为止尝试的代码:
@KafkaListener(topics = "${kafka.topic}",groupId = "test")
public void receive(String message) {
LOGGER.info(message);
insertData(message);
latch.countDown();
}
private void insertData(String message){
IndexRequest request = new IndexRequest(
"fx-rate",
"_doc",
"1");
request.source(message, XContentType.JSON);
}
答案 0 :(得分:1)
RestHighLevelClient实例需要构建一个REST低级客户端构建器,如下所示:
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(
new HttpHost("localhost", 9200, "http"),
new HttpHost("localhost", 9201, "http")));
创建索引(如果不存在)
CreateIndexRequest request = new CreateIndexRequest("twitter");
更新文档:
XContentBuilder builder = XContentFactory.jsonBuilder();
builder.startObject();
{
builder.field("updated", new Date());
builder.field("reason", "daily update");
}
builder.endObject();
UpdateRequest request = new UpdateRequest("posts", "doc", "1")
.doc(builder);
使用您的客户端发送请求。
P.S。 es high level client