如何通过提供源代码使用新的 Java API 客户端创建索引

Vin*_*eth 5 java elasticsearch

由于 Rest 客户端在 elasticsearch 7.15 中已被弃用,因此我正在将代码从 Java High Level REST Client 迁移到 Java API Client。正如本官方文档中提到的,索引是通过以 json 文件形式提供源(设置和映射)来创建的。最新的 elasticsearch 通过REST API支持相同的功能。但在新的 Java API 客户端中,CreateIndexRequest 没有提供源的选项。怎么做?将映射迁移到 Java(使用 IndexSettings 和 TypeMapping)不是一种选择。

小智 4

要使用 JSON 文件创建索引,我发现这样:

ElasticsearchClient client = new ElasticsearchClient(
                new RestClientTransport(
                    RestClient.builder(new HttpHost("localhost", 9200, "http")).build(),
                    new JacksonJsonpMapper()));

String mappingPath = "yourPath/yourJsonFile.json";
 
JsonpMapper mapper = client._transport().jsonpMapper();
JsonParser parser = mapper.jsonProvider()
            .createParser(new StringReader(
                Files.toString(new ClassPathResource(mappingPath).getFile(), Charsets.UTF_8)));

client.indices()
    .create(createIndexRequest -> createIndexRequest.index(indexName)
        .mappings(TypeMapping._DESERIALIZER.deserialize(parser, mapper)));
Run Code Online (Sandbox Code Playgroud)

然后我尝试对 BulkRequest 做同样的事情,但我没有找到好方法。