Support for running a _query with an http method

This allows operations like delete by query.
Included test for delete by query.
This commit is contained in:
Stephen Asbury 2014-09-11 13:49:57 -07:00
parent 7e772ee99b
commit 5a12eb4b68
2 changed files with 81 additions and 0 deletions

17
goes.go
View File

@ -196,6 +196,23 @@ func (c *Connection) Search(query map[string]interface{}, indexList []string, ty
return r.Run() 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 // Scan starts scroll over an index
func (c *Connection) Scan(query map[string]interface{}, indexList []string, typeList []string, timeout string, size int) (Response, error) { func (c *Connection) Scan(query map[string]interface{}, indexList []string, typeList []string, timeout string, size int) (Response, error) {
v := url.Values{} v := url.Values{}

View File

@ -508,6 +508,70 @@ func (s *GoesTestSuite) TestDelete(c *C) {
c.Assert(response, DeepEquals, expectedResponse) 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) { func (s *GoesTestSuite) TestGet(c *C) {
indexName := "testget" indexName := "testget"
docType := "tweet" docType := "tweet"