From 22f0f3b3be0201a65afd08789597da1f49fb4247 Mon Sep 17 00:00:00 2001 From: Paul Bonser Date: Thu, 2 Feb 2017 18:04:24 -0600 Subject: [PATCH] Add Client.DeleteByQuery rather than requiring users to pass 'DELETE' to Query --- goes.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/goes.go b/goes.go index 803ef52..d11e1fb 100644 --- a/goes.go +++ b/goes.go @@ -286,6 +286,34 @@ func (c *Client) Query(query interface{}, indexList []string, typeList []string, return c.Do(&r) } +// DeleteByQuery deletes documents matching the specified query. It will return an error for ES 2.x, +// because delete by query support was removed in those versions. +func (c *Client) DeleteByQuery(query interface{}, indexList []string, typeList []string, extraArgs url.Values) (*Response, error) { + version, err := c.Version() + if err != nil { + return nil, err + } + if version > "2" && version < "5" { + return nil, errors.New("ElasticSearch 2.x does not support delete by query") + } + + r := Request{ + Query: query, + IndexList: indexList, + TypeList: typeList, + Method: "DELETE", + API: "_query", + ExtraArgs: extraArgs, + } + + if version > "5" { + r.API = "_delete_by_query" + r.Method = "POST" + } + + return c.Do(&r) +} + // Scan starts scroll over an index. // For ES versions < 5.x, it uses search_type=scan; for 5.x it uses sort=_doc. This means that data // will be returned in the initial response for 5.x versions, but not for older versions. Code