count.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // Copyright 2012-2015 Oliver Eilhard. All rights reserved.
  2. // Use of this source code is governed by a MIT-license.
  3. // See http://olivere.mit-license.org/license.txt for details.
  4. package elastic
  5. import (
  6. "encoding/json"
  7. "fmt"
  8. "net/url"
  9. "strings"
  10. "github.com/olivere/elastic/uritemplates"
  11. )
  12. // CountService is a convenient service for determining the
  13. // number of documents in an index. Use SearchService with
  14. // a SearchType of count for counting with queries etc.
  15. type CountService struct {
  16. client *Client
  17. indices []string
  18. types []string
  19. query Query
  20. pretty bool
  21. }
  22. // CountResult is the result returned from using the Count API
  23. // (http://www.elasticsearch.org/guide/reference/api/count/)
  24. type CountResult struct {
  25. Count int64 `json:"count"`
  26. Shards shardsInfo `json:"_shards,omitempty"`
  27. }
  28. func NewCountService(client *Client) *CountService {
  29. builder := &CountService{
  30. client: client,
  31. }
  32. return builder
  33. }
  34. func (s *CountService) Index(index string) *CountService {
  35. if s.indices == nil {
  36. s.indices = make([]string, 0)
  37. }
  38. s.indices = append(s.indices, index)
  39. return s
  40. }
  41. func (s *CountService) Indices(indices ...string) *CountService {
  42. if s.indices == nil {
  43. s.indices = make([]string, 0)
  44. }
  45. s.indices = append(s.indices, indices...)
  46. return s
  47. }
  48. func (s *CountService) Type(typ string) *CountService {
  49. if s.types == nil {
  50. s.types = make([]string, 0)
  51. }
  52. s.types = append(s.types, typ)
  53. return s
  54. }
  55. func (s *CountService) Types(types ...string) *CountService {
  56. if s.types == nil {
  57. s.types = make([]string, 0)
  58. }
  59. s.types = append(s.types, types...)
  60. return s
  61. }
  62. func (s *CountService) Query(query Query) *CountService {
  63. s.query = query
  64. return s
  65. }
  66. func (s *CountService) Pretty(pretty bool) *CountService {
  67. s.pretty = pretty
  68. return s
  69. }
  70. func (s *CountService) Do() (int64, error) {
  71. var err error
  72. // Build url
  73. path := "/"
  74. // Indices part
  75. indexPart := make([]string, 0)
  76. for _, index := range s.indices {
  77. index, err = uritemplates.Expand("{index}", map[string]string{
  78. "index": index,
  79. })
  80. if err != nil {
  81. return 0, err
  82. }
  83. indexPart = append(indexPart, index)
  84. }
  85. if len(indexPart) > 0 {
  86. path += strings.Join(indexPart, ",")
  87. }
  88. // Types part
  89. typesPart := make([]string, 0)
  90. for _, typ := range s.types {
  91. typ, err = uritemplates.Expand("{type}", map[string]string{
  92. "type": typ,
  93. })
  94. if err != nil {
  95. return 0, err
  96. }
  97. typesPart = append(typesPart, typ)
  98. }
  99. if len(typesPart) > 0 {
  100. path += "/" + strings.Join(typesPart, ",")
  101. }
  102. // Search
  103. path += "/_count"
  104. // Parameters
  105. params := make(url.Values)
  106. if s.pretty {
  107. params.Set("pretty", fmt.Sprintf("%v", s.pretty))
  108. }
  109. // Set body if there is a query specified
  110. var body interface{}
  111. if s.query != nil {
  112. query := make(map[string]interface{})
  113. query["query"] = s.query.Source()
  114. body = query
  115. }
  116. // Get response
  117. res, err := s.client.PerformRequest("POST", path, params, body)
  118. if err != nil {
  119. return 0, err
  120. }
  121. // Return result
  122. ret := new(CountResult)
  123. if err := json.Unmarshal(res.Body, ret); err != nil {
  124. return 0, err
  125. }
  126. if ret != nil {
  127. return ret.Count, nil
  128. }
  129. return int64(0), nil
  130. }