Reputation: 135
I'm migrating our code from Java High Level REST Client to Java API Client since the Rest client got deprecated in elasticsearch 7.15. As mentioned in this official documentation, indexes were created by providing the source (settings and mapping) as a json file. The same is supported in the latest elasticsearch via REST API. But in the new Java API Client, CreateIndexRequest doesn't have the option to provide the source. How to do it? Migrating the mapping to Java (using IndexSettings and TypeMapping) is not an option.
Upvotes: 5
Views: 4048
Reputation: 51
to create an index with a JSON file I found this way:
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)));
Then I tried to do same thing for BulkRequest but I didn't find the good way.
Upvotes: 4