2

The OpenSearch Java Client GitHub repo states that the Java Client is the replacement for the High Level Java Client.

https://github.com/opensearch-project/opensearch-java

I'm trying to create an index with this client, but can't find any way to actually set the mappings, and there are no examples.

RestClient restClient = RestClient
        .builder(new HttpHost(host, port, scheme))
        .build();
Transport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
OpenSearchClient client = new OpenSearchClient(transport);

CreateIndexRequest createIndexRequest = new CreateIndexRequest.Builder()
         .index(index)
         // What do I do here?
         .build();

CreateIndexResponse response = client.indices().create(createIndexRequest);

This is using the OpenSearch Java Client, not the High Level Client.

2 Answers 2

0

The CreateIndexRequest builder does provide a mappings method which takes in a TypeMapping argument.

You should be able to construct a TypeMapping object and then pass it into the builder:

TypeMapping mapping = ...;
CreateIndexRequest createIndexRequest = new CreateIndexRequest.Builder()
         .index(index)
         .mappings(mapping)
         .build();
Sign up to request clarification or add additional context in comments.

Comments

0

I used the last version of

dependencies {
  implementation 'org.opensearch.client:opensearch-rest-client: 2.6.0'
  implementation 'org.opensearch.client:opensearch-java:2.4.0'
}

It creates an index with optional settings and mappings.

So you can use it

for checking:

 var existsIndexRequest = new ExistsRequest.Builder()
            .index(index)
            .build();
 var response = client.indices().exists(existsIndexRequest);

for creating:

var createIndexRequest = new CreateIndexRequest.Builder()
            .index(index)
            .build();
var createIndexResponse = client.indices().create(createIndexRequest);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.