github.com/opensearch-project/opensearch-go/v2@v2.3.0/guides/index_template.md (about) 1 # Index Template 2 3 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. 4 5 ## Setup 6 7 Assuming you have OpenSearch running locally on port 9200, you can create a client instance with the following code: 8 9 ```go 10 package main 11 12 import ( 13 "github.com/opensearch-project/opensearch-go/v2" 14 "log" 15 ) 16 17 func main() { 18 client, err := opensearch.NewDefaultClient() 19 if err != nil { 20 log.Printf("error occurred: [%s]", err.Error()) 21 } 22 log.Printf("response: [%+v]", client) 23 } 24 ``` 25 26 ## Index Template API Actions 27 28 ### Create an Index Template 29 30 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: 31 32 ```go 33 body := strings.NewReader(`{ 34 "index_patterns": ["books-*"], 35 "template": { 36 "settings": { 37 "index": { 38 "number_of_shards": 3, 39 "number_of_replicas": 0 40 } 41 }, 42 "mappings": { 43 "properties": { 44 "title": { "type": "text" }, 45 "author": { "type": "text" }, 46 "published_on": { "type": "date" }, 47 "pages": { "type": "integer" } 48 } 49 } 50 } 51 }`) 52 53 res, err := client.Indices.PutIndexTemplate("books", body) 54 if err != nil { 55 log.Printf("error occurred: [%s]", err.Error()) 56 } 57 log.Printf("response: [%+v]", res) 58 ``` 59 60 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: 61 62 ```go 63 res, err = client.Indices.Create("books-nonfiction") 64 if err != nil { 65 log.Printf("error occurred: [%s]", err.Error()) 66 } 67 log.Printf("response: [%+v]", res) 68 69 // check mappings properties 70 res, err = client.Indices.Get([]string{"books-nonfiction"}) 71 if err != nil { 72 log.Printf("error occurred: [%s]", err.Error()) 73 } 74 log.Printf("response: [%+v]", res) 75 ``` 76 77 ### Multiple Index Templates 78 79 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: 80 81 ```go 82 res, err := client.Indices.PutIndexTemplate("books", strings.NewReader(`{ 83 "index_patterns": ["books-*"], 84 "priority": 0, 85 "template": { 86 "settings": { 87 "index": { 88 "number_of_shards": 3, 89 "number_of_replicas": 0 90 } 91 } 92 } 93 }`)) 94 if err != nil { 95 log.Printf("error occurred: [%s]", err.Error()) 96 } 97 log.Printf("response: [%+v]", res) 98 99 // higher priority than the `books` template 100 res, err = client.Indices.PutIndexTemplate("books-fiction", strings.NewReader(`{ 101 "index_patterns": ["books-fiction-*"], 102 "priority": 1, 103 "template": { 104 "settings": { 105 "index": { 106 "number_of_shards": 1, 107 "number_of_replicas": 1 108 } 109 } 110 } 111 }`)) 112 if err != nil { 113 log.Printf("error occurred: [%s]", err.Error()) 114 } 115 log.Printf("response: [%+v]", res) 116 ``` 117 118 When we create an index named `books-fiction-romance`, OpenSearch will apply the `books-fiction-*` template's settings to the index: 119 120 ```go 121 res, err = client.Indices.Create("books-fiction-romance") 122 if err != nil { 123 log.Printf("error occurred: [%s]", err.Error()) 124 } 125 log.Printf("response: [%+v]", res) 126 127 res, err = client.Indices.Get([]string{"books-fiction-romance"}) 128 if err != nil { 129 log.Printf("error occurred: [%s]", err.Error()) 130 } 131 log.Printf("response: [%+v]", res) 132 ``` 133 134 ### Composable Index Templates 135 136 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: 137 138 ```go 139 // delete index templates if they exist 140 res, err := client.Indices.DeleteIndexTemplate("books-*") 141 if err != nil { 142 log.Printf("error occurred: [%s]", err.Error()) 143 } 144 log.Printf("response: [%+v]", res) 145 146 // delete indices if they exist 147 res, err = client.Indices.Delete([]string{"books-*", "books-fiction-*"}) 148 if err != nil { 149 log.Printf("error occurred: [%s]", err.Error()) 150 } 151 log.Printf("response: [%+v]", res) 152 153 // Composable Index Templates 154 res, err = client.Cluster.PutComponentTemplate("books_mappings", strings.NewReader(`{ 155 "template": { 156 "mappings": { 157 "properties": { 158 "title": { "type": "text" }, 159 "author": { "type": "text" }, 160 "published_on": { "type": "date" }, 161 "pages": { "type": "integer" } 162 } 163 } 164 } 165 }`)) 166 if err != nil { 167 log.Printf("error occurred: [%s]", err.Error()) 168 } 169 log.Printf("response: [%+v]", res) 170 171 // use the `books_mappings` component template with priority 0 172 res, err = client.Indices.PutIndexTemplate("books", strings.NewReader(`{ 173 "index_patterns": ["books-*"], 174 "composed_of": ["books_mappings"], 175 "priority": 0, 176 "template": { 177 "settings": { 178 "index": { 179 "number_of_shards": 3, 180 "number_of_replicas": 0 181 } 182 } 183 } 184 }`)) 185 if err != nil { 186 log.Printf("error occurred: [%s]", err.Error()) 187 } 188 log.Printf("response: [%+v]", res) 189 190 // use the `books_mappings` component template with priority 1 191 res, err = client.Indices.PutIndexTemplate("books", strings.NewReader(`{ 192 "index_patterns": ["books-fiction-*"], 193 "composed_of": ["books_mappings"], 194 "priority": 1, 195 "template": { 196 "settings": { 197 "index": { 198 "number_of_shards": 3, 199 "number_of_replicas": 0 200 } 201 } 202 } 203 }`)) 204 if err != nil { 205 log.Printf("error occurred: [%s]", err.Error()) 206 } 207 log.Printf("response: [%+v]", res) 208 ``` 209 210 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: 211 212 ```go 213 res, err = client.Indices.Create("books-fiction-horror") 214 if err != nil { 215 log.Printf("error occurred: [%s]", err.Error()) 216 } 217 log.Printf("response: [%+v]", res) 218 219 res, err = client.Indices.Get([]string{"books-fiction-horror"}) 220 if err != nil { 221 log.Printf("error occurred: [%s]", err.Error()) 222 } 223 log.Printf("response: [%+v]", res) 224 ``` 225 226 ### Get an Index Template 227 228 You can get an index template with the `get_index_template` API action: 229 230 ```go 231 res, err = client.Indices.GetIndexTemplate( 232 client.Indices.GetIndexTemplate.WithName("books"), 233 ) 234 if err != nil { 235 log.Printf("error occurred: [%s]", err.Error()) 236 } 237 log.Printf("response: [%+v]", res) 238 ``` 239 240 ### Delete an Index Template 241 242 You can delete an index template with the `delete_template` API action: 243 244 ```go 245 res, err = client.Indices.DeleteIndexTemplate("books") 246 if err != nil { 247 log.Printf("error occurred: [%s]", err.Error()) 248 } 249 log.Printf("response: [%+v]", res) 250 ``` 251 252 ## Cleanup 253 254 Let's delete all resources created in this guide: 255 256 ```go 257 res, err = client.Indices.DeleteIndexTemplate("books-fiction") 258 if err != nil { 259 log.Printf("error occurred: [%s]", err.Error()) 260 } 261 log.Printf("response: [%+v]", res) 262 ```