Skip to content
Oliver Eilhard edited this page Dec 11, 2019 · 10 revisions

The CreateIndex will create a new index (with Elasticsearch 7.x or later).

// Create a new index.
mapping := `{
	"settings":{
		"number_of_shards":1,
		"number_of_replicas":0
	},
	"mappings":{
		"properties":{
			"tags":{
				"type":"keyword"
			},
			"location":{
				"type":"geo_point"
			},
			"suggest_field":{
				"type":"completion"
			}
		}
	}
}`

ctx := context.Background()
createIndex, err := client.CreateIndex("twitter").BodyString(mapping).Do(ctx)
if err != nil {
    // Handle error
    panic(err)
}
if !createIndex.Acknowledged {
    // Not acknowledged
}

Notice that you can also use BodyJson(...) and provide an interface{} value to it (most probably a map[string]interface{}). It will be serialized to JSON and then used as the body of the request.

See the Create Index API documentation for more details.

Clone this wiki locally