Debian Codegraph @

update.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 // UpdateService updates a document in Elasticsearch.
17 // See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/docs-update.html
18 // for details.
19 type UpdateService struct {
20 client *Client
21 index string
22 typ string
23 id string
24 routing string
25 parent string
26 script *Script
27 fields []string
28 fsc *FetchSourceContext
29 version *int64
30 versionType string
31 retryOnConflict *int
32 refresh string
33 waitForActiveShards string
34 upsert interface{}
35 scriptedUpsert *bool
36 docAsUpsert *bool
37 detectNoop *bool
38 doc interface{}
39 timeout string
40 pretty bool
41 }
42
43 // NewUpdateService creates the service to update documents in Elasticsearch.
44 func NewUpdateService(client *Client) *UpdateService {
45 builder := &UpdateService{
46 client: client,
47 fields: make([]string, 0),
48 }
49 return builder
50 }
51
52 // Index is the name of the Elasticsearch index (required).
53 func (b *UpdateService) Index(name string) *UpdateService {
54 b.index = name
55 return b
56 }
57
58 // Type is the type of the document (required).
59 func (b *UpdateService) Type(typ string) *UpdateService {
60 b.typ = typ
61 return b
62 }
63
64 // Id is the identifier of the document to update (required).
65 func (b *UpdateService) Id(id string) *UpdateService {
66 b.id = id
67 return b
68 }
69
70 // Routing specifies a specific routing value.
71 func (b *UpdateService) Routing(routing string) *UpdateService {
72 b.routing = routing
73 return b
74 }
75
76 // Parent sets the id of the parent document.
77 func (b *UpdateService) Parent(parent string) *UpdateService {
78 b.parent = parent
79 return b
80 }
81
82 // Script is the script definition.
83 func (b *UpdateService) Script(script *Script) *UpdateService {
84 b.script = script
85 return b
86 }
87
88 // RetryOnConflict specifies how many times the operation should be retried
89 // when a conflict occurs (default: 0).
90 func (b *UpdateService) RetryOnConflict(retryOnConflict int) *UpdateService {
91 b.retryOnConflict = &retryOnConflict
92 return b
93 }
94
95 // Fields is a list of fields to return in the response.
96 func (b *UpdateService) Fields(fields ...string) *UpdateService {
97 b.fields = make([]string, 0, len(fields))
98 b.fields = append(b.fields, fields...)
99 return b
100 }
101
102 // Version defines the explicit version number for concurrency control.
103 func (b *UpdateService) Version(version int64) *UpdateService {
104 b.version = &version
105 return b
106 }
107
108 // VersionType is one of "internal" or "force".
109 func (b *UpdateService) VersionType(versionType string) *UpdateService {
110 b.versionType = versionType
111 return b
112 }
113
114 // Refresh the index after performing the update.
115 func (b *UpdateService) Refresh(refresh string) *UpdateService {
116 b.refresh = refresh
117 return b
118 }
119
120 // WaitForActiveShards sets the number of shard copies that must be active before
121 // proceeding with the update operation. Defaults to 1, meaning the primary shard only.
122 // Set to `all` for all shard copies, otherwise set to any non-negative value less than
123 // or equal to the total number of copies for the shard (number of replicas + 1).
124 func (b *UpdateService) WaitForActiveShards(waitForActiveShards string) *UpdateService {
125 b.waitForActiveShards = waitForActiveShards
126 return b
127 }
128
129 // Doc allows for updating a partial document.
130 func (b *UpdateService) Doc(doc interface{}) *UpdateService {
131 b.doc = doc
132 return b
133 }
134
135 // Upsert can be used to index the document when it doesn't exist yet.
136 // Use this e.g. to initialize a document with a default value.
137 func (b *UpdateService) Upsert(doc interface{}) *UpdateService {
138 b.upsert = doc
139 return b
140 }
141
142 // DocAsUpsert can be used to insert the document if it doesn't already exist.
143 func (b *UpdateService) DocAsUpsert(docAsUpsert bool) *UpdateService {
144 b.docAsUpsert = &docAsUpsert
145 return b
146 }
147
148 // DetectNoop will instruct Elasticsearch to check if changes will occur
149 // when updating via Doc. It there aren't any changes, the request will
150 // turn into a no-op.
151 func (b *UpdateService) DetectNoop(detectNoop bool) *UpdateService {
152 b.detectNoop = &detectNoop
153 return b
154 }
155
156 // ScriptedUpsert should be set to true if the referenced script
157 // (defined in Script or ScriptId) should be called to perform an insert.
158 // The default is false.
159 func (b *UpdateService) ScriptedUpsert(scriptedUpsert bool) *UpdateService {
160 b.scriptedUpsert = &scriptedUpsert
161 return b
162 }
163
164 // Timeout is an explicit timeout for the operation, e.g. "1000", "1s" or "500ms".
165 func (b *UpdateService) Timeout(timeout string) *UpdateService {
166 b.timeout = timeout
167 return b
168 }
169
170 // Pretty instructs to return human readable, prettified JSON.
171 func (b *UpdateService) Pretty(pretty bool) *UpdateService {
172 b.pretty = pretty
173 return b
174 }
175
176 // FetchSource asks Elasticsearch to return the updated _source in the response.
177 func (s *UpdateService) FetchSource(fetchSource bool) *UpdateService {
178 if s.fsc == nil {
179 s.fsc = NewFetchSourceContext(fetchSource)
180 } else {
181 s.fsc.SetFetchSource(fetchSource)
182 }
183 return s
184 }
185
186 // FetchSourceContext indicates that _source should be returned in the response,
187 // allowing wildcard patterns to be defined via FetchSourceContext.
188 func (s *UpdateService) FetchSourceContext(fetchSourceContext *FetchSourceContext) *UpdateService {
189 s.fsc = fetchSourceContext
190 return s
191 }
192
193 // url returns the URL part of the document request.
194 func (b *UpdateService) url() (string, url.Values, error) {
195 // Build url
196 path := "/{index}/{type}/{id}/_update"
197 path, err := uritemplates.Expand(path, map[string]string{
198 "index": b.index,
199 "type": b.typ,
200 "id": b.id,
201 })
202 if err != nil {
203 return "", url.Values{}, err
204 }
205
206 // Parameters
207 params := make(url.Values)
208 if b.pretty {
209 params.Set("pretty", "true")
210 }
211 if b.routing != "" {
212 params.Set("routing", b.routing)
213 }
214 if b.parent != "" {
215 params.Set("parent", b.parent)
216 }
217 if b.timeout != "" {
218 params.Set("timeout", b.timeout)
219 }
220 if b.refresh != "" {
221 params.Set("refresh", b.refresh)
222 }
223 if b.waitForActiveShards != "" {
224 params.Set("wait_for_active_shards", b.waitForActiveShards)
225 }
226 if len(b.fields) > 0 {
227 params.Set("fields", strings.Join(b.fields, ","))
228 }
229 if b.version != nil {
230 params.Set("version", fmt.Sprintf("%d", *b.version))
231 }
232 if b.versionType != "" {
233 params.Set("version_type", b.versionType)
234 }
235 if b.retryOnConflict != nil {
236 params.Set("retry_on_conflict", fmt.Sprintf("%v", *b.retryOnConflict))
237 }
238
239 return path, params, nil
240 }
241
242 // body returns the body part of the document request.
243 func (b *UpdateService) body() (interface{}, error) {
244 source := make(map[string]interface{})
245
246 if b.script != nil {
247 src, err := b.script.Source()
248 if err != nil {
249 return nil, err
250 }
251 source["script"] = src
252 }
253
254 if b.scriptedUpsert != nil {
255 source["scripted_upsert"] = *b.scriptedUpsert
256 }
257
258 if b.upsert != nil {
259 source["upsert"] = b.upsert
260 }
261
262 if b.doc != nil {
263 source["doc"] = b.doc
264 }
265 if b.docAsUpsert != nil {
266 source["doc_as_upsert"] = *b.docAsUpsert
267 }
268 if b.detectNoop != nil {
269 source["detect_noop"] = *b.detectNoop
270 }
271 if b.fsc != nil {
272 src, err := b.fsc.Source()
273 if err != nil {
274 return nil, err
275 }
276 source["_source"] = src
277 }
278
279 return source, nil
280 }
281
282 // Do executes the update operation.
283 func (b *UpdateService) Do(ctx context.Context) (*UpdateResponse, error) {
284 path, params, err := b.url()
285 if err != nil {
286 return nil, err
287 }
288
289 // Get body of the request
290 body, err := b.body()
291 if err != nil {
292 return nil, err
293 }
294
295 // Get response
296 res, err := b.client.PerformRequest(ctx, "POST", path, params, body)
297 if err != nil {
298 return nil, err
299 }
300
301 // Return result
302 ret := new(UpdateResponse)
303 if err := b.client.decoder.Decode(res.Body, ret); err != nil {
304 return nil, err
305 }
306 return ret, nil
307 }
308
309 // UpdateResponse is the result of updating a document in Elasticsearch.
310 type UpdateResponse struct {
311 Index string `json:"_index"`
312 Type string `json:"_type"`
313 Id string `json:"_id"`
314 Version int `json:"_version"`
315 Shards *ShardsInfo `json:"_shards"`
316 Result string `json:"result,omitempty"`
317 ForcedRefresh bool `json:"forced_refresh,omitempty"`
318 GetResult *GetResult `json:"get,omitempty"`
319 }