How to find Class implementing Function< CreateIndexRequest.Builder, ObjectBuilder< CreateIndexRequest>>?

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

Hi!

I know about this two styles:

   //Builder Style
    CreateIndexRequest createIndexRequest = new CreateIndexRequest.Builder()
        .index("value")
        .build();
    client.indices().create(createIndexRequest);

    //Functional Style
    client.indices().create(CreateIndexRequest.of(
        ir -> ir.index("value")
    ));

Okay thanks. This works. Here are 2 more working examples to create an index:

private void create_index_functional_style(ElasticsearchClient client) throws ElasticsearchException, IOException {
		client.indices().create(CreateIndexRequest.of(ir -> ir.index("products4")));
	}

	private void create_index_builder_style(ElasticsearchClient client) throws ElasticsearchException, IOException {
		CreateIndexRequest createIndexRequest = new CreateIndexRequest.Builder().index("products5").build();
		client.indices().create(createIndexRequest);
	}
	

	private void create_index_lambda_example(ElasticsearchClient client) throws ElasticsearchException, IOException {
		client.indices().create(c -> c.index("products2"));
	}

	private void create_index_no_lambda_example(ElasticsearchClient client) throws ElasticsearchException, IOException {
		CreateIndexRequest cir = CreateIndexRequest.of(new Function<CreateIndexRequest.Builder, ObjectBuilder<CreateIndexRequest>>() {

			@Override
			public ObjectBuilder<CreateIndexRequest> apply(Builder t) {
				return t.index("products3");
			}

		});
		client.indices().create(cir);
	}

Thanks - Enomine

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.