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
5package elastic
6
7//go:generate easyjson bulk_delete_request.go
8
9import (
10	"encoding/json"
11	"fmt"
12	"strings"
13)
14
15// -- Bulk delete request --
16
17// BulkDeleteRequest is a request to remove a document from Elasticsearch.
18//
19// See https://www.elastic.co/guide/en/elasticsearch/reference/6.8/docs-bulk.html
20// for details.
21type BulkDeleteRequest struct {
22	BulkableRequest
23	index         string
24	typ           string
25	id            string
26	parent        string
27	routing       string
28	version       int64  // default is MATCH_ANY
29	versionType   string // default is "internal"
30	ifSeqNo       *int64
31	ifPrimaryTerm *int64
32
33	source []string
34
35	useEasyJSON bool
36}
37
38//easyjson:json
39type bulkDeleteRequestCommand map[string]bulkDeleteRequestCommandOp
40
41//easyjson:json
42type bulkDeleteRequestCommandOp struct {
43	Index         string `json:"_index,omitempty"`
44	Type          string `json:"_type,omitempty"`
45	Id            string `json:"_id,omitempty"`
46	Parent        string `json:"parent,omitempty"`
47	Routing       string `json:"routing,omitempty"`
48	Version       int64  `json:"version,omitempty"`
49	VersionType   string `json:"version_type,omitempty"`
50	IfSeqNo       *int64 `json:"if_seq_no,omitempty"`
51	IfPrimaryTerm *int64 `json:"if_primary_term,omitempty"`
52}
53
54// NewBulkDeleteRequest returns a new BulkDeleteRequest.
55func NewBulkDeleteRequest() *BulkDeleteRequest {
56	return &BulkDeleteRequest{}
57}
58
59// UseEasyJSON is an experimental setting that enables serialization
60// with github.com/mailru/easyjson, which should in faster serialization
61// time and less allocations, but removed compatibility with encoding/json,
62// usage of unsafe etc. See https://github.com/mailru/easyjson#issues-notes-and-limitations
63// for details. This setting is disabled by default.
64func (r *BulkDeleteRequest) UseEasyJSON(enable bool) *BulkDeleteRequest {
65	r.useEasyJSON = enable
66	return r
67}
68
69// Index specifies the Elasticsearch index to use for this delete request.
70// If unspecified, the index set on the BulkService will be used.
71func (r *BulkDeleteRequest) Index(index string) *BulkDeleteRequest {
72	r.index = index
73	r.source = nil
74	return r
75}
76
77// Type specifies the Elasticsearch type to use for this delete request.
78// If unspecified, the type set on the BulkService will be used.
79func (r *BulkDeleteRequest) Type(typ string) *BulkDeleteRequest {
80	r.typ = typ
81	r.source = nil
82	return r
83}
84
85// Id specifies the identifier of the document to delete.
86func (r *BulkDeleteRequest) Id(id string) *BulkDeleteRequest {
87	r.id = id
88	r.source = nil
89	return r
90}
91
92// Parent specifies the parent of the request, which is used in parent/child
93// mappings.
94func (r *BulkDeleteRequest) Parent(parent string) *BulkDeleteRequest {
95	r.parent = parent
96	r.source = nil
97	return r
98}
99
100// Routing specifies a routing value for the request.
101func (r *BulkDeleteRequest) Routing(routing string) *BulkDeleteRequest {
102	r.routing = routing
103	r.source = nil
104	return r
105}
106
107// Version indicates the version to be deleted as part of an optimistic
108// concurrency model.
109func (r *BulkDeleteRequest) Version(version int64) *BulkDeleteRequest {
110	r.version = version
111	r.source = nil
112	return r
113}
114
115// VersionType can be "internal" (default), "external", "external_gte",
116// or "external_gt".
117func (r *BulkDeleteRequest) VersionType(versionType string) *BulkDeleteRequest {
118	r.versionType = versionType
119	r.source = nil
120	return r
121}
122
123// IfSeqNo indicates to only perform the delete operation if the last
124// operation that has changed the document has the specified sequence number.
125func (r *BulkDeleteRequest) IfSeqNo(ifSeqNo int64) *BulkDeleteRequest {
126	r.ifSeqNo = &ifSeqNo
127	return r
128}
129
130// IfPrimaryTerm indicates to only perform the delete operation if the
131// last operation that has changed the document has the specified primary term.
132func (r *BulkDeleteRequest) IfPrimaryTerm(ifPrimaryTerm int64) *BulkDeleteRequest {
133	r.ifPrimaryTerm = &ifPrimaryTerm
134	return r
135}
136
137// String returns the on-wire representation of the delete request,
138// concatenated as a single string.
139func (r *BulkDeleteRequest) String() string {
140	lines, err := r.Source()
141	if err != nil {
142		return fmt.Sprintf("error: %v", err)
143	}
144	return strings.Join(lines, "\n")
145}
146
147// Source returns the on-wire representation of the delete request,
148// split into an action-and-meta-data line and an (optional) source line.
149// See https://www.elastic.co/guide/en/elasticsearch/reference/6.8/docs-bulk.html
150// for details.
151func (r *BulkDeleteRequest) Source() ([]string, error) {
152	if r.source != nil {
153		return r.source, nil
154	}
155	command := bulkDeleteRequestCommand{
156		"delete": bulkDeleteRequestCommandOp{
157			Index:         r.index,
158			Type:          r.typ,
159			Id:            r.id,
160			Routing:       r.routing,
161			Parent:        r.parent,
162			Version:       r.version,
163			VersionType:   r.versionType,
164			IfSeqNo:       r.ifSeqNo,
165			IfPrimaryTerm: r.ifPrimaryTerm,
166		},
167	}
168
169	var err error
170	var body []byte
171	if r.useEasyJSON {
172		// easyjson
173		body, err = command.MarshalJSON()
174	} else {
175		// encoding/json
176		body, err = json.Marshal(command)
177	}
178	if err != nil {
179		return nil, err
180	}
181
182	lines := []string{string(body)}
183	r.source = lines
184
185	return lines, nil
186}
187