count: support for count requests

Added count method to connection
Added Count field to response
Added test for count method
This commit is contained in:
Stephen Asbury 2015-01-26 17:43:50 -08:00
parent cc93e7ac2c
commit 21ab94d0ca
3 changed files with 65 additions and 3 deletions

19
goes.go
View File

@ -210,6 +210,21 @@ func (c *Connection) Search(query map[string]interface{}, indexList []string, ty
return r.Run()
}
// Count executes a count query against an index, use the Count field in the response for the result
func (c *Connection) Count(query map[string]interface{}, indexList []string, typeList []string, extraArgs url.Values) (Response, error) {
r := Request{
Conn: c,
Query: query,
IndexList: indexList,
TypeList: typeList,
method: "POST",
api: "_count",
ExtraArgs: extraArgs,
}
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.
@ -494,9 +509,9 @@ func (c *Connection) Update(d Document, query map[string]interface{}, extraArgs
api: "_update",
}
if d.Id != nil {
if d.Id != nil {
r.id = d.Id.(string)
}
}
return r.Run()
}

View File

@ -201,7 +201,7 @@ func (s *GoesTestSuite) TestOptimize(c *C) {
// we must wait for a bit otherwise ES crashes
time.Sleep(1 * time.Second)
response, err := conn.Optimize([]string{indexName}, url.Values{"max_num_segments" : []string{"1"}})
response, err := conn.Optimize([]string{indexName}, url.Values{"max_num_segments": []string{"1"}})
c.Assert(err, IsNil)
c.Assert(response.All.Indices[indexName].Primaries["docs"].Count, Equals, 0)
@ -722,6 +722,52 @@ func (s *GoesTestSuite) TestSearch(c *C) {
c.Assert(response.Hits, DeepEquals, expectedHits)
}
func (s *GoesTestSuite) TestCount(c *C) {
indexName := "testcount"
docType := "tweet"
docId := "1234"
source := map[string]interface{}{
"user": "foo",
"message": "bar",
}
conn := NewConnection(ES_HOST, ES_PORT)
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: source,
}
_, err = conn.Index(d, url.Values{})
c.Assert(err, IsNil)
_, err = conn.RefreshIndex(indexName)
c.Assert(err, IsNil)
// I can feel my eyes bleeding
query := map[string]interface{}{
"query": map[string]interface{}{
"bool": map[string]interface{}{
"must": []map[string]interface{}{
{
"match_all": map[string]interface{}{},
},
},
},
},
}
response, err := conn.Count(query, []string{indexName}, []string{docType}, url.Values{})
c.Assert(response.Count, Equals, 1)
}
func (s *GoesTestSuite) TestIndexStatus(c *C) {
indexName := "testindexstatus"
conn := NewConnection(ES_HOST, ES_PORT)

View File

@ -70,6 +70,7 @@ type Response struct {
Type string `json:"_type"`
Version int `json:"_version"`
Found bool
Count int
// Used by the _stats API
All All `json:"_all"`