Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

618 withjson support method for put index template request #657

1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
- Added support for "cjk" analyzer ([#604](https://github.com/opensearch-project/opensearch-java/pull/604))
- Added support for wrapper queries ([#630](https://github.com/opensearch-project/opensearch-java/pull/630))
- Added size attribute to MultiTermsAggregation ([#627](https://github.com/opensearch-project/opensearch-java/pull/627))
- Added support for "withJson" in PutIndexTemplateRequest ([#657](https://github.com/opensearch-project/opensearch-java/pull/657))

### Dependencies
- Bumps `org.ajoberstar.grgit:grgit-gradle` from 5.0.0 to 5.2.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@

package org.opensearch.client.opensearch.indices;

import jakarta.json.stream.JsonParser;
import org.opensearch.client.json.jsonb.JsonbJsonpMapper;
import org.opensearch.client.opensearch._types.ErrorResponse;
import org.opensearch.client.opensearch._types.RequestBase;
import org.opensearch.client.opensearch.indices.put_index_template.IndexTemplateMapping;
Expand All @@ -49,10 +51,13 @@
import org.opensearch.client.util.ObjectBuilderBase;
import jakarta.json.stream.JsonGenerator;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Supplier;
import javax.annotation.Nullable;

// typedef: indices.put_index_template.Request
Expand Down Expand Up @@ -375,6 +380,32 @@ public final Builder version(@Nullable Long value) {
return this;
}

/**
* Re-writes previous builder fields ( template, index_patterns, version, priority, data_streams )
*
* @throws NullPointerException
* if value is null.
*/
public final Builder withJson(InputStream value){
assert value != null;
JsonpMapper mapper = new JsonbJsonpMapper();
JsonParser parser = mapper.jsonProvider().createParser(new InputStreamReader(value));
var builder = ObjectBuilderDeserializer.lazy(
Builder::new,
PutIndexTemplateRequest::setupPutIndexTemplateRequestDeserializer,
_builder -> _builder
).deserialize(parser,mapper);

template(builder.template);
indexPatterns(builder.indexPatterns);
version(builder.version);
priority(builder.priority);
dataStream(builder.dataStream);

return this;
}


/**
* Builds a {@link PutIndexTemplateRequest}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@

package org.opensearch.client.opensearch.integTest;

import jakarta.json.stream.JsonParser;
import org.opensearch.client.json.JsonpMapper;
import org.opensearch.client.json.jsonb.JsonbJsonpMapper;
import org.opensearch.client.opensearch.OpenSearchAsyncClient;
import org.opensearch.client.opensearch._types.OpenSearchException;
import org.opensearch.client.opensearch._types.mapping.Property;
import org.opensearch.client.opensearch.indices.CreateDataStreamResponse;
import org.opensearch.client.opensearch.indices.CreateIndexResponse;
import org.opensearch.client.opensearch.indices.DataStream;
Expand All @@ -21,11 +25,16 @@
import org.opensearch.client.opensearch.indices.GetIndexRequest;
import org.opensearch.client.opensearch.indices.GetIndexResponse;
import org.opensearch.client.opensearch.indices.GetIndicesSettingsRequest;
import org.opensearch.client.opensearch.indices.DeleteIndexTemplateResponse;
import org.opensearch.client.opensearch.indices.GetIndexTemplateResponse;
import org.opensearch.client.opensearch.indices.IndexState;
import org.opensearch.client.opensearch.indices.PutIndexTemplateResponse;
import org.opensearch.client.opensearch.indices.get_index_template.IndexTemplate;
import org.opensearch.common.settings.Settings;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -205,4 +214,93 @@ public void testGetNotExistingIndexAlias() throws Exception {
"alias [alias_not_exists] missing");
}
}

public void testWithJsonPutIndexTemplateRequest() throws IOException {

String jsonTemplate ="{ " +
" \"index_patterns\": [ " +
" \"logs-2023-01-*\"" +
" ], " +
" \"composed_of\":[], "+
" \"template\": { " +
" \"aliases\": { " +
" \"my_logs\": {} " +
" }, " +
" \"settings\": { " +
" \"number_of_shards\": 2, " +
" \"number_of_replicas\": 1 " +
" }, " +
" \"mappings\": { " +
" \"properties\": { " +
" \"timestamp\": { " +
" \"type\": \"date\"," +
" \"format\": \"yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis\"" +
" },\n" +
" \"value\": { " +
" \"type\": \"double\" " +
" } " +
" } " +
" } " +
" } " +
"}";

final var pTemplateName = "daily_logs";
InputStream pTemplateJson = new ByteArrayInputStream(jsonTemplate.getBytes());

// create an index template before creating data streams
PutIndexTemplateResponse putIndexTemplateResponse = javaClient().indices()
.putIndexTemplate(b -> b.name(pTemplateName).withJson(pTemplateJson));
assertTrue(putIndexTemplateResponse.acknowledged());


// verify the settings
GetIndexTemplateResponse getIndexTemplateResponse = javaClient().indices()
.getIndexTemplate(b->b.name(pTemplateName));
assertEquals(1, getIndexTemplateResponse.indexTemplates().size());
assertEquals("logs-2023-01-*",getIndexTemplateResponse.indexTemplates()
.get(0).indexTemplate().indexPatterns().get(0));

IndexTemplate responseIndexTemplate = getIndexTemplateResponse.indexTemplates().get(0).indexTemplate();

JsonpMapper mapper = new JsonbJsonpMapper();
JsonParser parser = mapper.jsonProvider().createParser(new ByteArrayInputStream(jsonTemplate.getBytes()));
IndexTemplate indexTemplate = IndexTemplate._DESERIALIZER.deserialize(parser,mapper);

assertEquals(indexTemplate.priority(),responseIndexTemplate.priority());
assert indexTemplate.template() != null;
assert responseIndexTemplate.template() != null;
assertEquals(indexTemplate.template().aliases().size(),responseIndexTemplate.template().aliases().size());
assert indexTemplate.template().mappings() != null;
assert responseIndexTemplate.template().mappings() != null;
assertEquals(indexTemplate.template().mappings().size(),responseIndexTemplate.template().mappings().size());

var mappings = indexTemplate.template().mappings().properties();
var mappingValues = mappings.get("value");
assertEquals(Property.Kind.Double,mappingValues._kind());

var timestampValues = mappings.get("timestamp");
assertEquals("yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis",timestampValues.date().format());

var indicesSettings = responseIndexTemplate.template().settings().get("index").toJson().asJsonObject();
assertEquals("\"2\"",indicesSettings.get("number_of_shards").toString());
assertEquals("\"1\"",indicesSettings.get("number_of_replicas").toString());

// delete index template index
DeleteIndexTemplateResponse deleteIndexTemplateResponse = javaClient().indices()
.deleteIndexTemplate(b -> b.name(pTemplateName));
assertTrue(deleteIndexTemplateResponse.acknowledged());

// verify index template is deleted
try {
javaClient().indices().getIndexTemplate(b -> b.name(pTemplateName));
fail();
} catch (OpenSearchException ex) {
assertNotNull(ex);
assertEquals(ex.status(), 404);
}



}

}