1// Copyright 2012-present Oliver Eilhard. All rights reserved.
2// Use of this source code is governed by a MIT-license.
3// See http://olivere.mit-license.org/license.txt for details.
4
5package elastic
6
7import (
8	"context"
9	"errors"
10	"net/url"
11
12	"github.com/olivere/elastic/uritemplates"
13)
14
15// IndicesCreateService creates a new index.
16//
17// See https://www.elastic.co/guide/en/elasticsearch/reference/6.7/indices-create-index.html
18// for details.
19type IndicesCreateService struct {
20	client        *Client
21	pretty        bool
22	index         string
23	timeout       string
24	masterTimeout string
25	bodyJson      interface{}
26	bodyString    string
27}
28
29// NewIndicesCreateService returns a new IndicesCreateService.
30func NewIndicesCreateService(client *Client) *IndicesCreateService {
31	return &IndicesCreateService{client: client}
32}
33
34// Index is the name of the index to create.
35func (b *IndicesCreateService) Index(index string) *IndicesCreateService {
36	b.index = index
37	return b
38}
39
40// Timeout the explicit operation timeout, e.g. "5s".
41func (s *IndicesCreateService) Timeout(timeout string) *IndicesCreateService {
42	s.timeout = timeout
43	return s
44}
45
46// MasterTimeout specifies the timeout for connection to master.
47func (s *IndicesCreateService) MasterTimeout(masterTimeout string) *IndicesCreateService {
48	s.masterTimeout = masterTimeout
49	return s
50}
51
52// Body specifies the configuration of the index as a string.
53// It is an alias for BodyString.
54func (b *IndicesCreateService) Body(body string) *IndicesCreateService {
55	b.bodyString = body
56	return b
57}
58
59// BodyString specifies the configuration of the index as a string.
60func (b *IndicesCreateService) BodyString(body string) *IndicesCreateService {
61	b.bodyString = body
62	return b
63}
64
65// BodyJson specifies the configuration of the index. The interface{} will
66// be serializes as a JSON document, so use a map[string]interface{}.
67func (b *IndicesCreateService) BodyJson(body interface{}) *IndicesCreateService {
68	b.bodyJson = body
69	return b
70}
71
72// Pretty indicates that the JSON response be indented and human readable.
73func (b *IndicesCreateService) Pretty(pretty bool) *IndicesCreateService {
74	b.pretty = pretty
75	return b
76}
77
78// Do executes the operation.
79func (b *IndicesCreateService) Do(ctx context.Context) (*IndicesCreateResult, error) {
80	if b.index == "" {
81		return nil, errors.New("missing index name")
82	}
83
84	// Build url
85	path, err := uritemplates.Expand("/{index}", map[string]string{
86		"index": b.index,
87	})
88	if err != nil {
89		return nil, err
90	}
91
92	params := make(url.Values)
93	if b.pretty {
94		params.Set("pretty", "true")
95	}
96	if b.masterTimeout != "" {
97		params.Set("master_timeout", b.masterTimeout)
98	}
99	if b.timeout != "" {
100		params.Set("timeout", b.timeout)
101	}
102
103	// Setup HTTP request body
104	var body interface{}
105	if b.bodyJson != nil {
106		body = b.bodyJson
107	} else {
108		body = b.bodyString
109	}
110
111	// Get response
112	res, err := b.client.PerformRequest(ctx, PerformRequestOptions{
113		Method: "PUT",
114		Path:   path,
115		Params: params,
116		Body:   body,
117	})
118	if err != nil {
119		return nil, err
120	}
121
122	ret := new(IndicesCreateResult)
123	if err := b.client.decoder.Decode(res.Body, ret); err != nil {
124		return nil, err
125	}
126	return ret, nil
127}
128
129// -- Result of a create index request.
130
131// IndicesCreateResult is the outcome of creating a new index.
132type IndicesCreateResult struct {
133	Acknowledged       bool   `json:"acknowledged"`
134	ShardsAcknowledged bool   `json:"shards_acknowledged"`
135	Index              string `json:"index,omitempty"`
136}
137