diff --git a/goes.go b/goes.go index da86b56..7f4358b 100644 --- a/goes.go +++ b/goes.go @@ -196,6 +196,23 @@ func (c *Connection) Search(query map[string]interface{}, indexList []string, ty return r.Run() } +//Query runs a query against an index using the provided http method. +//This method can be used to execute a delete by query, just pass in "DELETE" +//for the HTTP method. +func (c *Connection) Query(query map[string]interface{}, indexList []string, typeList []string, httpMethod string, extraArgs url.Values) (Response, error) { + r := Request{ + Conn: c, + Query: query, + IndexList: indexList, + TypeList: typeList, + method: httpMethod, + api: "_query", + ExtraArgs: extraArgs, + } + + return r.Run() +} + // Scan starts scroll over an index func (c *Connection) Scan(query map[string]interface{}, indexList []string, typeList []string, timeout string, size int) (Response, error) { v := url.Values{} diff --git a/goes_test.go b/goes_test.go index 5f4f9e9..de71d73 100644 --- a/goes_test.go +++ b/goes_test.go @@ -508,6 +508,70 @@ func (s *GoesTestSuite) TestDelete(c *C) { c.Assert(response, DeepEquals, expectedResponse) } +func (s *GoesTestSuite) TestDeleteByQuery(c *C) { + indexName := "testdeletebyquery" + docType := "tweet" + docId := "1234" + + conn := NewConnection(ES_HOST, ES_PORT) + // just in case + conn.DeleteIndex(indexName) + + _, err := conn.CreateIndex(indexName, map[string]interface{}{}) + c.Assert(err, IsNil) + defer conn.DeleteIndex(indexName) + + d := Document{ + Index: indexName, + Type: docType, + Id: docId, + Fields: map[string]interface{}{ + "user": "foo", + }, + } + + _, err = conn.Index(d, url.Values{}) + c.Assert(err, IsNil) + + _, err = conn.RefreshIndex(indexName) + c.Assert(err, IsNil) + + query := map[string]interface{}{ + "query": map[string]interface{}{ + "bool": map[string]interface{}{ + "must": []map[string]interface{}{ + { + "match_all": map[string]interface{}{}, + }, + }, + }, + }, + } + + //should be 1 doc before delete by query + response, err := conn.Search(query, []string{indexName}, []string{docType}, url.Values{}) + c.Assert(err, IsNil) + c.Assert(response.Hits.Total, Equals, uint64(1)) + + response, err = conn.Query(query, []string{indexName}, []string{docType}, "DELETE", url.Values{}) + + c.Assert(err, IsNil) + + expectedResponse := Response{ + Found: false, + Index: "", + Type: "", + Id: "", + Version: 0, + } + c.Assert(response, DeepEquals, expectedResponse) + + //should be 0 docs after delete by query + response, err = conn.Search(query, []string{indexName}, []string{docType}, url.Values{}) + c.Assert(err, IsNil) + c.Assert(response.Hits.Total, Equals, uint64(0)) +} + func (s *GoesTestSuite) TestGet(c *C) { indexName := "testget" docType := "tweet"