This repository has been archived by the owner on Oct 21, 2024. It is now read-only.
forked from olivere/elastic
-
Notifications
You must be signed in to change notification settings - Fork 4
/
delete.go
130 lines (111 loc) · 2.51 KB
/
delete.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// 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"
"net/url"
"gopkg.in/olivere/elastic.v2/uritemplates"
)
type DeleteService struct {
client *Client
index string
_type string
id string
routing string
refresh *bool
version *int
pretty bool
}
func NewDeleteService(client *Client) *DeleteService {
builder := &DeleteService{
client: client,
}
return builder
}
func (s *DeleteService) Index(index string) *DeleteService {
s.index = index
return s
}
func (s *DeleteService) Type(_type string) *DeleteService {
s._type = _type
return s
}
func (s *DeleteService) Id(id string) *DeleteService {
s.id = id
return s
}
func (s *DeleteService) Parent(parent string) *DeleteService {
if s.routing == "" {
s.routing = parent
}
return s
}
func (s *DeleteService) Refresh(refresh bool) *DeleteService {
s.refresh = &refresh
return s
}
func (s *DeleteService) Version(version int) *DeleteService {
s.version = &version
return s
}
func (s *DeleteService) Pretty(pretty bool) *DeleteService {
s.pretty = pretty
return s
}
// Do deletes the document. It fails if any of index, type, and identifier
// are missing.
func (s *DeleteService) Do() (*DeleteResult, error) {
if s.index == "" {
return nil, ErrMissingIndex
}
if s._type == "" {
return nil, ErrMissingType
}
if s.id == "" {
return nil, ErrMissingId
}
// Build url
path, err := uritemplates.Expand("/{index}/{type}/{id}", map[string]string{
"index": s.index,
"type": s._type,
"id": s.id,
})
if err != nil {
return nil, err
}
// Parameters
params := make(url.Values)
if s.refresh != nil {
params.Set("refresh", fmt.Sprintf("%v", *s.refresh))
}
if s.version != nil {
params.Set("version", fmt.Sprintf("%d", *s.version))
}
if s.routing != "" {
params.Set("routing", fmt.Sprintf("%s", s.routing))
}
if s.pretty {
params.Set("pretty", fmt.Sprintf("%v", s.pretty))
}
// Get response
res, err := s.client.PerformRequest("DELETE", path, params, nil)
if err != nil {
return nil, err
}
// Return response
ret := new(DeleteResult)
if err := json.Unmarshal(res.Body, ret); err != nil {
return nil, err
}
return ret, nil
}
// -- Result of a delete request.
type DeleteResult struct {
Found bool `json:"found"`
Index string `json:"_index"`
Type string `json:"_type"`
Id string `json:"_id"`
Version int64 `json:"_version"`
}