Debian Codegraph @

indices_get.go

raw
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
5 package elastic
6
7 import (
8 "context"
9 "fmt"
10 "net/url"
11 "strings"
12
13 "gopkg.in/olivere/elastic.v5/uritemplates"
14 )
15
16 // IndicesGetService retrieves information about one or more indices.
17 //
18 // See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/indices-get-index.html
19 // for more details.
20 type IndicesGetService struct {
21 client *Client
22 pretty bool
23 index []string
24 feature []string
25 local *bool
26 ignoreUnavailable *bool
27 allowNoIndices *bool
28 expandWildcards string
29 flatSettings *bool
30 human *bool
31 }
32
33 // NewIndicesGetService creates a new IndicesGetService.
34 func NewIndicesGetService(client *Client) *IndicesGetService {
35 return &IndicesGetService{
36 client: client,
37 index: make([]string, 0),
38 feature: make([]string, 0),
39 }
40 }
41
42 // Index is a list of index names.
43 func (s *IndicesGetService) Index(indices ...string) *IndicesGetService {
44 s.index = append(s.index, indices...)
45 return s
46 }
47
48 // Feature is a list of features.
49 func (s *IndicesGetService) Feature(features ...string) *IndicesGetService {
50 s.feature = append(s.feature, features...)
51 return s
52 }
53
54 // Local indicates whether to return local information, i.e. do not retrieve
55 // the state from master node (default: false).
56 func (s *IndicesGetService) Local(local bool) *IndicesGetService {
57 s.local = &local
58 return s
59 }
60
61 // IgnoreUnavailable indicates whether to ignore unavailable indexes (default: false).
62 func (s *IndicesGetService) IgnoreUnavailable(ignoreUnavailable bool) *IndicesGetService {
63 s.ignoreUnavailable = &ignoreUnavailable
64 return s
65 }
66
67 // AllowNoIndices indicates whether to ignore if a wildcard expression
68 // resolves to no concrete indices (default: false).
69 func (s *IndicesGetService) AllowNoIndices(allowNoIndices bool) *IndicesGetService {
70 s.allowNoIndices = &allowNoIndices
71 return s
72 }
73
74 // ExpandWildcards indicates whether wildcard expressions should get
75 // expanded to open or closed indices (default: open).
76 func (s *IndicesGetService) ExpandWildcards(expandWildcards string) *IndicesGetService {
77 s.expandWildcards = expandWildcards
78 return s
79 }
80
81 /* Disabled because serialization would fail in that case. */
82 /*
83 // FlatSettings make the service return settings in flat format (default: false).
84 func (s *IndicesGetService) FlatSettings(flatSettings bool) *IndicesGetService {
85 s.flatSettings = &flatSettings
86 return s
87 }
88 */
89
90 // Human indicates whether to return version and creation date values
91 // in human-readable format (default: false).
92 func (s *IndicesGetService) Human(human bool) *IndicesGetService {
93 s.human = &human
94 return s
95 }
96
97 // Pretty indicates that the JSON response be indented and human readable.
98 func (s *IndicesGetService) Pretty(pretty bool) *IndicesGetService {
99 s.pretty = pretty
100 return s
101 }
102
103 // buildURL builds the URL for the operation.
104 func (s *IndicesGetService) buildURL() (string, url.Values, error) {
105 var err error
106 var path string
107 var index []string
108
109 if len(s.index) > 0 {
110 index = s.index
111 } else {
112 index = []string{"_all"}
113 }
114
115 if len(s.feature) > 0 {
116 // Build URL
117 path, err = uritemplates.Expand("/{index}/{feature}", map[string]string{
118 "index": strings.Join(index, ","),
119 "feature": strings.Join(s.feature, ","),
120 })
121 } else {
122 // Build URL
123 path, err = uritemplates.Expand("/{index}", map[string]string{
124 "index": strings.Join(index, ","),
125 })
126 }
127 if err != nil {
128 return "", url.Values{}, err
129 }
130
131 // Add query string parameters
132 params := url.Values{}
133 if s.pretty {
134 params.Set("pretty", "1")
135 }
136 if s.expandWildcards != "" {
137 params.Set("expand_wildcards", s.expandWildcards)
138 }
139 if s.flatSettings != nil {
140 params.Set("flat_settings", fmt.Sprintf("%v", *s.flatSettings))
141 }
142 if s.human != nil {
143 params.Set("human", fmt.Sprintf("%v", *s.human))
144 }
145 if s.local != nil {
146 params.Set("local", fmt.Sprintf("%v", *s.local))
147 }
148 if s.ignoreUnavailable != nil {
149 params.Set("ignore_unavailable", fmt.Sprintf("%v", *s.ignoreUnavailable))
150 }
151 if s.allowNoIndices != nil {
152 params.Set("allow_no_indices", fmt.Sprintf("%v", *s.allowNoIndices))
153 }
154 return path, params, nil
155 }
156
157 // Validate checks if the operation is valid.
158 func (s *IndicesGetService) Validate() error {
159 var invalid []string
160 if len(s.index) == 0 {
161 invalid = append(invalid, "Index")
162 }
163 if len(invalid) > 0 {
164 return fmt.Errorf("missing required fields: %v", invalid)
165 }
166 return nil
167 }
168
169 // Do executes the operation.
170 func (s *IndicesGetService) Do(ctx context.Context) (map[string]*IndicesGetResponse, error) {
171 // Check pre-conditions
172 if err := s.Validate(); err != nil {
173 return nil, err
174 }
175
176 // Get URL for request
177 path, params, err := s.buildURL()
178 if err != nil {
179 return nil, err
180 }
181
182 // Get HTTP response
183 res, err := s.client.PerformRequest(ctx, "GET", path, params, nil)
184 if err != nil {
185 return nil, err
186 }
187
188 // Return operation response
189 var ret map[string]*IndicesGetResponse
190 if err := s.client.decoder.Decode(res.Body, &ret); err != nil {
191 return nil, err
192 }
193 return ret, nil
194 }
195
196 // IndicesGetResponse is part of the response of IndicesGetService.Do.
197 type IndicesGetResponse struct {
198 Aliases map[string]interface{} `json:"aliases"`
199 Mappings map[string]interface{} `json:"mappings"`
200 Settings map[string]interface{} `json:"settings"`
201 Warmers map[string]interface{} `json:"warmers"`
202 }