Merge pull request #15 from sasbury/master

Support for running a _query with an http method
This commit is contained in:
Jérôme Renard 2014-09-14 20:05:28 +02:00
commit 6fad26987a
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()
}
//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{}

View File

@ -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"