Debian Codegraph @

delete.go

raw
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 import (
8 "context"
9 "fmt"
10 "net/url"
11
12 "net/http"
13
14 "gopkg.in/olivere/elastic.v5/uritemplates"
15 )
16
17 // DeleteService allows to delete a typed JSON document from a specified
18 // index based on its id.
19 //
20 // See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/docs-delete.html
21 // for details.
22 type DeleteService struct {
23 client *Client
24 pretty bool
25 id string
26 index string
27 typ string
28 routing string
29 timeout string
30 version interface{}
31 versionType string
32 waitForActiveShards string
33 parent string
34 refresh string
35 }
36
37 // NewDeleteService creates a new DeleteService.
38 func NewDeleteService(client *Client) *DeleteService {
39 return &DeleteService{
40 client: client,
41 }
42 }
43
44 // Type is the type of the document.
45 func (s *DeleteService) Type(typ string) *DeleteService {
46 s.typ = typ
47 return s
48 }
49
50 // Id is the document ID.
51 func (s *DeleteService) Id(id string) *DeleteService {
52 s.id = id
53 return s
54 }
55
56 // Index is the name of the index.
57 func (s *DeleteService) Index(index string) *DeleteService {
58 s.index = index
59 return s
60 }
61
62 // Routing is a specific routing value.
63 func (s *DeleteService) Routing(routing string) *DeleteService {
64 s.routing = routing
65 return s
66 }
67
68 // Timeout is an explicit operation timeout.
69 func (s *DeleteService) Timeout(timeout string) *DeleteService {
70 s.timeout = timeout
71 return s
72 }
73
74 // Version is an explicit version number for concurrency control.
75 func (s *DeleteService) Version(version interface{}) *DeleteService {
76 s.version = version
77 return s
78 }
79
80 // VersionType is a specific version type.
81 func (s *DeleteService) VersionType(versionType string) *DeleteService {
82 s.versionType = versionType
83 return s
84 }
85
86 // WaitForActiveShards sets the number of shard copies that must be active
87 // before proceeding with the delete operation. Defaults to 1, meaning the
88 // primary shard only. Set to `all` for all shard copies, otherwise set to
89 // any non-negative value less than or equal to the total number of copies
90 // for the shard (number of replicas + 1).
91 func (s *DeleteService) WaitForActiveShards(waitForActiveShards string) *DeleteService {
92 s.waitForActiveShards = waitForActiveShards
93 return s
94 }
95
96 // Parent is the ID of parent document.
97 func (s *DeleteService) Parent(parent string) *DeleteService {
98 s.parent = parent
99 return s
100 }
101
102 // Refresh the index after performing the operation.
103 func (s *DeleteService) Refresh(refresh string) *DeleteService {
104 s.refresh = refresh
105 return s
106 }
107
108 // Pretty indicates that the JSON response be indented and human readable.
109 func (s *DeleteService) Pretty(pretty bool) *DeleteService {
110 s.pretty = pretty
111 return s
112 }
113
114 // buildURL builds the URL for the operation.
115 func (s *DeleteService) buildURL() (string, url.Values, error) {
116 // Build URL
117 path, err := uritemplates.Expand("/{index}/{type}/{id}", map[string]string{
118 "index": s.index,
119 "type": s.typ,
120 "id": s.id,
121 })
122 if err != nil {
123 return "", url.Values{}, err
124 }
125
126 // Add query string parameters
127 params := url.Values{}
128 if s.pretty {
129 params.Set("pretty", "1")
130 }
131 if s.refresh != "" {
132 params.Set("refresh", s.refresh)
133 }
134 if s.routing != "" {
135 params.Set("routing", s.routing)
136 }
137 if s.timeout != "" {
138 params.Set("timeout", s.timeout)
139 }
140 if s.version != nil {
141 params.Set("version", fmt.Sprintf("%v", s.version))
142 }
143 if s.versionType != "" {
144 params.Set("version_type", s.versionType)
145 }
146 if s.waitForActiveShards != "" {
147 params.Set("wait_for_active_shards", s.waitForActiveShards)
148 }
149 if s.parent != "" {
150 params.Set("parent", s.parent)
151 }
152 return path, params, nil
153 }
154
155 // Validate checks if the operation is valid.
156 func (s *DeleteService) Validate() error {
157 var invalid []string
158 if s.typ == "" {
159 invalid = append(invalid, "Type")
160 }
161 if s.id == "" {
162 invalid = append(invalid, "Id")
163 }
164 if s.index == "" {
165 invalid = append(invalid, "Index")
166 }
167 if len(invalid) > 0 {
168 return fmt.Errorf("missing required fields: %v", invalid)
169 }
170 return nil
171 }
172
173 // Do executes the operation. If the document is not found (404), Elasticsearch will
174 // still return a response. This response is serialized and returned as well. In other
175 // words, for HTTP status code 404, both an error and a response might be returned.
176 func (s *DeleteService) Do(ctx context.Context) (*DeleteResponse, error) {
177 // Check pre-conditions
178 if err := s.Validate(); err != nil {
179 return nil, err
180 }
181
182 // Get URL for request
183 path, params, err := s.buildURL()
184 if err != nil {
185 return nil, err
186 }
187
188 // Get HTTP response
189 res, err := s.client.PerformRequest(ctx, "DELETE", path, params, nil, http.StatusNotFound)
190 if err != nil {
191 return nil, err
192 }
193
194 // Return operation response
195 ret := new(DeleteResponse)
196 if err := s.client.decoder.Decode(res.Body, ret); err != nil {
197 return nil, err
198 }
199
200 // If we have a 404, we return both a result and an error, just like ES does
201 if res.StatusCode == http.StatusNotFound {
202 return ret, &Error{Status: http.StatusNotFound}
203 }
204
205 return ret, nil
206 }
207
208 // -- Result of a delete request.
209
210 // DeleteResponse is the outcome of running DeleteService.Do.
211 type DeleteResponse struct {
212 Index string `json:"_index,omitempty"`
213 Type string `json:"_type,omitempty"`
214 Id string `json:"_id,omitempty"`
215 Version int64 `json:"_version,omitempty"`
216 Result string `json:"result,omitempty"`
217 Shards *ShardsInfo `json:"_shards,omitempty"`
218 SeqNo int64 `json:"_seq_no,omitempty"`
219 PrimaryTerm int64 `json:"_primary_term,omitempty"`
220 Status int `json:"status,omitempty"`
221 ForcedRefresh bool `json:"forced_refresh,omitempty"`
222 Found bool `json:"found,omitempty"`
223 }