无法使用Java高级Rest客户端创建索引

时间:2019-01-09 13:15:26

标签: java spring-boot elasticsearch

我正在使用Java高级REST客户端将Elasticsearch集成到我的应用程序中,但无法创建索引

在某个地方,我发现要执行请求,我们需要使用index(request)方法(我在代码中已对此进行了注释),但令人震惊的是,RestHighLevelClient类型不推荐使用index(request)方法。

这是我的代码:

@GetMapping("/createIndex")
public boolean createIndex() throws IOException {


    IndexRequest request = new IndexRequest(
            "muviuser", 
            "user",  
            "1");   
    String jsonString = "{" +
            "\"user\":\"Bran\"," +
            "\"postDate\":\"2018-01-30\"," +
            "\"message\":\"trying out Elasticsearch\"" +
            "}";
    request.source(jsonString, XContentType.JSON);

    //client.index(request);
}

1 个答案:

答案 0 :(得分:2)

正如documentation所解释的,以下是使用高级ES API创建索引的方法:

    CreateIndexRequest request = new CreateIndexRequest(indexName);
    request.mapping("_doc", mappingJson, XContentType.JSON);
    CreateIndexResponse response = client.indices().create(request);

请注意,您的source文档看起来不正确,因为它需要遵循特定的ES request format并带有映射,设置和别名。最好只指定mapping

相关问题