Skip to content

Commit

Permalink
Added Index Template Guide
Browse files Browse the repository at this point in the history
Signed-off-by: Theo Truong <[email protected]>
  • Loading branch information
nhtruong committed Apr 3, 2023
1 parent ece5581 commit e55e0e6
Show file tree
Hide file tree
Showing 2 changed files with 183 additions and 1 deletion.
2 changes: 1 addition & 1 deletion guides/index_lifecycle.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ We can also delete multiple indices at once:
```ruby
client.indices.delete(index: [:movies, :paintings, :burner], ignore: 404)
```
Notice that we are passing `ignore: 404` to the request. This tells the client to ignore the `404` error if the index doesn't exist for deletion. Without it, the above `delete` request will throw an error because the `movies` index have already been deleted in the previous example.
Notice that we are passing `ignore: 404` to the request. This tells the client to ignore the `404` error if the index doesn't exist for deletion. Without it, the above `delete` request will throw an error because the `movies` index has already been deleted in the previous example.

## Cleanup

Expand Down
182 changes: 182 additions & 0 deletions guides/index_template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
# Index Template
Index templates are a convenient way to define settings, mappings, and aliases for one or more indices when they are created. In this guide, you'll learn how to create an index template and apply it to an index.

## Setup

Assuming you have OpenSearch running locally on port 9200, you can create a client instance
with the following code:
```ruby
require 'opensearch-ruby'
client = OpenSearch::Client.new({ host: 'localhost' })
```

## 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:

```ruby
client.indices.put_index_template(
name: :books,
body: {
index_patterns: ['books-*'],
template: {
settings: {
index: {
number_of_shards: 3,
number_of_replicas: 0
}
},
mappings: {
properties: {
title: { type: 'text' },
author: { type: 'text' },
published_on: { type: 'date' },
pages: { type: 'integer' }
}
}
}
}
)
```

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:

```ruby
client.indices.create(index: 'books-nonfiction')
puts client.indices.get(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:

```ruby
client.indices.put_index_template(
name: 'books',
body: {
index_patterns: ['books-*'],
priority: 0, # default priority
template: {
settings: {
index: {
number_of_shards: 3,
number_of_replicas: 0
}
}
}
}
)

client.indices.put_index_template(
name: 'books-fiction',
body: {
index_patterns: ['books-fiction-*'],
priority: 1, # higher priority than the `books` template
template: {
settings: {
index: {
number_of_shards: 1,
number_of_replicas: 1
}
}
}
}
)
```

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

```ruby
client.indices.create(index: 'books-fiction-romance')
puts client.indices.get(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:

```ruby
client.cluster.put_component_template(
name: 'books_mappings',
body: {
template: {
mappings: {
properties: {
title: { type: 'text' },
author: { type: 'text' },
published_on: { type: 'date' },
pages: { type: 'integer' }
}
}
}
}
)

client.indices.put_index_template(
name: 'books',
body: {
index_patterns: ['books-*'],
composed_of: ['books_mappings'], # use the `books_mappings` component template
priority: 0,
template: {
settings: {
index: {
number_of_shards: 3,
number_of_replicas: 0
}
}
}
}
)

client.indices.put_index_template(
name: 'books',
body: {
index_patterns: ['books-*'],
composed_of: ['books_mappings'], # use the `books_mappings` component template
priority: 1,
template: {
settings: {
index: {
number_of_shards: 1,
number_of_replicas: 1
}
}
}
}
)
```

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:

```ruby
client.indices.create(index: 'books-fiction-horror')
puts client.indices.get(index: 'books-fiction-horror')
```

### Get an Index Template
You can get an index template with the `get_index_template` API action:

```ruby
puts client.indices.get_index_template(name: 'books')
```

### Delete an Index Template
You can delete an index template with the `delete_template` API action:

```ruby
client.indices.delete_index_template(name: 'books')
```

## Cleanup
Let's delete all resources created in this guide:

```ruby
client.indices.delete(index: 'books-*')
client.indices.delete_index_template(name: 'books-fiction')
client.cluster.delete_component_template(name: 'books_mappings')
```




0 comments on commit e55e0e6

Please sign in to comment.