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

[GUIDE] Add index_template guide #289

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
- Adds `bulk` guide ([#292](https://github.com/opensearch-project/opensearch-go/pull/292))
- Adds `search` guide ([#291](https://github.com/opensearch-project/opensearch-go/pull/291))
- Adds `document_lifecycle` guide ([#290](https://github.com/opensearch-project/opensearch-go/pull/290))
- Adds `index_template` guide ([#289](https://github.com/opensearch-project/opensearch-go/pull/289))

### Changed

Expand Down
262 changes: 262 additions & 0 deletions guides/index_template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,262 @@
# 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:

```go
package main

import (
"github.com/opensearch-project/opensearch-go/v2"
"log"
)

func main() {
client, err := opensearch.NewDefaultClient()
if err != nil {
log.Printf("error occurred: [%s]", err.Error())
}
log.Printf("response: [%+v]", client)
}
```

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

```go
body := strings.NewReader(`{
"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" }
}
}
}
}`)

res, err := client.Indices.PutIndexTemplate("books", body)
if err != nil {
log.Printf("error occurred: [%s]", err.Error())
}
log.Printf("response: [%+v]", res)
```

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:

```go
res, err = client.Indices.Create("books-nonfiction")
if err != nil {
log.Printf("error occurred: [%s]", err.Error())
}
log.Printf("response: [%+v]", res)

// check mappings properties
res, err = client.Indices.Get([]string{"books-nonfiction"})
if err != nil {
log.Printf("error occurred: [%s]", err.Error())
}
log.Printf("response: [%+v]", res)
```

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

```go
res, err := client.Indices.PutIndexTemplate("books", strings.NewReader(`{
"index_patterns": ["books-*"],
"priority": 0,
"template": {
"settings": {
"index": {
"number_of_shards": 3,
"number_of_replicas": 0
}
}
}
}`))
if err != nil {
log.Printf("error occurred: [%s]", err.Error())
}
log.Printf("response: [%+v]", res)

// higher priority than the `books` template
res, err = client.Indices.PutIndexTemplate("books-fiction", strings.NewReader(`{
"index_patterns": ["books-fiction-*"],
"priority": 1,
"template": {
"settings": {
"index": {
"number_of_shards": 1,
"number_of_replicas": 1
}
}
}
}`))
if err != nil {
log.Printf("error occurred: [%s]", err.Error())
}
log.Printf("response: [%+v]", res)
```

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

```go
res, err = client.Indices.Create("books-fiction-romance")
if err != nil {
log.Printf("error occurred: [%s]", err.Error())
}
log.Printf("response: [%+v]", res)

res, err = client.Indices.Get([]string{"books-fiction-romance"})
if err != nil {
log.Printf("error occurred: [%s]", err.Error())
}
log.Printf("response: [%+v]", res)
```

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

```go
// delete index templates if they exist
res, err := client.Indices.DeleteIndexTemplate("books-*")
if err != nil {
log.Printf("error occurred: [%s]", err.Error())
}
log.Printf("response: [%+v]", res)

// delete indices if they exist
res, err = client.Indices.Delete([]string{"books-*", "books-fiction-*"})
if err != nil {
log.Printf("error occurred: [%s]", err.Error())
}
log.Printf("response: [%+v]", res)

// Composable Index Templates
res, err = client.Cluster.PutComponentTemplate("books_mappings", strings.NewReader(`{
"template": {
"mappings": {
"properties": {
"title": { "type": "text" },
"author": { "type": "text" },
"published_on": { "type": "date" },
"pages": { "type": "integer" }
}
}
}
}`))
if err != nil {
log.Printf("error occurred: [%s]", err.Error())
}
log.Printf("response: [%+v]", res)

// use the `books_mappings` component template with priority 0
res, err = client.Indices.PutIndexTemplate("books", strings.NewReader(`{
"index_patterns": ["books-*"],
"composed_of": ["books_mappings"],
"priority": 0,
"template": {
"settings": {
"index": {
"number_of_shards": 3,
"number_of_replicas": 0
}
}
}
}`))
if err != nil {
log.Printf("error occurred: [%s]", err.Error())
}
log.Printf("response: [%+v]", res)

// use the `books_mappings` component template with priority 1
res, err = client.Indices.PutIndexTemplate("books", strings.NewReader(`{
"index_patterns": ["books-fiction-*"],
"composed_of": ["books_mappings"],
"priority": 1,
"template": {
"settings": {
"index": {
"number_of_shards": 3,
"number_of_replicas": 0
}
}
}
}`))
if err != nil {
log.Printf("error occurred: [%s]", err.Error())
}
log.Printf("response: [%+v]", res)
```

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:

```go
res, err = client.Indices.Create("books-fiction-horror")
if err != nil {
log.Printf("error occurred: [%s]", err.Error())
}
log.Printf("response: [%+v]", res)

res, err = client.Indices.Get([]string{"books-fiction-horror"})
if err != nil {
log.Printf("error occurred: [%s]", err.Error())
}
log.Printf("response: [%+v]", res)
```

### Get an Index Template

You can get an index template with the `get_index_template` API action:

```go
res, err = client.Indices.GetIndexTemplate(
client.Indices.GetIndexTemplate.WithName("books"),
)
if err != nil {
log.Printf("error occurred: [%s]", err.Error())
}
log.Printf("response: [%+v]", res)
```

### Delete an Index Template

You can delete an index template with the `delete_template` API action:

```go
res, err = client.Indices.DeleteIndexTemplate("books")
if err != nil {
log.Printf("error occurred: [%s]", err.Error())
}
log.Printf("response: [%+v]", res)
```

## Cleanup

Let's delete all resources created in this guide:

```go
res, err = client.Indices.DeleteIndexTemplate("books-fiction")
if err != nil {
log.Printf("error occurred: [%s]", err.Error())
}
log.Printf("response: [%+v]", res)
```