@@ -13,16 +13,21 @@ import (
13
13
"github.com/olivere/elastic/uritemplates"
14
14
)
15
15
16
- // ClusterStateService returns the state of the cluster.
17
- // It is documented at http://www.elasticsearch.org/guide/en/elasticsearch/reference/1.4/cluster-state.html.
16
+ // ClusterStateService allows to get a comprehensive state information of the whole cluster.
17
+ //
18
+ // See https://www.elastic.co/guide/en/elasticsearch/reference/current/cluster-state.html
19
+ // for details.
18
20
type ClusterStateService struct {
19
- client * Client
20
- pretty bool
21
- indices []string
22
- metrics []string
23
- local * bool
24
- masterTimeout string
25
- flatSettings * bool
21
+ client * Client
22
+ pretty bool
23
+ indices []string
24
+ metrics []string
25
+ allowNoIndices * bool
26
+ expandWildcards string
27
+ flatSettings * bool
28
+ ignoreUnavailable * bool
29
+ local * bool
30
+ masterTimeout string
26
31
}
27
32
28
33
// NewClusterStateService creates a new ClusterStateService.
@@ -44,34 +49,55 @@ func (s *ClusterStateService) Index(indices ...string) *ClusterStateService {
44
49
// Metric limits the information returned to the specified metric.
45
50
// It can be one of: version, master_node, nodes, routing_table, metadata,
46
51
// blocks, or customs.
47
- func (s * ClusterStateService ) Metric (metric string ) * ClusterStateService {
48
- return s .Metrics (metric )
52
+ func (s * ClusterStateService ) Metric (metrics ... string ) * ClusterStateService {
53
+ s .metrics = append (s .metrics , metrics ... )
54
+ return s
49
55
}
50
56
51
- // Metrics limits the information returned to the specified metrics.
52
- // It can be any of: version, master_node, nodes, routing_table, metadata,
53
- // blocks, or customs.
54
- func (s * ClusterStateService ) Metrics (metrics ... string ) * ClusterStateService {
55
- s .metrics = append (s .metrics , metrics ... )
57
+ // AllowNoIndices indicates whether to ignore if a wildcard indices
58
+ // expression resolves into no concrete indices.
59
+ // (This includes `_all` string or when no indices have been specified).
60
+ func (s * ClusterStateService ) AllowNoIndices (allowNoIndices bool ) * ClusterStateService {
61
+ s .allowNoIndices = & allowNoIndices
62
+ return s
63
+ }
64
+
65
+ // ExpandWildcards indicates whether to expand wildcard expression to
66
+ // concrete indices that are open, closed or both..
67
+ func (s * ClusterStateService ) ExpandWildcards (expandWildcards string ) * ClusterStateService {
68
+ s .expandWildcards = expandWildcards
69
+ return s
70
+ }
71
+
72
+ // FlatSettings, when set, returns settings in flat format (default: false).
73
+ func (s * ClusterStateService ) FlatSettings (flatSettings bool ) * ClusterStateService {
74
+ s .flatSettings = & flatSettings
75
+ return s
76
+ }
77
+
78
+ // IgnoreUnavailable indicates whether specified concrete indices should be
79
+ // ignored when unavailable (missing or closed).
80
+ func (s * ClusterStateService ) IgnoreUnavailable (ignoreUnavailable bool ) * ClusterStateService {
81
+ s .ignoreUnavailable = & ignoreUnavailable
56
82
return s
57
83
}
58
84
59
- // Local indicates whether to return local information. If it is true,
60
- // we do not retrieve the state from master node (default: false).
85
+ // Local indicates whether to return local information. When set, it does not
86
+ // retrieve the state from master node (default: false).
61
87
func (s * ClusterStateService ) Local (local bool ) * ClusterStateService {
62
88
s .local = & local
63
89
return s
64
90
}
65
91
66
- // MasterTimeout specifies the timeout for connection to master.
92
+ // MasterTimeout specifies timeout for connection to master.
67
93
func (s * ClusterStateService ) MasterTimeout (masterTimeout string ) * ClusterStateService {
68
94
s .masterTimeout = masterTimeout
69
95
return s
70
96
}
71
97
72
- // FlatSettings indicates whether to return settings in flat format (default: false) .
73
- func (s * ClusterStateService ) FlatSettings ( flatSettings bool ) * ClusterStateService {
74
- s .flatSettings = & flatSettings
98
+ // Pretty indicates that the JSON response be indented and human readable .
99
+ func (s * ClusterStateService ) Pretty ( pretty bool ) * ClusterStateService {
100
+ s .pretty = pretty
75
101
return s
76
102
}
77
103
@@ -96,16 +122,27 @@ func (s *ClusterStateService) buildURL() (string, url.Values, error) {
96
122
97
123
// Add query string parameters
98
124
params := url.Values {}
99
- if s .masterTimeout != "" {
100
- params .Set ("master_timeout" , s .masterTimeout )
125
+ if s .pretty {
126
+ params .Set ("pretty" , "1" )
127
+ }
128
+ if s .allowNoIndices != nil {
129
+ params .Set ("allow_no_indices" , fmt .Sprintf ("%v" , * s .allowNoIndices ))
130
+ }
131
+ if s .expandWildcards != "" {
132
+ params .Set ("expand_wildcards" , s .expandWildcards )
101
133
}
102
134
if s .flatSettings != nil {
103
135
params .Set ("flat_settings" , fmt .Sprintf ("%v" , * s .flatSettings ))
104
136
}
137
+ if s .ignoreUnavailable != nil {
138
+ params .Set ("ignore_unavailable" , fmt .Sprintf ("%v" , * s .ignoreUnavailable ))
139
+ }
105
140
if s .local != nil {
106
141
params .Set ("local" , fmt .Sprintf ("%v" , * s .local ))
107
142
}
108
-
143
+ if s .masterTimeout != "" {
144
+ params .Set ("master_timeout" , s .masterTimeout )
145
+ }
109
146
return path , params , nil
110
147
}
111
148
@@ -144,42 +181,104 @@ func (s *ClusterStateService) Do() (*ClusterStateResponse, error) {
144
181
// ClusterStateResponse is the response of ClusterStateService.Do.
145
182
type ClusterStateResponse struct {
146
183
ClusterName string `json:"cluster_name"`
147
- Version int `json:"version"`
184
+ Version int64 `json:"version"`
185
+ StateUUID string `json:"state_uuid"`
148
186
MasterNode string `json:"master_node"`
149
- Blocks map [string ]interface {} `json:"blocks"`
150
- Nodes map [string ]* ClusterStateNode `json:"nodes"`
151
- Metadata * ClusterStateMetadata `json:"metadata"`
152
- RoutingTable map [string ]* ClusterStateRoutingTable `json:"routing_table"`
153
- RoutingNodes * ClusterStateRoutingNode `json:"routing_nodes"`
154
- Allocations []interface {} `json:"allocations"`
187
+ Blocks map [string ]* clusterBlocks `json:"blocks"`
188
+ Nodes map [string ]* discoveryNode `json:"nodes"`
189
+ Metadata * clusterStateMetadata `json:"metadata"`
190
+ RoutingTable map [string ]* clusterStateRoutingTable `json:"routing_table"`
191
+ RoutingNodes * clusterStateRoutingNode `json:"routing_nodes"`
155
192
Customs map [string ]interface {} `json:"customs"`
156
193
}
157
194
158
- type ClusterStateMetadata struct {
159
- Templates map [string ]interface {} `json:"templates"`
160
- Indices map [string ]interface {} `json:"indices"`
161
- Repositories map [string ]interface {} `json:"repositories"`
195
+ type clusterBlocks struct {
196
+ Global map [string ]* clusterBlock `json:"global"` // id -> cluster block
197
+ Indices map [string ]* clusterBlock `json:"indices"` // index name -> cluster block
162
198
}
163
199
164
- type ClusterStateNode struct {
165
- Name string `json:"name"`
166
- TransportAddress string `json:"transport_address"`
167
- Attributes map [string ]interface {} `json:"attributes"`
200
+ type clusterBlock struct {
201
+ Description string `json:"description"`
202
+ Retryable bool `json:"retryable"`
203
+ DisableStatePersistence bool `json:"disable_state_persistence"`
204
+ Levels []string `json:"levels"`
205
+ }
168
206
169
- // TODO(oe) are these still valid?
170
- State string `json:"state"`
171
- Primary bool `json:"primary"`
172
- Node string `json:"node"`
173
- RelocatingNode * string `json:"relocating_node"`
174
- Shard int `json:"shard"`
175
- Index string `json:"index"`
207
+ type clusterStateMetadata struct {
208
+ ClusterUUID string `json:"cluster_uuid"`
209
+ Templates map [string ]* indexTemplateMetaData `json:"templates"` // template name -> index template metadata
210
+ Indices map [string ]* indexMetaData `json:"indices"` // index name _> meta data
211
+ RoutingTable struct {
212
+ Indices map [string ]* indexRoutingTable `json:"indices"` // index name -> routing table
213
+ } `json:"routing_table"`
214
+ RoutingNodes struct {
215
+ Unassigned []* shardRouting `json:"unassigned"`
216
+ Nodes []* shardRouting `json:"nodes"`
217
+ } `json:"routing_nodes"`
218
+ Customs map [string ]interface {} `json:"customs"`
176
219
}
177
220
178
- type ClusterStateRoutingTable struct {
221
+ type discoveryNode struct {
222
+ Name string `json:"name"` // server name, e.g. "es1"
223
+ TransportAddress string `json:"transport_address"` // e.g. inet[/1.2.3.4:9300]
224
+ Attributes map [string ]interface {} `json:"attributes"` // e.g. { "data": true, "master": true }
225
+ }
226
+
227
+ type clusterStateRoutingTable struct {
179
228
Indices map [string ]interface {} `json:"indices"`
180
229
}
181
230
182
- type ClusterStateRoutingNode struct {
183
- Unassigned []interface {} `json:"unassigned"`
184
- Nodes map [string ]interface {} `json:"nodes"`
231
+ type clusterStateRoutingNode struct {
232
+ Unassigned []* shardRouting `json:"unassigned"`
233
+ // Node Id -> shardRouting
234
+ Nodes map [string ][]* shardRouting `json:"nodes"`
235
+ }
236
+
237
+ type indexTemplateMetaData struct {
238
+ Template string `json:"template"` // e.g. "store-*"
239
+ Order int `json:"order"`
240
+ Settings map [string ]interface {} `json:"settings"` // index settings
241
+ Mappings map [string ]interface {} `json:"mappings"` // type name -> mapping
242
+ }
243
+
244
+ type indexMetaData struct {
245
+ State string `json:"state"`
246
+ Settings map [string ]interface {} `json:"settings"`
247
+ Mappings map [string ]interface {} `json:"mappings"`
248
+ Aliases []string `json:"aliases"` // e.g. [ "alias1", "alias2" ]
249
+ }
250
+
251
+ type indexRoutingTable struct {
252
+ Shards map [string ]* shardRouting `json:"shards"`
253
+ }
254
+
255
+ type shardRouting struct {
256
+ State string `json:"state"`
257
+ Primary bool `json:"primary"`
258
+ Node string `json:"node"`
259
+ RelocatingNode string `json:"relocating_node"`
260
+ Shard int `json:"shard"`
261
+ Index string `json:"index"`
262
+ Version int64 `json:"state"`
263
+ RestoreSource * RestoreSource `json:"restore_source"`
264
+ AllocationId * allocationId `json:"allocation_id"`
265
+ UnassignedInfo * unassignedInfo `json:"unassigned_info"`
266
+ }
267
+
268
+ type RestoreSource struct {
269
+ Repository string `json:"repository"`
270
+ Snapshot string `json:"snapshot"`
271
+ Version string `json:"version"`
272
+ Index string `json:"index"`
273
+ }
274
+
275
+ type allocationId struct {
276
+ Id string `json:"id"`
277
+ RelocationId string `json:"relocation_id"`
278
+ }
279
+
280
+ type unassignedInfo struct {
281
+ Reason string `json:"reason"`
282
+ At string `json:"at"`
283
+ Details string `json:"details"`
185
284
}
0 commit comments