Debian Codegraph @

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 "encoding/json"
10 "fmt"
11 "net/url"
12 "strings"
13
14 "gopkg.in/olivere/elastic.v5/uritemplates"
15 )
16
17 // GetService allows to get a typed JSON document from the index based
18 // on its id.
19 //
20 // See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/docs-get.html
21 // for details.
22 type GetService struct {
23 client *Client
24 pretty bool
25 index string
26 typ string
27 id string
28 routing string
29 preference string
30 storedFields []string
31 refresh string
32 realtime *bool
33 fsc *FetchSourceContext
34 version interface{}
35 versionType string
36 parent string
37 ignoreErrorsOnGeneratedFields *bool
38 }
39
40 // NewGetService creates a new GetService.
41 func NewGetService(client *Client) *GetService {
42 return &GetService{
43 client: client,
44 typ: "_all",
45 }
46 }
47
48 // Index is the name of the index.
49 func (s *GetService) Index(index string) *GetService {
50 s.index = index
51 return s
52 }
53
54 // Type is the type of the document (use `_all` to fetch the first document
55 // matching the ID across all types).
56 func (s *GetService) Type(typ string) *GetService {
57 s.typ = typ
58 return s
59 }
60
61 // Id is the document ID.
62 func (s *GetService) Id(id string) *GetService {
63 s.id = id
64 return s
65 }
66
67 // Parent is the ID of the parent document.
68 func (s *GetService) Parent(parent string) *GetService {
69 s.parent = parent
70 return s
71 }
72
73 // Routing is the specific routing value.
74 func (s *GetService) Routing(routing string) *GetService {
75 s.routing = routing
76 return s
77 }
78
79 // Preference specifies the node or shard the operation should be performed on (default: random).
80 func (s *GetService) Preference(preference string) *GetService {
81 s.preference = preference
82 return s
83 }
84
85 // StoredFields is a list of fields to return in the response.
86 func (s *GetService) StoredFields(storedFields ...string) *GetService {
87 s.storedFields = append(s.storedFields, storedFields...)
88 return s
89 }
90
91 func (s *GetService) FetchSource(fetchSource bool) *GetService {
92 if s.fsc == nil {
93 s.fsc = NewFetchSourceContext(fetchSource)
94 } else {
95 s.fsc.SetFetchSource(fetchSource)
96 }
97 return s
98 }
99
100 func (s *GetService) FetchSourceContext(fetchSourceContext *FetchSourceContext) *GetService {
101 s.fsc = fetchSourceContext
102 return s
103 }
104
105 // Refresh the shard containing the document before performing the operation.
106 func (s *GetService) Refresh(refresh string) *GetService {
107 s.refresh = refresh
108 return s
109 }
110
111 // Realtime specifies whether to perform the operation in realtime or search mode.
112 func (s *GetService) Realtime(realtime bool) *GetService {
113 s.realtime = &realtime
114 return s
115 }
116
117 // VersionType is the specific version type.
118 func (s *GetService) VersionType(versionType string) *GetService {
119 s.versionType = versionType
120 return s
121 }
122
123 // Version is an explicit version number for concurrency control.
124 func (s *GetService) Version(version interface{}) *GetService {
125 s.version = version
126 return s
127 }
128
129 // IgnoreErrorsOnGeneratedFields indicates whether to ignore fields that
130 // are generated if the transaction log is accessed.
131 func (s *GetService) IgnoreErrorsOnGeneratedFields(ignore bool) *GetService {
132 s.ignoreErrorsOnGeneratedFields = &ignore
133 return s
134 }
135
136 // Pretty indicates that the JSON response be indented and human readable.
137 func (s *GetService) Pretty(pretty bool) *GetService {
138 s.pretty = pretty
139 return s
140 }
141
142 // Validate checks if the operation is valid.
143 func (s *GetService) Validate() error {
144 var invalid []string
145 if s.id == "" {
146 invalid = append(invalid, "Id")
147 }
148 if s.index == "" {
149 invalid = append(invalid, "Index")
150 }
151 if s.typ == "" {
152 invalid = append(invalid, "Type")
153 }
154 if len(invalid) > 0 {
155 return fmt.Errorf("missing required fields: %v", invalid)
156 }
157 return nil
158 }
159
160 // buildURL builds the URL for the operation.
161 func (s *GetService) buildURL() (string, url.Values, error) {
162 // Build URL
163 path, err := uritemplates.Expand("/{index}/{type}/{id}", map[string]string{
164 "id": s.id,
165 "index": s.index,
166 "type": s.typ,
167 })
168 if err != nil {
169 return "", url.Values{}, err
170 }
171
172 // Add query string parameters
173 params := url.Values{}
174 if s.pretty {
175 params.Set("pretty", "1")
176 }
177 if s.routing != "" {
178 params.Set("routing", s.routing)
179 }
180 if s.parent != "" {
181 params.Set("parent", s.parent)
182 }
183 if s.preference != "" {
184 params.Set("preference", s.preference)
185 }
186 if len(s.storedFields) > 0 {
187 params.Set("stored_fields", strings.Join(s.storedFields, ","))
188 }
189 if s.refresh != "" {
190 params.Set("refresh", s.refresh)
191 }
192 if s.version != nil {
193 params.Set("version", fmt.Sprintf("%v", s.version))
194 }
195 if s.versionType != "" {
196 params.Set("version_type", s.versionType)
197 }
198 if s.realtime != nil {
199 params.Set("realtime", fmt.Sprintf("%v", *s.realtime))
200 }
201 if s.ignoreErrorsOnGeneratedFields != nil {
202 params.Add("ignore_errors_on_generated_fields", fmt.Sprintf("%v", *s.ignoreErrorsOnGeneratedFields))
203 }
204 if s.fsc != nil {
205 for k, values := range s.fsc.Query() {
206 params.Add(k, strings.Join(values, ","))
207 }
208 }
209 return path, params, nil
210 }
211
212 // Do executes the operation.
213 func (s *GetService) Do(ctx context.Context) (*GetResult, error) {
214 // Check pre-conditions
215 if err := s.Validate(); err != nil {
216 return nil, err
217 }
218
219 // Get URL for request
220 path, params, err := s.buildURL()
221 if err != nil {
222 return nil, err
223 }
224
225 // Get HTTP response
226 res, err := s.client.PerformRequest(ctx, "GET", path, params, nil)
227 if err != nil {
228 return nil, err
229 }
230
231 // Return operation response
232 ret := new(GetResult)
233 if err := s.client.decoder.Decode(res.Body, ret); err != nil {
234 return nil, err
235 }
236 return ret, nil
237 }
238
239 // -- Result of a get request.
240
241 // GetResult is the outcome of GetService.Do.
242 type GetResult struct {
243 Index string `json:"_index"` // index meta field
244 Type string `json:"_type"` // type meta field
245 Id string `json:"_id"` // id meta field
246 Uid string `json:"_uid"` // uid meta field (see MapperService.java for all meta fields)
247 Routing string `json:"_routing"` // routing meta field
248 Parent string `json:"_parent"` // parent meta field
249 Version *int64 `json:"_version"` // version number, when Version is set to true in SearchService
250 Source *json.RawMessage `json:"_source,omitempty"`
251 Found bool `json:"found,omitempty"`
252 Fields map[string]interface{} `json:"fields,omitempty"`
253 //Error string `json:"error,omitempty"` // used only in MultiGet
254 // TODO double-check that MultiGet now returns details error information
255 Error *ErrorDetails `json:"error,omitempty"` // only used in MultiGet
256 }