Hi,
How can I create an index using a json file ?
Below code is not creating mappings for me:
import jakarta.json.stream.JsonParser;
import org.opensearch.client.json.JsonpMapper;
import org.opensearch.client.opensearch.OpenSearchClient;
import org.opensearch.client.opensearch._types.mapping.TypeMapping;
public void createIndex() {
try {
JsonpMapper mapper = openSearchClient._transport().jsonpMapper();
JsonParser parser = mapper.jsonProvider()
.createParser(new StringReader(Files.readString(Path.of(Objects.requireNonNull(getClass()
.getClassLoader()
.getResource(openSearchProperties.getIndexSource()))
.getPath()))));
CreateIndexRequest createIndexRequest = new CreateIndexRequest.Builder().index(openSearchProperties.getIndexName())
.mappings(TypeMapping._DESERIALIZER.deserialize(parser, mapper))
.build();
CreateIndexResponse createIndexResponse = openSearchClient.indices().create(createIndexRequest);
if (FALSE.equals(createIndexResponse.acknowledged())) {
throw new RuntimeException("Create index response is not acknowledged");
}
} catch (Exception ex) {
throw new RuntimeException("Failed to create index");
}
}