Debian Codegraph @

mtermvectors.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 // MultiTermvectorService returns information and statistics on terms in the
18 // fields of a particular document. The document could be stored in the
19 // index or artificially provided by the user.
20 //
21 // See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/docs-multi-termvectors.html
22 // for documentation.
23 type MultiTermvectorService struct {
24 client *Client
25 pretty bool
26 index string
27 typ string
28 fieldStatistics *bool
29 fields []string
30 ids []string
31 offsets *bool
32 parent string
33 payloads *bool
34 positions *bool
35 preference string
36 realtime *bool
37 routing string
38 termStatistics *bool
39 version interface{}
40 versionType string
41 bodyJson interface{}
42 bodyString string
43 docs []*MultiTermvectorItem
44 }
45
46 // NewMultiTermvectorService creates a new MultiTermvectorService.
47 func NewMultiTermvectorService(client *Client) *MultiTermvectorService {
48 return &MultiTermvectorService{
49 client: client,
50 }
51 }
52
53 // Pretty indicates that the JSON response be indented and human readable.
54 func (s *MultiTermvectorService) Pretty(pretty bool) *MultiTermvectorService {
55 s.pretty = pretty
56 return s
57 }
58
59 // Add adds documents to MultiTermvectors service.
60 func (s *MultiTermvectorService) Add(docs ...*MultiTermvectorItem) *MultiTermvectorService {
61 s.docs = append(s.docs, docs...)
62 return s
63 }
64
65 // Index in which the document resides.
66 func (s *MultiTermvectorService) Index(index string) *MultiTermvectorService {
67 s.index = index
68 return s
69 }
70
71 // Type of the document.
72 func (s *MultiTermvectorService) Type(typ string) *MultiTermvectorService {
73 s.typ = typ
74 return s
75 }
76
77 // FieldStatistics specifies if document count, sum of document frequencies and sum of total term frequencies should be returned. Applies to all returned documents unless otherwise specified in body "params" or "docs".
78 func (s *MultiTermvectorService) FieldStatistics(fieldStatistics bool) *MultiTermvectorService {
79 s.fieldStatistics = &fieldStatistics
80 return s
81 }
82
83 // Fields is a comma-separated list of fields to return. Applies to all returned documents unless otherwise specified in body "params" or "docs".
84 func (s *MultiTermvectorService) Fields(fields []string) *MultiTermvectorService {
85 s.fields = fields
86 return s
87 }
88
89 // Ids is a comma-separated list of documents ids. You must define ids as parameter or set "ids" or "docs" in the request body.
90 func (s *MultiTermvectorService) Ids(ids []string) *MultiTermvectorService {
91 s.ids = ids
92 return s
93 }
94
95 // Offsets specifies if term offsets should be returned. Applies to all returned documents unless otherwise specified in body "params" or "docs".
96 func (s *MultiTermvectorService) Offsets(offsets bool) *MultiTermvectorService {
97 s.offsets = &offsets
98 return s
99 }
100
101 // Parent id of documents. Applies to all returned documents unless otherwise specified in body "params" or "docs".
102 func (s *MultiTermvectorService) Parent(parent string) *MultiTermvectorService {
103 s.parent = parent
104 return s
105 }
106
107 // Payloads specifies if term payloads should be returned. Applies to all returned documents unless otherwise specified in body "params" or "docs".
108 func (s *MultiTermvectorService) Payloads(payloads bool) *MultiTermvectorService {
109 s.payloads = &payloads
110 return s
111 }
112
113 // Positions specifies if term positions should be returned. Applies to all returned documents unless otherwise specified in body "params" or "docs".
114 func (s *MultiTermvectorService) Positions(positions bool) *MultiTermvectorService {
115 s.positions = &positions
116 return s
117 }
118
119 // Preference specifies the node or shard the operation should be performed on (default: random). Applies to all returned documents unless otherwise specified in body "params" or "docs".
120 func (s *MultiTermvectorService) Preference(preference string) *MultiTermvectorService {
121 s.preference = preference
122 return s
123 }
124
125 // Realtime specifies if requests are real-time as opposed to near-real-time (default: true).
126 func (s *MultiTermvectorService) Realtime(realtime bool) *MultiTermvectorService {
127 s.realtime = &realtime
128 return s
129 }
130
131 // Routing specific routing value. Applies to all returned documents unless otherwise specified in body "params" or "docs".
132 func (s *MultiTermvectorService) Routing(routing string) *MultiTermvectorService {
133 s.routing = routing
134 return s
135 }
136
137 // TermStatistics specifies if total term frequency and document frequency should be returned. Applies to all returned documents unless otherwise specified in body "params" or "docs".
138 func (s *MultiTermvectorService) TermStatistics(termStatistics bool) *MultiTermvectorService {
139 s.termStatistics = &termStatistics
140 return s
141 }
142
143 // Version is explicit version number for concurrency control.
144 func (s *MultiTermvectorService) Version(version interface{}) *MultiTermvectorService {
145 s.version = version
146 return s
147 }
148
149 // VersionType is specific version type.
150 func (s *MultiTermvectorService) VersionType(versionType string) *MultiTermvectorService {
151 s.versionType = versionType
152 return s
153 }
154
155 // BodyJson is documented as: Define ids, documents, parameters or a list of parameters per document here. You must at least provide a list of document ids. See documentation..
156 func (s *MultiTermvectorService) BodyJson(body interface{}) *MultiTermvectorService {
157 s.bodyJson = body
158 return s
159 }
160
161 // BodyString is documented as: Define ids, documents, parameters or a list of parameters per document here. You must at least provide a list of document ids. See documentation..
162 func (s *MultiTermvectorService) BodyString(body string) *MultiTermvectorService {
163 s.bodyString = body
164 return s
165 }
166
167 func (s *MultiTermvectorService) Source() interface{} {
168 source := make(map[string]interface{})
169 docs := make([]interface{}, len(s.docs))
170 for i, doc := range s.docs {
171 docs[i] = doc.Source()
172 }
173 source["docs"] = docs
174 return source
175 }
176
177 // buildURL builds the URL for the operation.
178 func (s *MultiTermvectorService) buildURL() (string, url.Values, error) {
179 var path string
180 var err error
181
182 if s.index != "" && s.typ != "" {
183 path, err = uritemplates.Expand("/{index}/{type}/_mtermvectors", map[string]string{
184 "index": s.index,
185 "type": s.typ,
186 })
187 } else if s.index != "" && s.typ == "" {
188 path, err = uritemplates.Expand("/{index}/_mtermvectors", map[string]string{
189 "index": s.index,
190 })
191 } else {
192 path = "/_mtermvectors"
193 }
194 if err != nil {
195 return "", url.Values{}, err
196 }
197
198 // Add query string parameters
199 params := url.Values{}
200 if s.pretty {
201 params.Set("pretty", "1")
202 }
203 if s.fieldStatistics != nil {
204 params.Set("field_statistics", fmt.Sprintf("%v", *s.fieldStatistics))
205 }
206 if len(s.fields) > 0 {
207 params.Set("fields", strings.Join(s.fields, ","))
208 }
209 if len(s.ids) > 0 {
210 params.Set("ids", strings.Join(s.ids, ","))
211 }
212 if s.offsets != nil {
213 params.Set("offsets", fmt.Sprintf("%v", *s.offsets))
214 }
215 if s.parent != "" {
216 params.Set("parent", s.parent)
217 }
218 if s.payloads != nil {
219 params.Set("payloads", fmt.Sprintf("%v", *s.payloads))
220 }
221 if s.positions != nil {
222 params.Set("positions", fmt.Sprintf("%v", *s.positions))
223 }
224 if s.preference != "" {
225 params.Set("preference", s.preference)
226 }
227 if s.realtime != nil {
228 params.Set("realtime", fmt.Sprintf("%v", *s.realtime))
229 }
230 if s.routing != "" {
231 params.Set("routing", s.routing)
232 }
233 if s.termStatistics != nil {
234 params.Set("term_statistics", fmt.Sprintf("%v", *s.termStatistics))
235 }
236 if s.version != nil {
237 params.Set("version", fmt.Sprintf("%v", s.version))
238 }
239 if s.versionType != "" {
240 params.Set("version_type", s.versionType)
241 }
242 return path, params, nil
243 }
244
245 // Validate checks if the operation is valid.
246 func (s *MultiTermvectorService) Validate() error {
247 var invalid []string
248 if s.index == "" && s.typ != "" {
249 invalid = append(invalid, "Index")
250 }
251 if len(invalid) > 0 {
252 return fmt.Errorf("missing required fields: %v", invalid)
253 }
254 return nil
255 }
256
257 // Do executes the operation.
258 func (s *MultiTermvectorService) Do(ctx context.Context) (*MultiTermvectorResponse, error) {
259 // Check pre-conditions
260 if err := s.Validate(); err != nil {
261 return nil, err
262 }
263
264 // Get URL for request
265 path, params, err := s.buildURL()
266 if err != nil {
267 return nil, err
268 }
269
270 // Setup HTTP request body
271 var body interface{}
272 if s.bodyJson != nil {
273 body = s.bodyJson
274 } else if len(s.bodyString) > 0 {
275 body = s.bodyString
276 } else {
277 body = s.Source()
278 }
279
280 // Get HTTP response
281 res, err := s.client.PerformRequest(ctx, "GET", path, params, body)
282 if err != nil {
283 return nil, err
284 }
285
286 // Return operation response
287 ret := new(MultiTermvectorResponse)
288 if err := json.Unmarshal(res.Body, ret); err != nil {
289 return nil, err
290 }
291 return ret, nil
292 }
293
294 // MultiTermvectorResponse is the response of MultiTermvectorService.Do.
295 type MultiTermvectorResponse struct {
296 Docs []*TermvectorsResponse `json:"docs"`
297 }
298
299 // -- MultiTermvectorItem --
300
301 // MultiTermvectorItem is a single document to retrieve via MultiTermvectorService.
302 type MultiTermvectorItem struct {
303 index string
304 typ string
305 id string
306 doc interface{}
307 fieldStatistics *bool
308 fields []string
309 perFieldAnalyzer map[string]string
310 offsets *bool
311 parent string
312 payloads *bool
313 positions *bool
314 preference string
315 realtime *bool
316 routing string
317 termStatistics *bool
318 }
319
320 func NewMultiTermvectorItem() *MultiTermvectorItem {
321 return &MultiTermvectorItem{}
322 }
323
324 func (s *MultiTermvectorItem) Index(index string) *MultiTermvectorItem {
325 s.index = index
326 return s
327 }
328
329 func (s *MultiTermvectorItem) Type(typ string) *MultiTermvectorItem {
330 s.typ = typ
331 return s
332 }
333
334 func (s *MultiTermvectorItem) Id(id string) *MultiTermvectorItem {
335 s.id = id
336 return s
337 }
338
339 // Doc is the document to analyze.
340 func (s *MultiTermvectorItem) Doc(doc interface{}) *MultiTermvectorItem {
341 s.doc = doc
342 return s
343 }
344
345 // FieldStatistics specifies if document count, sum of document frequencies
346 // and sum of total term frequencies should be returned.
347 func (s *MultiTermvectorItem) FieldStatistics(fieldStatistics bool) *MultiTermvectorItem {
348 s.fieldStatistics = &fieldStatistics
349 return s
350 }
351
352 // Fields a list of fields to return.
353 func (s *MultiTermvectorItem) Fields(fields ...string) *MultiTermvectorItem {
354 if s.fields == nil {
355 s.fields = make([]string, 0)
356 }
357 s.fields = append(s.fields, fields...)
358 return s
359 }
360
361 // PerFieldAnalyzer allows to specify a different analyzer than the one
362 // at the field.
363 func (s *MultiTermvectorItem) PerFieldAnalyzer(perFieldAnalyzer map[string]string) *MultiTermvectorItem {
364 s.perFieldAnalyzer = perFieldAnalyzer
365 return s
366 }
367
368 // Offsets specifies if term offsets should be returned.
369 func (s *MultiTermvectorItem) Offsets(offsets bool) *MultiTermvectorItem {
370 s.offsets = &offsets
371 return s
372 }
373
374 // Parent id of documents.
375 func (s *MultiTermvectorItem) Parent(parent string) *MultiTermvectorItem {
376 s.parent = parent
377 return s
378 }
379
380 // Payloads specifies if term payloads should be returned.
381 func (s *MultiTermvectorItem) Payloads(payloads bool) *MultiTermvectorItem {
382 s.payloads = &payloads
383 return s
384 }
385
386 // Positions specifies if term positions should be returned.
387 func (s *MultiTermvectorItem) Positions(positions bool) *MultiTermvectorItem {
388 s.positions = &positions
389 return s
390 }
391
392 // Preference specify the node or shard the operation
393 // should be performed on (default: random).
394 func (s *MultiTermvectorItem) Preference(preference string) *MultiTermvectorItem {
395 s.preference = preference
396 return s
397 }
398
399 // Realtime specifies if request is real-time as opposed to
400 // near-real-time (default: true).
401 func (s *MultiTermvectorItem) Realtime(realtime bool) *MultiTermvectorItem {
402 s.realtime = &realtime
403 return s
404 }
405
406 // Routing is a specific routing value.
407 func (s *MultiTermvectorItem) Routing(routing string) *MultiTermvectorItem {
408 s.routing = routing
409 return s
410 }
411
412 // TermStatistics specifies if total term frequency and document frequency
413 // should be returned.
414 func (s *MultiTermvectorItem) TermStatistics(termStatistics bool) *MultiTermvectorItem {
415 s.termStatistics = &termStatistics
416 return s
417 }
418
419 // Source returns the serialized JSON to be sent to Elasticsearch as
420 // part of a MultiTermvector.
421 func (s *MultiTermvectorItem) Source() interface{} {
422 source := make(map[string]interface{})
423
424 source["_id"] = s.id
425
426 if s.index != "" {
427 source["_index"] = s.index
428 }
429 if s.typ != "" {
430 source["_type"] = s.typ
431 }
432 if s.fields != nil {
433 source["fields"] = s.fields
434 }
435 if s.fieldStatistics != nil {
436 source["field_statistics"] = fmt.Sprintf("%v", *s.fieldStatistics)
437 }
438 if s.offsets != nil {
439 source["offsets"] = s.offsets
440 }
441 if s.parent != "" {
442 source["parent"] = s.parent
443 }
444 if s.payloads != nil {
445 source["payloads"] = fmt.Sprintf("%v", *s.payloads)
446 }
447 if s.positions != nil {
448 source["positions"] = fmt.Sprintf("%v", *s.positions)
449 }
450 if s.preference != "" {
451 source["preference"] = s.preference
452 }
453 if s.realtime != nil {
454 source["realtime"] = fmt.Sprintf("%v", *s.realtime)
455 }
456 if s.routing != "" {
457 source["routing"] = s.routing
458 }
459 if s.termStatistics != nil {
460 source["term_statistics"] = fmt.Sprintf("%v", *s.termStatistics)
461 }
462 if s.doc != nil {
463 source["doc"] = s.doc
464 }
465 if s.perFieldAnalyzer != nil && len(s.perFieldAnalyzer) > 0 {
466 source["per_field_analyzer"] = s.perFieldAnalyzer
467 }
468
469 return source
470 }