Hi,
may one create a new index to the client by
client.indices().create(c -> c.index("products2"));
May one other be interested in doing so without using lambda-expression. He will need to give a CreateIndexRequest Object, which can be created by delivering a Function<CreateIndexRequest.Builder,ObjectBuilder>.
But how to find a class, that implements this interface?
public void createClient() throws Exception {
zset_username_password_host_port(1);
// https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/current/connecting.html
// https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/current/_basic_authentication.html
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
HttpClientConfigCallback httpClientConfigCallback = new HttpClientConfigCallback() {
@Override
public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
httpClientBuilder.disableAuthCaching();
return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
}
};
// Create the low-level client
RestClientBuilder restClientBuilder = RestClient.builder(new HttpHost(host, port, "https"));
restClientBuilder.setHttpClientConfigCallback(httpClientConfigCallback);
RestClient restClient = restClientBuilder.build();
// Create the transport with a Jackson mapper
ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
// And create the API client
ElasticsearchClient client = new ElasticsearchClient(transport);
create_index_no_lambda_example(client);
create_index_lambda_example(client);
search_example(client);
restClient.close();
}
private void create_index_no_lambda_example(ElasticsearchClient client) throws ElasticsearchException, IOException {
CreateIndexRequest cir = CreateIndexRequest.of(null); // needs an object implementing Function<CreateIndexRequest.Builder,ObjectBuilder<CreateIndexRequest>>
client.indices().create(cir);
}
private void create_index_lambda_example(ElasticsearchClient client) throws ElasticsearchException, IOException {
client.indices().create(c -> c.index("products2"));
}
Thanks - Enomine