Debian Codegraph @

indices_get_field_mapping.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 // IndicesGetFieldMappingService retrieves the mapping definitions for the fields in an index
17 // or index/type.
18 //
19 // See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/indices-get-field-mapping.html
20 // for details.
21 type IndicesGetFieldMappingService struct {
22 client *Client
23 pretty bool
24 index []string
25 typ []string
26 field []string
27 local *bool
28 ignoreUnavailable *bool
29 allowNoIndices *bool
30 expandWildcards string
31 }
32
33 // NewGetFieldMappingService is an alias for NewIndicesGetFieldMappingService.
34 // Use NewIndicesGetFieldMappingService.
35 func NewGetFieldMappingService(client *Client) *IndicesGetFieldMappingService {
36 return NewIndicesGetFieldMappingService(client)
37 }
38
39 // NewIndicesGetFieldMappingService creates a new IndicesGetFieldMappingService.
40 func NewIndicesGetFieldMappingService(client *Client) *IndicesGetFieldMappingService {
41 return &IndicesGetFieldMappingService{
42 client: client,
43 }
44 }
45
46 // Index is a list of index names.
47 func (s *IndicesGetFieldMappingService) Index(indices ...string) *IndicesGetFieldMappingService {
48 s.index = append(s.index, indices...)
49 return s
50 }
51
52 // Type is a list of document types.
53 func (s *IndicesGetFieldMappingService) Type(types ...string) *IndicesGetFieldMappingService {
54 s.typ = append(s.typ, types...)
55 return s
56 }
57
58 // Field is a list of fields.
59 func (s *IndicesGetFieldMappingService) Field(fields ...string) *IndicesGetFieldMappingService {
60 s.field = append(s.field, fields...)
61 return s
62 }
63
64 // AllowNoIndices indicates whether to ignore if a wildcard indices
65 // expression resolves into no concrete indices.
66 // This includes `_all` string or when no indices have been specified.
67 func (s *IndicesGetFieldMappingService) AllowNoIndices(allowNoIndices bool) *IndicesGetFieldMappingService {
68 s.allowNoIndices = &allowNoIndices
69 return s
70 }
71
72 // ExpandWildcards indicates whether to expand wildcard expression to
73 // concrete indices that are open, closed or both..
74 func (s *IndicesGetFieldMappingService) ExpandWildcards(expandWildcards string) *IndicesGetFieldMappingService {
75 s.expandWildcards = expandWildcards
76 return s
77 }
78
79 // Local indicates whether to return local information, do not retrieve
80 // the state from master node (default: false).
81 func (s *IndicesGetFieldMappingService) Local(local bool) *IndicesGetFieldMappingService {
82 s.local = &local
83 return s
84 }
85
86 // IgnoreUnavailable indicates whether specified concrete indices should be
87 // ignored when unavailable (missing or closed).
88 func (s *IndicesGetFieldMappingService) IgnoreUnavailable(ignoreUnavailable bool) *IndicesGetFieldMappingService {
89 s.ignoreUnavailable = &ignoreUnavailable
90 return s
91 }
92
93 // Pretty indicates that the JSON response be indented and human readable.
94 func (s *IndicesGetFieldMappingService) Pretty(pretty bool) *IndicesGetFieldMappingService {
95 s.pretty = pretty
96 return s
97 }
98
99 // buildURL builds the URL for the operation.
100 func (s *IndicesGetFieldMappingService) buildURL() (string, url.Values, error) {
101 var index, typ, field []string
102
103 if len(s.index) > 0 {
104 index = s.index
105 } else {
106 index = []string{"_all"}
107 }
108
109 if len(s.typ) > 0 {
110 typ = s.typ
111 } else {
112 typ = []string{"_all"}
113 }
114
115 if len(s.field) > 0 {
116 field = s.field
117 } else {
118 field = []string{"*"}
119 }
120
121 // Build URL
122 path, err := uritemplates.Expand("/{index}/_mapping/{type}/field/{field}", map[string]string{
123 "index": strings.Join(index, ","),
124 "type": strings.Join(typ, ","),
125 "field": strings.Join(field, ","),
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.ignoreUnavailable != nil {
137 params.Set("ignore_unavailable", fmt.Sprintf("%v", *s.ignoreUnavailable))
138 }
139 if s.allowNoIndices != nil {
140 params.Set("allow_no_indices", fmt.Sprintf("%v", *s.allowNoIndices))
141 }
142 if s.expandWildcards != "" {
143 params.Set("expand_wildcards", s.expandWildcards)
144 }
145 if s.local != nil {
146 params.Set("local", fmt.Sprintf("%v", *s.local))
147 }
148 return path, params, nil
149 }
150
151 // Validate checks if the operation is valid.
152 func (s *IndicesGetFieldMappingService) Validate() error {
153 return nil
154 }
155
156 // Do executes the operation. It returns mapping definitions for an index
157 // or index/type.
158 func (s *IndicesGetFieldMappingService) Do(ctx context.Context) (map[string]interface{}, error) {
159 var ret map[string]interface{}
160
161 // Check pre-conditions
162 if err := s.Validate(); err != nil {
163 return nil, err
164 }
165
166 // Get URL for request
167 path, params, err := s.buildURL()
168 if err != nil {
169 return nil, err
170 }
171
172 // Get HTTP response
173 res, err := s.client.PerformRequest(ctx, "GET", path, params, nil)
174 if err != nil {
175 return nil, err
176 }
177
178 // Return operation response
179 if err := s.client.decoder.Decode(res.Body, &ret); err != nil {
180 return nil, err
181 }
182 return ret, nil
183 }