delete_by_query.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. // Copyright 2012-2015 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. package elastic
  5. import (
  6. "encoding/json"
  7. "fmt"
  8. "net/url"
  9. "strings"
  10. "github.com/olivere/elastic/uritemplates"
  11. )
  12. // DeleteByQueryService deletes documents that match a query.
  13. // See http://www.elasticsearch.org/guide/en/elasticsearch/reference/master/docs-delete-by-query.html.
  14. type DeleteByQueryService struct {
  15. client *Client
  16. indices []string
  17. types []string
  18. analyzer string
  19. consistency string
  20. defaultOper string
  21. df string
  22. ignoreUnavailable *bool
  23. allowNoIndices *bool
  24. expandWildcards string
  25. replication string
  26. routing string
  27. timeout string
  28. pretty bool
  29. q string
  30. query Query
  31. }
  32. // NewDeleteByQueryService creates a new DeleteByQueryService.
  33. // You typically use the client's DeleteByQuery to get a reference to
  34. // the service.
  35. func NewDeleteByQueryService(client *Client) *DeleteByQueryService {
  36. builder := &DeleteByQueryService{
  37. client: client,
  38. }
  39. return builder
  40. }
  41. // Index limits the delete-by-query to a single index.
  42. // You can use _all to perform the operation on all indices.
  43. func (s *DeleteByQueryService) Index(index string) *DeleteByQueryService {
  44. if s.indices == nil {
  45. s.indices = make([]string, 0)
  46. }
  47. s.indices = append(s.indices, index)
  48. return s
  49. }
  50. // Indices sets the indices on which to perform the delete operation.
  51. func (s *DeleteByQueryService) Indices(indices ...string) *DeleteByQueryService {
  52. if s.indices == nil {
  53. s.indices = make([]string, 0)
  54. }
  55. s.indices = append(s.indices, indices...)
  56. return s
  57. }
  58. // Type limits the delete operation to the given type.
  59. func (s *DeleteByQueryService) Type(typ string) *DeleteByQueryService {
  60. if s.types == nil {
  61. s.types = make([]string, 0)
  62. }
  63. s.types = append(s.types, typ)
  64. return s
  65. }
  66. // Types limits the delete operation to the given types.
  67. func (s *DeleteByQueryService) Types(types ...string) *DeleteByQueryService {
  68. if s.types == nil {
  69. s.types = make([]string, 0)
  70. }
  71. s.types = append(s.types, types...)
  72. return s
  73. }
  74. // Analyzer to use for the query string.
  75. func (s *DeleteByQueryService) Analyzer(analyzer string) *DeleteByQueryService {
  76. s.analyzer = analyzer
  77. return s
  78. }
  79. // Consistency represents the specific write consistency setting for the operation.
  80. // It can be one, quorum, or all.
  81. func (s *DeleteByQueryService) Consistency(consistency string) *DeleteByQueryService {
  82. s.consistency = consistency
  83. return s
  84. }
  85. // DefaultOperator for query string query (AND or OR).
  86. func (s *DeleteByQueryService) DefaultOperator(defaultOperator string) *DeleteByQueryService {
  87. s.defaultOper = defaultOperator
  88. return s
  89. }
  90. // DF is the field to use as default where no field prefix is given in the query string.
  91. func (s *DeleteByQueryService) DF(defaultField string) *DeleteByQueryService {
  92. s.df = defaultField
  93. return s
  94. }
  95. // DefaultField is the field to use as default where no field prefix is given in the query string.
  96. // It is an alias to the DF func.
  97. func (s *DeleteByQueryService) DefaultField(defaultField string) *DeleteByQueryService {
  98. s.df = defaultField
  99. return s
  100. }
  101. // IgnoreUnavailable indicates whether specified concrete indices should be
  102. // ignored when unavailable (missing or closed).
  103. func (s *DeleteByQueryService) IgnoreUnavailable(ignore bool) *DeleteByQueryService {
  104. s.ignoreUnavailable = &ignore
  105. return s
  106. }
  107. // AllowNoIndices indicates whether to ignore if a wildcard indices
  108. // expression resolves into no concrete indices (including the _all string
  109. // or when no indices have been specified).
  110. func (s *DeleteByQueryService) AllowNoIndices(allow bool) *DeleteByQueryService {
  111. s.allowNoIndices = &allow
  112. return s
  113. }
  114. // ExpandWildcards indicates whether to expand wildcard expression to
  115. // concrete indices that are open, closed or both. It can be "open" or "closed".
  116. func (s *DeleteByQueryService) ExpandWildcards(expand string) *DeleteByQueryService {
  117. s.expandWildcards = expand
  118. return s
  119. }
  120. // Replication sets a specific replication type (sync or async).
  121. func (s *DeleteByQueryService) Replication(replication string) *DeleteByQueryService {
  122. s.replication = replication
  123. return s
  124. }
  125. // Q specifies the query in Lucene query string syntax. You can also use
  126. // Query to programmatically specify the query.
  127. func (s *DeleteByQueryService) Q(query string) *DeleteByQueryService {
  128. s.q = query
  129. return s
  130. }
  131. // QueryString is an alias to Q. Notice that you can also use Query to
  132. // programmatically set the query.
  133. func (s *DeleteByQueryService) QueryString(query string) *DeleteByQueryService {
  134. s.q = query
  135. return s
  136. }
  137. // Routing sets a specific routing value.
  138. func (s *DeleteByQueryService) Routing(routing string) *DeleteByQueryService {
  139. s.routing = routing
  140. return s
  141. }
  142. // Timeout sets an explicit operation timeout, e.g. "1s" or "10000ms".
  143. func (s *DeleteByQueryService) Timeout(timeout string) *DeleteByQueryService {
  144. s.timeout = timeout
  145. return s
  146. }
  147. // Pretty indents the JSON output from Elasticsearch.
  148. func (s *DeleteByQueryService) Pretty(pretty bool) *DeleteByQueryService {
  149. s.pretty = pretty
  150. return s
  151. }
  152. // Query sets the query programmatically.
  153. func (s *DeleteByQueryService) Query(query Query) *DeleteByQueryService {
  154. s.query = query
  155. return s
  156. }
  157. // Do executes the delete-by-query operation.
  158. func (s *DeleteByQueryService) Do() (*DeleteByQueryResult, error) {
  159. var err error
  160. // Build url
  161. path := "/"
  162. // Indices part
  163. indexPart := make([]string, 0)
  164. for _, index := range s.indices {
  165. index, err = uritemplates.Expand("{index}", map[string]string{
  166. "index": index,
  167. })
  168. if err != nil {
  169. return nil, err
  170. }
  171. indexPart = append(indexPart, index)
  172. }
  173. if len(indexPart) > 0 {
  174. path += strings.Join(indexPart, ",")
  175. }
  176. // Types part
  177. typesPart := make([]string, 0)
  178. for _, typ := range s.types {
  179. typ, err = uritemplates.Expand("{type}", map[string]string{
  180. "type": typ,
  181. })
  182. if err != nil {
  183. return nil, err
  184. }
  185. typesPart = append(typesPart, typ)
  186. }
  187. if len(typesPart) > 0 {
  188. path += "/" + strings.Join(typesPart, ",")
  189. }
  190. // Search
  191. path += "/_query"
  192. // Parameters
  193. params := make(url.Values)
  194. if s.analyzer != "" {
  195. params.Set("analyzer", s.analyzer)
  196. }
  197. if s.consistency != "" {
  198. params.Set("consistency", s.consistency)
  199. }
  200. if s.defaultOper != "" {
  201. params.Set("default_operator", s.defaultOper)
  202. }
  203. if s.df != "" {
  204. params.Set("df", s.df)
  205. }
  206. if s.ignoreUnavailable != nil {
  207. params.Set("ignore_unavailable", fmt.Sprintf("%v", *s.ignoreUnavailable))
  208. }
  209. if s.allowNoIndices != nil {
  210. params.Set("allow_no_indices", fmt.Sprintf("%v", *s.allowNoIndices))
  211. }
  212. if s.expandWildcards != "" {
  213. params.Set("expand_wildcards", s.expandWildcards)
  214. }
  215. if s.replication != "" {
  216. params.Set("replication", s.replication)
  217. }
  218. if s.routing != "" {
  219. params.Set("routing", s.routing)
  220. }
  221. if s.timeout != "" {
  222. params.Set("timeout", s.timeout)
  223. }
  224. if s.pretty {
  225. params.Set("pretty", fmt.Sprintf("%v", s.pretty))
  226. }
  227. if s.q != "" {
  228. params.Set("q", s.q)
  229. }
  230. // Set body if there is a query set
  231. var body interface{}
  232. if s.query != nil {
  233. query := make(map[string]interface{})
  234. query["query"] = s.query.Source()
  235. body = query
  236. }
  237. // Get response
  238. res, err := s.client.PerformRequest("DELETE", path, params, body)
  239. if err != nil {
  240. return nil, err
  241. }
  242. // Return result
  243. ret := new(DeleteByQueryResult)
  244. if err := json.Unmarshal(res.Body, ret); err != nil {
  245. return nil, err
  246. }
  247. return ret, nil
  248. }
  249. // DeleteByQueryResult is the outcome of executing Do with DeleteByQueryService.
  250. type DeleteByQueryResult struct {
  251. Indices map[string]IndexDeleteByQueryResult `json:"_indices"`
  252. }
  253. // IndexDeleteByQueryResult is the result of a delete-by-query for a specific
  254. // index.
  255. type IndexDeleteByQueryResult struct {
  256. Shards shardsInfo `json:"_shards"`
  257. }