forked from olivere/elastic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bulk_delete_request.go
112 lines (94 loc) · 2.35 KB
/
bulk_delete_request.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright 2012-2015 Oliver Eilhard. All rights reserved.
// Use of this source code is governed by a MIT-license.
// See http://olivere.mit-license.org/license.txt for details.
package elastic
import (
"encoding/json"
"fmt"
"strings"
)
// -- Bulk delete request --
// Bulk request to remove document from Elasticsearch.
type BulkDeleteRequest struct {
BulkableRequest
index string
typ string
id string
routing string
refresh *bool
version int64 // default is MATCH_ANY
versionType string // default is "internal"
}
func NewBulkDeleteRequest() *BulkDeleteRequest {
return &BulkDeleteRequest{}
}
func (r *BulkDeleteRequest) Index(index string) *BulkDeleteRequest {
r.index = index
return r
}
func (r *BulkDeleteRequest) Type(typ string) *BulkDeleteRequest {
r.typ = typ
return r
}
func (r *BulkDeleteRequest) Id(id string) *BulkDeleteRequest {
r.id = id
return r
}
func (r *BulkDeleteRequest) Routing(routing string) *BulkDeleteRequest {
r.routing = routing
return r
}
func (r *BulkDeleteRequest) Refresh(refresh bool) *BulkDeleteRequest {
r.refresh = &refresh
return r
}
func (r *BulkDeleteRequest) Version(version int64) *BulkDeleteRequest {
r.version = version
return r
}
// VersionType can be "internal" (default), "external", "external_gte",
// "external_gt", or "force".
func (r *BulkDeleteRequest) VersionType(versionType string) *BulkDeleteRequest {
r.versionType = versionType
return r
}
func (r *BulkDeleteRequest) String() string {
lines, err := r.Source()
if err == nil {
return strings.Join(lines, "\n")
}
return fmt.Sprintf("error: %v", err)
}
func (r *BulkDeleteRequest) Source() ([]string, error) {
lines := make([]string, 1)
source := make(map[string]interface{})
deleteCommand := make(map[string]interface{})
if r.index != "" {
deleteCommand["_index"] = r.index
}
if r.typ != "" {
deleteCommand["_type"] = r.typ
}
if r.id != "" {
deleteCommand["_id"] = r.id
}
if r.routing != "" {
deleteCommand["_routing"] = r.routing
}
if r.version > 0 {
deleteCommand["_version"] = r.version
}
if r.versionType != "" {
deleteCommand["_version_type"] = r.versionType
}
if r.refresh != nil {
deleteCommand["refresh"] = *r.refresh
}
source["delete"] = deleteCommand
body, err := json.Marshal(source)
if err != nil {
return nil, err
}
lines[0] = string(body)
return lines, nil
}