Skip to content

Commit

Permalink
Added withJson to PutIndexTemplateRequest opensearch-project#618
Browse files Browse the repository at this point in the history
Signed-off-by: pranishd1 <8871437+pranishd1@users.noreply.github.com>
  • Loading branch information
pranishd1 committed Oct 7, 2023
1 parent 57dc5cf commit ca4f5fb
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 0 deletions.
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);
}



}

}

0 comments on commit ca4f5fb

Please sign in to comment.