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.

Kong
  • 8,792
  • 15
  • 68
  • 98

2 Answers2

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();
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);
makson
  • 1,975
  • 3
  • 23
  • 31