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:
parent
7e772ee99b
commit
5a12eb4b68
17
goes.go
17
goes.go
@ -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{}
|
||||||
|
64
goes_test.go
64
goes_test.go
@ -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"
|
||||||
|
Loading…
Reference in New Issue
Block a user