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