Debian Codegraph @

mget.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
14 // MgetService allows to get multiple documents based on an index,
15 // type (optional) and id (possibly routing). The response includes
16 // a docs array with all the fetched documents, each element similar
17 // in structure to a document provided by the Get API.
18 //
19 // See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/docs-multi-get.html
20 // for details.
21 type MgetService struct {
22 client *Client
23 pretty bool
24 preference string
25 realtime *bool
26 refresh string
27 routing string
28 storedFields []string
29 items []*MultiGetItem
30 }
31
32 // NewMgetService initializes a new Multi GET API request call.
33 func NewMgetService(client *Client) *MgetService {
34 builder := &MgetService{
35 client: client,
36 }
37 return builder
38 }
39
40 // Preference specifies the node or shard the operation should be performed
41 // on (default: random).
42 func (s *MgetService) Preference(preference string) *MgetService {
43 s.preference = preference
44 return s
45 }
46
47 // Refresh the shard containing the document before performing the operation.
48 func (s *MgetService) Refresh(refresh string) *MgetService {
49 s.refresh = refresh
50 return s
51 }
52
53 // Realtime specifies whether to perform the operation in realtime or search mode.
54 func (s *MgetService) Realtime(realtime bool) *MgetService {
55 s.realtime = &realtime
56 return s
57 }
58
59 // Routing is the specific routing value.
60 func (s *MgetService) Routing(routing string) *MgetService {
61 s.routing = routing
62 return s
63 }
64
65 // StoredFields is a list of fields to return in the response.
66 func (s *MgetService) StoredFields(storedFields ...string) *MgetService {
67 s.storedFields = append(s.storedFields, storedFields...)
68 return s
69 }
70
71 // Pretty indicates that the JSON response be indented and human readable.
72 func (s *MgetService) Pretty(pretty bool) *MgetService {
73 s.pretty = pretty
74 return s
75 }
76
77 // Add an item to the request.
78 func (s *MgetService) Add(items ...*MultiGetItem) *MgetService {
79 s.items = append(s.items, items...)
80 return s
81 }
82
83 // Source returns the request body, which will be serialized into JSON.
84 func (s *MgetService) Source() (interface{}, error) {
85 source := make(map[string]interface{})
86 items := make([]interface{}, len(s.items))
87 for i, item := range s.items {
88 src, err := item.Source()
89 if err != nil {
90 return nil, err
91 }
92 items[i] = src
93 }
94 source["docs"] = items
95 return source, nil
96 }
97
98 // Do executes the request.
99 func (s *MgetService) Do(ctx context.Context) (*MgetResponse, error) {
100 // Build url
101 path := "/_mget"
102
103 params := make(url.Values)
104 if s.realtime != nil {
105 params.Add("realtime", fmt.Sprintf("%v", *s.realtime))
106 }
107 if s.preference != "" {
108 params.Add("preference", s.preference)
109 }
110 if s.refresh != "" {
111 params.Add("refresh", s.refresh)
112 }
113 if s.routing != "" {
114 params.Set("routing", s.routing)
115 }
116 if len(s.storedFields) > 0 {
117 params.Set("stored_fields", strings.Join(s.storedFields, ","))
118 }
119
120 // Set body
121 body, err := s.Source()
122 if err != nil {
123 return nil, err
124 }
125
126 // Get response
127 res, err := s.client.PerformRequest(ctx, "GET", path, params, body)
128 if err != nil {
129 return nil, err
130 }
131
132 // Return result
133 ret := new(MgetResponse)
134 if err := s.client.decoder.Decode(res.Body, ret); err != nil {
135 return nil, err
136 }
137 return ret, nil
138 }
139
140 // -- Multi Get Item --
141
142 // MultiGetItem is a single document to retrieve via the MgetService.
143 type MultiGetItem struct {
144 index string
145 typ string
146 id string
147 routing string
148 storedFields []string
149 version *int64 // see org.elasticsearch.common.lucene.uid.Versions
150 versionType string // see org.elasticsearch.index.VersionType
151 fsc *FetchSourceContext
152 }
153
154 // NewMultiGetItem initializes a new, single item for a Multi GET request.
155 func NewMultiGetItem() *MultiGetItem {
156 return &MultiGetItem{}
157 }
158
159 // Index specifies the index name.
160 func (item *MultiGetItem) Index(index string) *MultiGetItem {
161 item.index = index
162 return item
163 }
164
165 // Type specifies the type name.
166 func (item *MultiGetItem) Type(typ string) *MultiGetItem {
167 item.typ = typ
168 return item
169 }
170
171 // Id specifies the identifier of the document.
172 func (item *MultiGetItem) Id(id string) *MultiGetItem {
173 item.id = id
174 return item
175 }
176
177 // Routing is the specific routing value.
178 func (item *MultiGetItem) Routing(routing string) *MultiGetItem {
179 item.routing = routing
180 return item
181 }
182
183 // StoredFields is a list of fields to return in the response.
184 func (item *MultiGetItem) StoredFields(storedFields ...string) *MultiGetItem {
185 item.storedFields = append(item.storedFields, storedFields...)
186 return item
187 }
188
189 // Version can be MatchAny (-3), MatchAnyPre120 (0), NotFound (-1),
190 // or NotSet (-2). These are specified in org.elasticsearch.common.lucene.uid.Versions.
191 // The default in Elasticsearch is MatchAny (-3).
192 func (item *MultiGetItem) Version(version int64) *MultiGetItem {
193 item.version = &version
194 return item
195 }
196
197 // VersionType can be "internal", "external", "external_gt", "external_gte",
198 // or "force". See org.elasticsearch.index.VersionType in Elasticsearch source.
199 // It is "internal" by default.
200 func (item *MultiGetItem) VersionType(versionType string) *MultiGetItem {
201 item.versionType = versionType
202 return item
203 }
204
205 // FetchSource allows to specify source filtering.
206 func (item *MultiGetItem) FetchSource(fetchSourceContext *FetchSourceContext) *MultiGetItem {
207 item.fsc = fetchSourceContext
208 return item
209 }
210
211 // Source returns the serialized JSON to be sent to Elasticsearch as
212 // part of a MultiGet search.
213 func (item *MultiGetItem) Source() (interface{}, error) {
214 source := make(map[string]interface{})
215
216 source["_id"] = item.id
217
218 if item.index != "" {
219 source["_index"] = item.index
220 }
221 if item.typ != "" {
222 source["_type"] = item.typ
223 }
224 if item.fsc != nil {
225 src, err := item.fsc.Source()
226 if err != nil {
227 return nil, err
228 }
229 source["_source"] = src
230 }
231 if item.routing != "" {
232 source["_routing"] = item.routing
233 }
234 if len(item.storedFields) > 0 {
235 source["stored_fields"] = strings.Join(item.storedFields, ",")
236 }
237 if item.version != nil {
238 source["version"] = fmt.Sprintf("%d", *item.version)
239 }
240 if item.versionType != "" {
241 source["version_type"] = item.versionType
242 }
243
244 return source, nil
245 }
246
247 // -- Result of a Multi Get request.
248
249 // MgetResponse is the outcome of a Multi GET API request.
250 type MgetResponse struct {
251 Docs []*GetResult `json:"docs,omitempty"`
252 }