field_stats.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. // Copyright 2012-present 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. "context"
  7. "fmt"
  8. "net/http"
  9. "net/url"
  10. "strings"
  11. "gopkg.in/olivere/elastic.v5/uritemplates"
  12. )
  13. const (
  14. FieldStatsClusterLevel = "cluster"
  15. FieldStatsIndicesLevel = "indices"
  16. )
  17. // FieldStatsService allows finding statistical properties of a field without executing a search,
  18. // but looking up measurements that are natively available in the Lucene index.
  19. //
  20. // See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-field-stats.html
  21. // for details
  22. type FieldStatsService struct {
  23. client *Client
  24. pretty bool
  25. level string
  26. index []string
  27. allowNoIndices *bool
  28. expandWildcards string
  29. fields []string
  30. ignoreUnavailable *bool
  31. bodyJson interface{}
  32. bodyString string
  33. }
  34. // NewFieldStatsService creates a new FieldStatsService
  35. func NewFieldStatsService(client *Client) *FieldStatsService {
  36. return &FieldStatsService{
  37. client: client,
  38. index: make([]string, 0),
  39. fields: make([]string, 0),
  40. }
  41. }
  42. // Index is a list of index names; use `_all` or empty string to perform
  43. // the operation on all indices.
  44. func (s *FieldStatsService) Index(index ...string) *FieldStatsService {
  45. s.index = append(s.index, index...)
  46. return s
  47. }
  48. // AllowNoIndices indicates whether to ignore if a wildcard indices expression
  49. // resolves into no concrete indices.
  50. // (This includes `_all` string or when no indices have been specified).
  51. func (s *FieldStatsService) AllowNoIndices(allowNoIndices bool) *FieldStatsService {
  52. s.allowNoIndices = &allowNoIndices
  53. return s
  54. }
  55. // ExpandWildcards indicates whether to expand wildcard expression to
  56. // concrete indices that are open, closed or both.
  57. func (s *FieldStatsService) ExpandWildcards(expandWildcards string) *FieldStatsService {
  58. s.expandWildcards = expandWildcards
  59. return s
  60. }
  61. // Fields is a list of fields for to get field statistics
  62. // for (min value, max value, and more).
  63. func (s *FieldStatsService) Fields(fields ...string) *FieldStatsService {
  64. s.fields = append(s.fields, fields...)
  65. return s
  66. }
  67. // IgnoreUnavailable is documented as: Whether specified concrete indices should be ignored when unavailable (missing or closed).
  68. func (s *FieldStatsService) IgnoreUnavailable(ignoreUnavailable bool) *FieldStatsService {
  69. s.ignoreUnavailable = &ignoreUnavailable
  70. return s
  71. }
  72. // Level sets if stats should be returned on a per index level or on a cluster wide level;
  73. // should be one of 'cluster' or 'indices'; defaults to former
  74. func (s *FieldStatsService) Level(level string) *FieldStatsService {
  75. s.level = level
  76. return s
  77. }
  78. // ClusterLevel is a helper that sets Level to "cluster".
  79. func (s *FieldStatsService) ClusterLevel() *FieldStatsService {
  80. s.level = FieldStatsClusterLevel
  81. return s
  82. }
  83. // IndicesLevel is a helper that sets Level to "indices".
  84. func (s *FieldStatsService) IndicesLevel() *FieldStatsService {
  85. s.level = FieldStatsIndicesLevel
  86. return s
  87. }
  88. // Pretty indicates that the JSON response be indented and human readable.
  89. func (s *FieldStatsService) Pretty(pretty bool) *FieldStatsService {
  90. s.pretty = pretty
  91. return s
  92. }
  93. // BodyJson is documented as: Field json objects containing the name and optionally a range to filter out indices result, that have results outside the defined bounds.
  94. func (s *FieldStatsService) BodyJson(body interface{}) *FieldStatsService {
  95. s.bodyJson = body
  96. return s
  97. }
  98. // BodyString is documented as: Field json objects containing the name and optionally a range to filter out indices result, that have results outside the defined bounds.
  99. func (s *FieldStatsService) BodyString(body string) *FieldStatsService {
  100. s.bodyString = body
  101. return s
  102. }
  103. // buildURL builds the URL for the operation.
  104. func (s *FieldStatsService) buildURL() (string, url.Values, error) {
  105. // Build URL
  106. var err error
  107. var path string
  108. if len(s.index) > 0 {
  109. path, err = uritemplates.Expand("/{index}/_field_stats", map[string]string{
  110. "index": strings.Join(s.index, ","),
  111. })
  112. } else {
  113. path = "/_field_stats"
  114. }
  115. if err != nil {
  116. return "", url.Values{}, err
  117. }
  118. // Add query string parameters
  119. params := url.Values{}
  120. if s.allowNoIndices != nil {
  121. params.Set("allow_no_indices", fmt.Sprintf("%v", *s.allowNoIndices))
  122. }
  123. if s.expandWildcards != "" {
  124. params.Set("expand_wildcards", s.expandWildcards)
  125. }
  126. if len(s.fields) > 0 {
  127. params.Set("fields", strings.Join(s.fields, ","))
  128. }
  129. if s.ignoreUnavailable != nil {
  130. params.Set("ignore_unavailable", fmt.Sprintf("%v", *s.ignoreUnavailable))
  131. }
  132. if s.level != "" {
  133. params.Set("level", s.level)
  134. }
  135. return path, params, nil
  136. }
  137. // Validate checks if the operation is valid.
  138. func (s *FieldStatsService) Validate() error {
  139. var invalid []string
  140. if s.level != "" && (s.level != FieldStatsIndicesLevel && s.level != FieldStatsClusterLevel) {
  141. invalid = append(invalid, "Level")
  142. }
  143. if len(invalid) != 0 {
  144. return fmt.Errorf("missing or invalid required fields: %v", invalid)
  145. }
  146. return nil
  147. }
  148. // Do executes the operation.
  149. func (s *FieldStatsService) Do(ctx context.Context) (*FieldStatsResponse, error) {
  150. // Check pre-conditions
  151. if err := s.Validate(); err != nil {
  152. return nil, err
  153. }
  154. // Get URL for request
  155. path, params, err := s.buildURL()
  156. if err != nil {
  157. return nil, err
  158. }
  159. // Setup HTTP request body
  160. var body interface{}
  161. if s.bodyJson != nil {
  162. body = s.bodyJson
  163. } else {
  164. body = s.bodyString
  165. }
  166. // Get HTTP response
  167. res, err := s.client.PerformRequest(ctx, "POST", path, params, body, http.StatusNotFound)
  168. if err != nil {
  169. return nil, err
  170. }
  171. // TODO(oe): Is 404 really a valid response here?
  172. if res.StatusCode == http.StatusNotFound {
  173. return &FieldStatsResponse{make(map[string]IndexFieldStats)}, nil
  174. }
  175. // Return operation response
  176. ret := new(FieldStatsResponse)
  177. if err := s.client.decoder.Decode(res.Body, ret); err != nil {
  178. return nil, err
  179. }
  180. return ret, nil
  181. }
  182. // -- Request --
  183. // FieldStatsRequest can be used to set up the body to be used in the
  184. // Field Stats API.
  185. type FieldStatsRequest struct {
  186. Fields []string `json:"fields"`
  187. IndexConstraints map[string]*FieldStatsConstraints `json:"index_constraints,omitempty"`
  188. }
  189. // FieldStatsConstraints is a constraint on a field.
  190. type FieldStatsConstraints struct {
  191. Min *FieldStatsComparison `json:"min_value,omitempty"`
  192. Max *FieldStatsComparison `json:"max_value,omitempty"`
  193. }
  194. // FieldStatsComparison contain all comparison operations that can be used
  195. // in FieldStatsConstraints.
  196. type FieldStatsComparison struct {
  197. Lte interface{} `json:"lte,omitempty"`
  198. Lt interface{} `json:"lt,omitempty"`
  199. Gte interface{} `json:"gte,omitempty"`
  200. Gt interface{} `json:"gt,omitempty"`
  201. }
  202. // -- Response --
  203. // FieldStatsResponse is the response body content
  204. type FieldStatsResponse struct {
  205. Indices map[string]IndexFieldStats `json:"indices,omitempty"`
  206. }
  207. // IndexFieldStats contains field stats for an index
  208. type IndexFieldStats struct {
  209. Fields map[string]FieldStats `json:"fields,omitempty"`
  210. }
  211. // FieldStats contains stats of an individual field
  212. type FieldStats struct {
  213. Type string `json:"type"`
  214. MaxDoc int64 `json:"max_doc"`
  215. DocCount int64 `json:"doc_count"`
  216. Density int64 `json:"density"`
  217. SumDocFrequeny int64 `json:"sum_doc_freq"`
  218. SumTotalTermFrequency int64 `json:"sum_total_term_freq"`
  219. Searchable bool `json:"searchable"`
  220. Aggregatable bool `json:"aggregatable"`
  221. MinValue interface{} `json:"min_value"`
  222. MinValueAsString string `json:"min_value_as_string"`
  223. MaxValue interface{} `json:"max_value"`
  224. MaxValueAsString string `json:"max_value_as_string"`
  225. }