From ac14a16e3a9d1fd67b50bab6cd09279114065fa5 Mon Sep 17 00:00:00 2001 From: Djcarrillo6 Date: Tue, 10 Oct 2023 21:56:39 -0700 Subject: [PATCH] Added new guide for using index templates with the low-level client. Signed-off-by: Djcarrillo6 --- guides/index-template.md | 304 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 304 insertions(+) create mode 100644 guides/index-template.md diff --git a/guides/index-template.md b/guides/index-template.md new file mode 100644 index 0000000000..fd11da505d --- /dev/null +++ b/guides/index-template.md @@ -0,0 +1,304 @@ +# Index Template +Index templates allow you to define default settings, mappings, and aliases for one or more indices during their creation. This guide will teach you how to create index templates and apply them to indices using the OpenSearch .NET client. + +## Setup +**This guide is designed specifically for the OpenSearch.Net low-level client. At the time this guide was written, none of the methods referenced in the examples below exist yet in the high-level OpenSearch.Client. These examples can be used as a model for the time at which the high-level client implementations are being developed.** + + +Assuming you have OpenSearch running locally on port 9200, you can create a client instance with the following code: + +```csharp +using OpenSearch.Net; +using OpenSearch.Net.Specification.ClusterApi; + +var node = new Uri("https://localhost:9200"); +var config = new ConnectionConfiguration(node) + .ServerCertificateValidationCallback(CertificateValidations.AllowAll) + .BasicAuthentication("admin", "admin"); + +var client = new OpenSearchLowLevelClient(config);; + +// The PutComponentTemplate method is not yet implemented in the low-level client, so you need to use an instance of the LowLevelClusterNamespace class which contains the PutComponentTemplate method. +LowLevelClusterNamespace cluster = client.Cluster; +``` + + +# Index Template API Actions + + +## Create an Index Template +You can create an index template to define default settings and mappings for indices of certain patterns. The following example creates an index template named `books` with default settings and mappings for indices of the `books-*` pattern: + +```csharp +var createResponse = client.Indices.PutTemplateForAll("books", PostData.String(@"{ + ""index_patterns"": [""books-*""], + ""settings"": { + ""number_of_shards"": 3, + ""number_of_replicas"": 0 + }, + ""mappings"": { + ""properties"": { + ""title"": { ""type"": ""text"" }, + ""author"": { ""type"": ""text"" }, + ""published_on"": { ""type"": ""date"" }, + ""pages"": { ""type"": ""integer"" } + } + } +}")).Body; + +Console.WriteLine($"Create response: {createResponse}") // Create response: {"acknowledged":true} +``` + +Now, when you create an index that matches the `books-*` pattern, OpenSearch will automatically apply the template's settings and mappings to the index. Let's create an index named books-nonfiction and verify that its settings and mappings match those of the template: + +```csharp +var createResponse = client.Indices.Create("books-nonfiction", PostData.String(@"{ + ""settings"": { + ""number_of_shards"": 3, + ""number_of_replicas"": 0 + }, + ""mappings"": { + ""properties"": { + ""title"": { ""type"": ""text"" }, + ""author"": { ""type"": ""text"" }, + ""published_on"": { ""type"": ""date"" }, + ""pages"": { ""type"": ""integer"" } + } + } +}")).Body; + +Console.WriteLine($"Create response: {createResponse}"); // Create response: {"acknowledged":true,"shards_acknowledged":true,"index":"books-nonfiction"} +``` + + +## Multiple Index Templates +If multiple index templates match the index's name, OpenSearch will apply the template with the highest `priority`. The following example creates two index templates named `books-*` and `books-fiction-*` with different settings: + +```csharp +var createResponseOne = client.Indices.PutTemplateV2ForAll("books", PostData.String(@"{ + ""index_patterns"": [""books-*""], + // ""priority"": 1, + ""settings"": { + ""number_of_shards"": 3, + ""number_of_replicas"": 0 + } +}")).Body; + +Console.WriteLine($"Create response: {createResponseOne}"); // Create response: {"acknowledged":true} + +var createResponseTwo = client.Indices.PutTemplateV2ForAll("books", PostData.String(@"{ + ""index_patterns"": [""books-fiction-*""], + ""priority"": 2, + ""settings"": { + ""number_of_shards"": 3, + ""number_of_replicas"": 0 + } +}")).Body; + +Console.WriteLine($"Create response: {createResponseTwo}"); // Create response: {"acknowledged":true} +``` + +When we create an index named `books-fiction-romance`, OpenSearch will apply the `books-fiction-*` template's settings to the index: + +```csharp +var createResponse = client.Indices.Create("books-fiction-romance", PostData.String(@"{ + ""settings"": { + ""number_of_shards"": 3, + ""number_of_replicas"": 0 + }, + ""mappings"": { + ""properties"": { + ""title"": { ""type"": ""text"" }, + ""author"": { ""type"": ""text"" }, + ""published_on"": { ""type"": ""date"" }, + ""pages"": { ""type"": ""integer"" } + } + } +}")).Body; + +Console.WriteLine($"Create response: {createResponse}") // Create response: {"acknowledged":true,"shards_acknowledged":true,"index":"books-fiction-romance"} +``` + + +# Composable Index Templates +Composable index templates are a new type of index template that allow you to define multiple component templates and compose them into a final template. The following example creates a component template named `books_mappings` with default mappings for indices of the `books-*` and `books-fiction-*` patterns: + +```csharp +var createResponse = client.Indices.PutComponentTemplate("books_mappings", PostData.String(@"{ + ""index_patterns"": [""books-*"", ""books-fiction-*""], + ""mappings"": { + ""properties"": { + ""title"": { ""type"": ""text"" }, + ""author"": { ""type"": ""text"" }, + ""published_on"": { ""type"": ""date"" }, + ""pages"": { ""type"": ""integer"" } + } + } +}")).Body; + +Console.WriteLine($"Create response: {createResponse}"); // Create response: {"acknowledged":true} + +// book-* +var createResponse = client.Indices.PutTemplateV2ForAll("books", PostData.String(@"{ + ""index_patterns"": [""books-*""], + ""template"": { + ""settings"": { + ""number_of_shards"": 3, + ""number_of_replicas"": 0 + } + }, + ""priority"": 1 +}")).Body; + +Console.WriteLine($"Create response: {createResponse}"); // Create response: {"acknowledged":true} + +var createResponseTwo = client.Indices.PutTemplateV2ForAll("books", PostData.String(@"{ + ""index_patterns"": [""books-fiction-*""], + ""template"": { + ""settings"": { + ""number_of_shards"": 3, + ""number_of_replicas"": 0 + } + }, + ""priority"": 2 +}")).Body; + +Console.WriteLine($"Create response: {createResponseTwo}"); // Create response: {"acknowledged":true} +``` + + +```csharp +var createResponse = client.Indices.Create("books-fiction-horror", PostData.String(@"{ + ""settings"": { + ""number_of_shards"": 3, + ""number_of_replicas"": 0 + }, + ""mappings"": { + ""properties"": { + ""title"": { ""type"": ""text"" }, + ""author"": { ""type"": ""text"" }, + ""published_on"": { ""type"": ""date"" }, + ""pages"": { ""type"": ""integer"" } + } + } +}")).Body; + +Console.WriteLine($"Create response: {createResponse}") // Create response: {"acknowledged":true,"shards_acknowledged":true,"index":"books-fiction-horror"} +``` + + +# Composable Index Templates +Composable index templates are a new type of index template that allow you to define multiple component templates and compose them into a final template. The following example creates a component template named books_mappings with default mappings for indices of the `books-*` and `books-fiction-*` patterns: + +```csharp +var componentTemplateCreateResponse = cluster.PutComponentTemplate("books_mappings", PostData.String(@"{ + ""template"": { + ""mappings"": { + ""properties"": { + ""title"": { ""type"": ""text"" }, + ""author"": { ""type"": ""text"" }, + ""published_on"": { ""type"": ""date"" }, + ""pages"": { ""type"": ""integer"" } + } + } + } + }")).Body; + + Console.WriteLine($"Create response: {componentTemplateCreateResponse}"); // Create response: {"acknowledged":true} + + var createResponse = client.Indices.PutTemplateV2ForAll("books", PostData.String(@"{ + ""index_patterns"": [""books-*""], + ""composed_of"": [""books_mappings""], + ""template"": { + ""settings"": { + ""number_of_shards"": 3, + ""number_of_replicas"": 0 + } + }, + ""priority"": 1, + ""_meta"": { + ""description"": ""Using component template books_mappings"" + } + }")).Body; + + Console.WriteLine($"Create response: {createResponse}"); // Create response: {"acknowledged":true} + + var createResponseTwo = client.Indices.PutTemplateV2ForAll("books", PostData.String(@"{ + ""index_patterns"": [""books-fiction-*""], + ""composed_of"": [""books_mappings""], + ""template"": { + ""settings"": { + ""number_of_shards"": 3, + ""number_of_replicas"": 0 + } + }, + ""priority"": 2, + ""_meta"": { + ""description"": ""Using component template books_mappings"" + } + }")).Body; + + Console.WriteLine($"Create response: {createResponseTwo}"); // Create response: {"acknowledged":true} +``` + +When we create an index named `books-fiction-horror`, OpenSearch will apply the `books-fiction-*` template's settings, and `books_mappings` template mappings to the index: + +```csharp +var createResponse = client.Indices.Create("books-fiction-horror", PostData.String(@"{ + ""settings"": { + ""number_of_shards"": 3, + ""number_of_replicas"": 0 + }, + ""mappings"": { + ""properties"": { + ""title"": { ""type"": ""text"" }, + ""author"": { ""type"": ""text"" }, + ""published_on"": { ""type"": ""date"" }, + ""pages"": { ""type"": ""integer"" } + } + } + }")).Body; + + Console.WriteLine($"Create response: {createResponse}"); // Create response: {"acknowledged":true,"shards_acknowledged":true,"index":"books-fiction-horror"} +``` + +# Get an Index Template +You can get an index template with the `get_index_template` API action. The following example gets the `books` index template: + +```csharp +var getResponse = client.Indices.GetTemplateForAll("books").Body; + +Console.WriteLine($"Get response: {getResponse}"); // Get response: {"books":{"order":0,"index_patterns":["books-*"],"settings":{"index":{"number_of_shards":"3","number_of_replicas":"0"}},"mappings":{},"aliases":{}}} +``` + +# Delete an Index Template +You can delete an index template with the `delete_index_template` API action. The following example deletes the `books` index template: + +```csharp +var deleteResponse = client.Indices.DeleteTemplateForAll("books").Body; + +Console.WriteLine($"Delete response: {deleteResponse}"); // Delete response: {"acknowledged":true} +``` + + +# Cleanup +You can delete the index we created earlier with the following code: + +```csharp +// delete indices +var deleteIndexResponse = client.Indices.Delete("books-fiction-horror").Body; + +Console.WriteLine($"Delete index response: {deleteIndexResponse}"); // Delete index response: {"acknowledged":true} + + +// delete index template +var deleteTemplateResponse = client.Indices.DeleteTemplateForAll("books_mappings").Body; + +Console.WriteLine($"Delete template response: {deleteTemplateResponse}"); // Delete template response: {"acknowledged":true} + + +// delete component template +var deleteTemplateResponse2 = client.Indices.DeleteTemplateForAll("books").Body; + +Console.WriteLine($"Delete template response: {deleteTemplateResponse2}"); // Delete template response: {"acknowledged":true} +``` \ No newline at end of file