bulk_update_request.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. package elastic
  5. //go:generate easyjson bulk_update_request.go
  6. import (
  7. "encoding/json"
  8. "fmt"
  9. "strings"
  10. )
  11. // BulkUpdateRequest is a request to update a document in Elasticsearch.
  12. //
  13. // See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/docs-bulk.html
  14. // for details.
  15. type BulkUpdateRequest struct {
  16. BulkableRequest
  17. index string
  18. typ string
  19. id string
  20. routing string
  21. parent string
  22. script *Script
  23. scriptedUpsert *bool
  24. version int64 // default is MATCH_ANY
  25. versionType string // default is "internal"
  26. retryOnConflict *int
  27. upsert interface{}
  28. docAsUpsert *bool
  29. detectNoop *bool
  30. doc interface{}
  31. returnSource *bool
  32. source []string
  33. useEasyJSON bool
  34. }
  35. //easyjson:json
  36. type bulkUpdateRequestCommand map[string]bulkUpdateRequestCommandOp
  37. //easyjson:json
  38. type bulkUpdateRequestCommandOp struct {
  39. Id string `json:"_id,omitempty"`
  40. Index string `json:"_index,omitempty"`
  41. Type string `json:"_type,omitempty"`
  42. Parent string `json:"_parent,omitempty"`
  43. RetryOnConflict *int `json:"_retry_on_conflict,omitempty"`
  44. Routing string `json:"_routing,omitempty"`
  45. Version int64 `json:"_version,omitempty"`
  46. VersionType string `json:"_version_type,omitempty"`
  47. }
  48. //easyjson:json
  49. type bulkUpdateRequestCommandData struct {
  50. DetectNoop *bool `json:"detect_noop,omitempty"`
  51. Doc interface{} `json:"doc,omitempty"`
  52. DocAsUpsert *bool `json:"doc_as_upsert,omitempty"`
  53. Upsert interface{} `json:"upsert,omitempty"`
  54. Script interface{} `json:"script,omitempty"`
  55. ScriptedUpsert *bool `json:"scripted_upsert,omitempty"`
  56. Source *bool `json:"_source,omitempty"`
  57. }
  58. // NewBulkUpdateRequest returns a new BulkUpdateRequest.
  59. func NewBulkUpdateRequest() *BulkUpdateRequest {
  60. return &BulkUpdateRequest{}
  61. }
  62. // UseEasyJSON is an experimental setting that enables serialization
  63. // with github.com/mailru/easyjson, which should in faster serialization
  64. // time and less allocations, but removed compatibility with encoding/json,
  65. // usage of unsafe etc. See https://github.com/mailru/easyjson#issues-notes-and-limitations
  66. // for details. This setting is disabled by default.
  67. func (r *BulkUpdateRequest) UseEasyJSON(enable bool) *BulkUpdateRequest {
  68. r.useEasyJSON = enable
  69. return r
  70. }
  71. // Index specifies the Elasticsearch index to use for this update request.
  72. // If unspecified, the index set on the BulkService will be used.
  73. func (r *BulkUpdateRequest) Index(index string) *BulkUpdateRequest {
  74. r.index = index
  75. r.source = nil
  76. return r
  77. }
  78. // Type specifies the Elasticsearch type to use for this update request.
  79. // If unspecified, the type set on the BulkService will be used.
  80. func (r *BulkUpdateRequest) Type(typ string) *BulkUpdateRequest {
  81. r.typ = typ
  82. r.source = nil
  83. return r
  84. }
  85. // Id specifies the identifier of the document to update.
  86. func (r *BulkUpdateRequest) Id(id string) *BulkUpdateRequest {
  87. r.id = id
  88. r.source = nil
  89. return r
  90. }
  91. // Routing specifies a routing value for the request.
  92. func (r *BulkUpdateRequest) Routing(routing string) *BulkUpdateRequest {
  93. r.routing = routing
  94. r.source = nil
  95. return r
  96. }
  97. // Parent specifies the identifier of the parent document (if available).
  98. func (r *BulkUpdateRequest) Parent(parent string) *BulkUpdateRequest {
  99. r.parent = parent
  100. r.source = nil
  101. return r
  102. }
  103. // Script specifies an update script.
  104. // See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/docs-bulk.html#bulk-update
  105. // and https://www.elastic.co/guide/en/elasticsearch/reference/5.2/modules-scripting.html
  106. // for details.
  107. func (r *BulkUpdateRequest) Script(script *Script) *BulkUpdateRequest {
  108. r.script = script
  109. r.source = nil
  110. return r
  111. }
  112. // ScripedUpsert specifies if your script will run regardless of
  113. // whether the document exists or not.
  114. //
  115. // See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/docs-update.html#_literal_scripted_upsert_literal
  116. func (r *BulkUpdateRequest) ScriptedUpsert(upsert bool) *BulkUpdateRequest {
  117. r.scriptedUpsert = &upsert
  118. r.source = nil
  119. return r
  120. }
  121. // RetryOnConflict specifies how often to retry in case of a version conflict.
  122. func (r *BulkUpdateRequest) RetryOnConflict(retryOnConflict int) *BulkUpdateRequest {
  123. r.retryOnConflict = &retryOnConflict
  124. r.source = nil
  125. return r
  126. }
  127. // Version indicates the version of the document as part of an optimistic
  128. // concurrency model.
  129. func (r *BulkUpdateRequest) Version(version int64) *BulkUpdateRequest {
  130. r.version = version
  131. r.source = nil
  132. return r
  133. }
  134. // VersionType can be "internal" (default), "external", "external_gte",
  135. // "external_gt", or "force".
  136. func (r *BulkUpdateRequest) VersionType(versionType string) *BulkUpdateRequest {
  137. r.versionType = versionType
  138. r.source = nil
  139. return r
  140. }
  141. // Doc specifies the updated document.
  142. func (r *BulkUpdateRequest) Doc(doc interface{}) *BulkUpdateRequest {
  143. r.doc = doc
  144. r.source = nil
  145. return r
  146. }
  147. // DocAsUpsert indicates whether the contents of Doc should be used as
  148. // the Upsert value.
  149. //
  150. // See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/docs-update.html#_literal_doc_as_upsert_literal
  151. // for details.
  152. func (r *BulkUpdateRequest) DocAsUpsert(docAsUpsert bool) *BulkUpdateRequest {
  153. r.docAsUpsert = &docAsUpsert
  154. r.source = nil
  155. return r
  156. }
  157. // DetectNoop specifies whether changes that don't affect the document
  158. // should be ignored (true) or unignored (false). This is enabled by default
  159. // in Elasticsearch.
  160. func (r *BulkUpdateRequest) DetectNoop(detectNoop bool) *BulkUpdateRequest {
  161. r.detectNoop = &detectNoop
  162. r.source = nil
  163. return r
  164. }
  165. // Upsert specifies the document to use for upserts. It will be used for
  166. // create if the original document does not exist.
  167. func (r *BulkUpdateRequest) Upsert(doc interface{}) *BulkUpdateRequest {
  168. r.upsert = doc
  169. r.source = nil
  170. return r
  171. }
  172. // ReturnSource specifies whether Elasticsearch should return the source
  173. // after the update. In the request, this responds to the `_source` field.
  174. // It is false by default.
  175. func (r *BulkUpdateRequest) ReturnSource(source bool) *BulkUpdateRequest {
  176. r.returnSource = &source
  177. r.source = nil
  178. return r
  179. }
  180. // String returns the on-wire representation of the update request,
  181. // concatenated as a single string.
  182. func (r *BulkUpdateRequest) String() string {
  183. lines, err := r.Source()
  184. if err != nil {
  185. return fmt.Sprintf("error: %v", err)
  186. }
  187. return strings.Join(lines, "\n")
  188. }
  189. // Source returns the on-wire representation of the update request,
  190. // split into an action-and-meta-data line and an (optional) source line.
  191. // See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/docs-bulk.html
  192. // for details.
  193. func (r *BulkUpdateRequest) Source() ([]string, error) {
  194. // { "update" : { "_index" : "test", "_type" : "type1", "_id" : "1", ... } }
  195. // { "doc" : { "field1" : "value1", ... } }
  196. // or
  197. // { "update" : { "_index" : "test", "_type" : "type1", "_id" : "1", ... } }
  198. // { "script" : { ... } }
  199. if r.source != nil {
  200. return r.source, nil
  201. }
  202. lines := make([]string, 2)
  203. // "update" ...
  204. updateCommand := bulkUpdateRequestCommandOp{
  205. Index: r.index,
  206. Type: r.typ,
  207. Id: r.id,
  208. Routing: r.routing,
  209. Parent: r.parent,
  210. Version: r.version,
  211. VersionType: r.versionType,
  212. RetryOnConflict: r.retryOnConflict,
  213. }
  214. command := bulkUpdateRequestCommand{
  215. "update": updateCommand,
  216. }
  217. var err error
  218. var body []byte
  219. if r.useEasyJSON {
  220. // easyjson
  221. body, err = command.MarshalJSON()
  222. } else {
  223. // encoding/json
  224. body, err = json.Marshal(command)
  225. }
  226. if err != nil {
  227. return nil, err
  228. }
  229. lines[0] = string(body)
  230. // 2nd line: {"doc" : { ... }} or {"script": {...}}
  231. data := bulkUpdateRequestCommandData{
  232. DocAsUpsert: r.docAsUpsert,
  233. DetectNoop: r.detectNoop,
  234. Upsert: r.upsert,
  235. ScriptedUpsert: r.scriptedUpsert,
  236. Doc: r.doc,
  237. Source: r.returnSource,
  238. }
  239. if r.script != nil {
  240. script, err := r.script.Source()
  241. if err != nil {
  242. return nil, err
  243. }
  244. data.Script = script
  245. }
  246. if r.useEasyJSON {
  247. // easyjson
  248. body, err = data.MarshalJSON()
  249. } else {
  250. // encoding/json
  251. body, err = json.Marshal(data)
  252. }
  253. if err != nil {
  254. return nil, err
  255. }
  256. lines[1] = string(body)
  257. r.source = lines
  258. return lines, nil
  259. }