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