Skip to content

Commit

Permalink
Review feedback
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Farr <[email protected]>
  • Loading branch information
Xtansia committed Nov 21, 2023
1 parent 23a262b commit c6e3093
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 30 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ jobs:
path: client/build/output/*

integration-opensearch-unreleased:
if: false # TODO: Temporarily disabled due to failures building & running OpenSearch from source, pending investigation & fixes
if: false # TODO: Temporarily disabled due to failures building & running OpenSearch from source, pending investigation & fixes (https://github.com/opensearch-project/opensearch-net/issues/268)
name: Integration OpenSearch Unreleased
runs-on: ubuntu-latest
strategy:
Expand Down
62 changes: 33 additions & 29 deletions guides/index-template.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,22 @@ var putTemplate = await client.Indices.PutComposableTemplateAsync("books", d =>
.Date(f => f.Name(b => b.PublishedOn))
.Number(f => f.Name(b => b.Pages).Type(NumberType.Integer))
))));
Debug.Assert(putTemplate.IsValid, putTemplate.DebugInformation);
Console.WriteLine($"Put Template: {putTemplate.IsValid}");
// -> Put Template: 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 createIndex = await client.Indices.CreateAsync("books-nonfiction");
Debug.Assert(createIndex.IsValid, createIndex.DebugInformation);
Console.WriteLine($"Create Index: {createIndex.IsValid}");
// -> Create Index: True
var getIndex = await client.Indices.GetAsync("books-nonfiction");
Debug.Assert(
getIndex.Indices["books-nonfiction"].Mappings.Properties["pages"].Type == "integer",
"`pages` property should have `integer` type");
Console.WriteLine($"`pages` property type: {getIndex.Indices["books-nonfiction"].Mappings.Properties["pages"].Type}");
// -> `pages` property type: integer
```


### Multiple Index Templates

```csharp
Expand All @@ -75,7 +75,8 @@ putTemplate = await client.Indices.PutComposableTemplateAsync("books", d => d
.Settings(s => s
.NumberOfShards(3)
.NumberOfReplicas(0))));
Debug.Assert(putTemplate.IsValid, putTemplate.DebugInformation);
Console.WriteLine($"Put Template: {putTemplate.IsValid}");
// -> Put Template: True
putTemplate = await client.Indices.PutComposableTemplateAsync("books-fiction", d => d
.IndexPatterns("books-fiction-*")
Expand All @@ -84,19 +85,20 @@ putTemplate = await client.Indices.PutComposableTemplateAsync("books-fiction", d
.Settings(s => s
.NumberOfShards(1)
.NumberOfReplicas(1))));
Debug.Assert(putTemplate.IsValid, putTemplate.DebugInformation);
Console.WriteLine($"Put Template: {putTemplate.IsValid}");
// -> Put Template: True
```

When we create an index named `books-fiction-romance`, OpenSearch will apply the `books-fiction-*` template's settings to the index:

```csharp
createIndex = await client.Indices.CreateAsync("books-fiction-romance");
Debug.Assert(createIndex.IsValid, createIndex.DebugInformation);
Console.WriteLine($"Create Index: {createIndex.IsValid}");
// -> Create Index: True
getIndex = await client.Indices.GetAsync("books-fiction-romance");
Debug.Assert(
getIndex.Indices["books-fiction-romance"].Settings.NumberOfShards == 1,
"`books-fiction-romance` index should have 1 shard");
Console.WriteLine($"Number of shards: {getIndex.Indices["books-fiction-romance"].Settings.NumberOfShards}");
// -> Number of shards: 1
```


Expand All @@ -113,7 +115,8 @@ var putComponentTemplate = await client.Cluster.PutComponentTemplateAsync("books
.Date(f => f.Name(b => b.PublishedOn))
.Number(f => f.Name(b => b.Pages).Type(NumberType.Integer))
))));
Debug.Assert(putComponentTemplate.IsValid, putComponentTemplate.DebugInformation);
Console.WriteLine($"Put Component Template: {putComponentTemplate.IsValid}");
// -> Put Component Template: True
putTemplate = await client.Indices.PutComposableTemplateAsync("books", d => d
.IndexPatterns("books-*")
Expand All @@ -123,7 +126,8 @@ putTemplate = await client.Indices.PutComposableTemplateAsync("books", d => d
.Settings(s => s
.NumberOfShards(3)
.NumberOfReplicas(0))));
Debug.Assert(putTemplate.IsValid, putTemplate.DebugInformation);
Console.WriteLine($"Put Template: {putTemplate.IsValid}");
// -> Put Template: True
putTemplate = await client.Indices.PutComposableTemplateAsync("books-fiction", d => d
.IndexPatterns("books-fiction-*")
Expand All @@ -133,40 +137,40 @@ putTemplate = await client.Indices.PutComposableTemplateAsync("books-fiction", d
.Settings(s => s
.NumberOfShards(1)
.NumberOfReplicas(1))));
Debug.Assert(putTemplate.IsValid, putTemplate.DebugInformation);
Console.WriteLine($"Put Template: {putTemplate.IsValid}");
// -> Put Template: 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
createIndex = await client.Indices.CreateAsync("books-fiction-horror");
Debug.Assert(createIndex.IsValid, createIndex.DebugInformation);
Console.WriteLine($"Create Index: {createIndex.IsValid}");
// -> Create Index: True
getIndex = await client.Indices.GetAsync("books-fiction-horror");
Debug.Assert(
getIndex.Indices["books-fiction-horror"].Settings.NumberOfShards == 1,
"`books-fiction-horror` index should have 1 shard");
Debug.Assert(
getIndex.Indices["books-fiction-horror"].Mappings.Properties["pages"].Type == "integer",
"`pages` property should have `integer` type");
Console.WriteLine($"Number of shards: {getIndex.Indices["books-fiction-horror"].Settings.NumberOfShards}");
Console.WriteLine($"`pages` property type: {getIndex.Indices["books-fiction-horror"].Mappings.Properties["pages"].Type}");
// -> Number of shards: 1
// -> `pages` property type: integer
```

### Get an Index Template
You can get an index template with the `GetComposableTemplate` API action. The following example gets the `books` index template:

```csharp
var getTemplate = await client.Indices.GetComposableTemplateAsync("books");
Debug.Assert(
getTemplate.IndexTemplates.First().IndexTemplate.IndexPatterns.First() == "books-*",
"First index pattern should be `books-*`");
Console.WriteLine($"First index pattern: {getTemplate.IndexTemplates.First().IndexTemplate.IndexPatterns.First()}");
// -> First index pattern: books-*
```

### Delete an Index Template
You can delete an index template with the `DeleteComposableTemplate` API action. The following example deletes the `books` index template:

```csharp
var deleteTemplate = await client.Indices.DeleteComposableTemplateAsync("books");
Debug.Assert(deleteTemplate.IsValid, deleteTemplate.DebugInformation);
Console.WriteLine($"Delete Template: {deleteTemplate.IsValid}");
// -> Delete Template: True
```


Expand All @@ -175,11 +179,11 @@ Let's delete all resources created in this guide:

```csharp
var deleteIndex = await client.Indices.DeleteAsync("books-*");
Debug.Assert(deleteIndex.IsValid, deleteIndex.DebugInformation);
Console.WriteLine($"Delete Index: {deleteIndex.IsValid}");

deleteTemplate = await client.Indices.DeleteComposableTemplateAsync("books-fiction");
Debug.Assert(deleteTemplate.IsValid, deleteTemplate.DebugInformation);
Console.WriteLine($"Delete Template: {deleteTemplate.IsValid}");

var deleteComponentTemplate = await client.Cluster.DeleteComponentTemplateAsync("books_mappings");
Debug.Assert(deleteComponentTemplate.IsValid, deleteComponentTemplate.DebugInformation);
Console.WriteLine($"Delete Component Template: {deleteComponentTemplate.IsValid}");
```
23 changes: 23 additions & 0 deletions samples/Samples/IndexTemplate/IndexTemplateSample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,19 @@ protected override async Task Run(IOpenSearchClient client)
.Number(f => f.Name(b => b.Pages).Type(NumberType.Integer))
))));
Debug.Assert(putTemplate.IsValid, putTemplate.DebugInformation);
Console.WriteLine($"Put Template: {putTemplate.IsValid}");

// Confirm mapping

var createIndex = await client.Indices.CreateAsync("books-nonfiction");
Debug.Assert(createIndex.IsValid, createIndex.DebugInformation);
Console.WriteLine($"Create Index: {createIndex.IsValid}");

var getIndex = await client.Indices.GetAsync("books-nonfiction");
Debug.Assert(
getIndex.Indices["books-nonfiction"].Mappings.Properties["pages"].Type == "integer",
"`pages` property should have `integer` type");
Console.WriteLine($"`pages` property type: {getIndex.Indices["books-nonfiction"].Mappings.Properties["pages"].Type}");

// Multiple index templates

Expand All @@ -54,6 +58,7 @@ protected override async Task Run(IOpenSearchClient client)
.NumberOfShards(3)
.NumberOfReplicas(0))));
Debug.Assert(putTemplate.IsValid, putTemplate.DebugInformation);
Console.WriteLine($"Put Template: {putTemplate.IsValid}");

putTemplate = await client.Indices.PutComposableTemplateAsync("books-fiction", d => d
.IndexPatterns("books-fiction-*")
Expand All @@ -63,15 +68,19 @@ protected override async Task Run(IOpenSearchClient client)
.NumberOfShards(1)
.NumberOfReplicas(1))));
Debug.Assert(putTemplate.IsValid, putTemplate.DebugInformation);
Console.WriteLine($"Put Template: {putTemplate.IsValid}");

// Validate settings

createIndex = await client.Indices.CreateAsync("books-fiction-romance");
Debug.Assert(createIndex.IsValid, createIndex.DebugInformation);
Console.WriteLine($"Create Index: {createIndex.IsValid}");

getIndex = await client.Indices.GetAsync("books-fiction-romance");
Debug.Assert(
getIndex.Indices["books-fiction-romance"].Settings.NumberOfShards == 1,
"`books-fiction-romance` index should have 1 shard");
Console.WriteLine($"Number of shards: {getIndex.Indices["books-fiction-romance"].Settings.NumberOfShards}");

// Component templates

Expand All @@ -85,6 +94,7 @@ protected override async Task Run(IOpenSearchClient client)
.Number(f => f.Name(b => b.Pages).Type(NumberType.Integer))
))));
Debug.Assert(putComponentTemplate.IsValid, putComponentTemplate.DebugInformation);
Console.WriteLine($"Put Component Template: {putComponentTemplate.IsValid}");

putTemplate = await client.Indices.PutComposableTemplateAsync("books", d => d
.IndexPatterns("books-*")
Expand All @@ -95,6 +105,7 @@ protected override async Task Run(IOpenSearchClient client)
.NumberOfShards(3)
.NumberOfReplicas(0))));
Debug.Assert(putTemplate.IsValid, putTemplate.DebugInformation);
Console.WriteLine($"Put Template: {putTemplate.IsValid}");

putTemplate = await client.Indices.PutComposableTemplateAsync("books-fiction", d => d
.IndexPatterns("books-fiction-*")
Expand All @@ -105,38 +116,50 @@ protected override async Task Run(IOpenSearchClient client)
.NumberOfShards(1)
.NumberOfReplicas(1))));
Debug.Assert(putTemplate.IsValid, putTemplate.DebugInformation);
Console.WriteLine($"Put Template: {putTemplate.IsValid}");

// Validate settings & mappings
createIndex = await client.Indices.CreateAsync("books-fiction-horror");
Debug.Assert(createIndex.IsValid, createIndex.DebugInformation);
Console.WriteLine($"Create Index: {createIndex.IsValid}");

getIndex = await client.Indices.GetAsync("books-fiction-horror");
Debug.Assert(
getIndex.Indices["books-fiction-horror"].Settings.NumberOfShards == 1,
"`books-fiction-horror` index should have 1 shard");
Debug.Assert(
getIndex.Indices["books-fiction-horror"].Mappings.Properties["pages"].Type == "integer",
"`pages` property should have `integer` type");
Console.WriteLine($"Number of shards: {getIndex.Indices["books-fiction-horror"].Settings.NumberOfShards}");
Console.WriteLine($"`pages` property type: {getIndex.Indices["books-fiction-horror"].Mappings.Properties["pages"].Type}");

// Get index template

var getTemplate = await client.Indices.GetComposableTemplateAsync("books");
Debug.Assert(
getTemplate.IndexTemplates.First().IndexTemplate.IndexPatterns.First() == "books-*",
"First index pattern should be `books-*`");
Console.WriteLine($"First index pattern: {getTemplate.IndexTemplates.First().IndexTemplate.IndexPatterns.First()}");

// Delete index template

var deleteTemplate = await client.Indices.DeleteComposableTemplateAsync("books");
Debug.Assert(deleteTemplate.IsValid, deleteTemplate.DebugInformation);
Console.WriteLine($"Delete Template: {deleteTemplate.IsValid}");

// Cleanup

var deleteIndex = await client.Indices.DeleteAsync("books-*");
Debug.Assert(deleteIndex.IsValid, deleteIndex.DebugInformation);
Console.WriteLine($"Delete Index: {deleteIndex.IsValid}");

deleteTemplate = await client.Indices.DeleteComposableTemplateAsync("books-fiction");
Debug.Assert(deleteTemplate.IsValid, deleteTemplate.DebugInformation);
Console.WriteLine($"Delete Template: {deleteTemplate.IsValid}");

var deleteComponentTemplate = await client.Cluster.DeleteComponentTemplateAsync("books_mappings");
Debug.Assert(deleteComponentTemplate.IsValid, deleteComponentTemplate.DebugInformation);
Console.WriteLine($"Delete Component Template: {deleteComponentTemplate.IsValid}");
}

private class Book
Expand Down

0 comments on commit c6e3093

Please sign in to comment.