You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

field_caps.go 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. "github.com/olivere/elastic/v7/uritemplates"
  12. )
  13. // FieldCapsService allows retrieving the capabilities of fields among multiple indices.
  14. //
  15. // See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-field-caps.html
  16. // for details
  17. type FieldCapsService struct {
  18. client *Client
  19. pretty *bool // pretty format the returned JSON response
  20. human *bool // return human readable values for statistics
  21. errorTrace *bool // include the stack trace of returned errors
  22. filterPath []string // list of filters used to reduce the response
  23. headers http.Header // custom request-level HTTP headers
  24. index []string
  25. allowNoIndices *bool
  26. expandWildcards string
  27. fields []string
  28. ignoreUnavailable *bool
  29. bodyJson interface{}
  30. bodyString string
  31. }
  32. // NewFieldCapsService creates a new FieldCapsService
  33. func NewFieldCapsService(client *Client) *FieldCapsService {
  34. return &FieldCapsService{
  35. client: client,
  36. }
  37. }
  38. // Pretty tells Elasticsearch whether to return a formatted JSON response.
  39. func (s *FieldCapsService) Pretty(pretty bool) *FieldCapsService {
  40. s.pretty = &pretty
  41. return s
  42. }
  43. // Human specifies whether human readable values should be returned in
  44. // the JSON response, e.g. "7.5mb".
  45. func (s *FieldCapsService) Human(human bool) *FieldCapsService {
  46. s.human = &human
  47. return s
  48. }
  49. // ErrorTrace specifies whether to include the stack trace of returned errors.
  50. func (s *FieldCapsService) ErrorTrace(errorTrace bool) *FieldCapsService {
  51. s.errorTrace = &errorTrace
  52. return s
  53. }
  54. // FilterPath specifies a list of filters used to reduce the response.
  55. func (s *FieldCapsService) FilterPath(filterPath ...string) *FieldCapsService {
  56. s.filterPath = filterPath
  57. return s
  58. }
  59. // Header adds a header to the request.
  60. func (s *FieldCapsService) Header(name string, value string) *FieldCapsService {
  61. if s.headers == nil {
  62. s.headers = http.Header{}
  63. }
  64. s.headers.Add(name, value)
  65. return s
  66. }
  67. // Headers specifies the headers of the request.
  68. func (s *FieldCapsService) Headers(headers http.Header) *FieldCapsService {
  69. s.headers = headers
  70. return s
  71. }
  72. // Index is a list of index names; use `_all` or empty string to perform
  73. // the operation on all indices.
  74. func (s *FieldCapsService) Index(index ...string) *FieldCapsService {
  75. s.index = append(s.index, index...)
  76. return s
  77. }
  78. // AllowNoIndices indicates whether to ignore if a wildcard indices expression
  79. // resolves into no concrete indices.
  80. // (This includes `_all` string or when no indices have been specified).
  81. func (s *FieldCapsService) AllowNoIndices(allowNoIndices bool) *FieldCapsService {
  82. s.allowNoIndices = &allowNoIndices
  83. return s
  84. }
  85. // ExpandWildcards indicates whether to expand wildcard expression to
  86. // concrete indices that are open, closed or both.
  87. func (s *FieldCapsService) ExpandWildcards(expandWildcards string) *FieldCapsService {
  88. s.expandWildcards = expandWildcards
  89. return s
  90. }
  91. // Fields is a list of fields for to get field capabilities.
  92. func (s *FieldCapsService) Fields(fields ...string) *FieldCapsService {
  93. s.fields = append(s.fields, fields...)
  94. return s
  95. }
  96. // IgnoreUnavailable is documented as: Whether specified concrete indices should be ignored when unavailable (missing or closed).
  97. func (s *FieldCapsService) IgnoreUnavailable(ignoreUnavailable bool) *FieldCapsService {
  98. s.ignoreUnavailable = &ignoreUnavailable
  99. return s
  100. }
  101. // 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.
  102. func (s *FieldCapsService) BodyJson(body interface{}) *FieldCapsService {
  103. s.bodyJson = body
  104. return s
  105. }
  106. // 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.
  107. func (s *FieldCapsService) BodyString(body string) *FieldCapsService {
  108. s.bodyString = body
  109. return s
  110. }
  111. // buildURL builds the URL for the operation.
  112. func (s *FieldCapsService) buildURL() (string, url.Values, error) {
  113. // Build URL
  114. var err error
  115. var path string
  116. if len(s.index) > 0 {
  117. path, err = uritemplates.Expand("/{index}/_field_caps", map[string]string{
  118. "index": strings.Join(s.index, ","),
  119. })
  120. } else {
  121. path = "/_field_caps"
  122. }
  123. if err != nil {
  124. return "", url.Values{}, err
  125. }
  126. // Add query string parameters
  127. params := url.Values{}
  128. if v := s.pretty; v != nil {
  129. params.Set("pretty", fmt.Sprint(*v))
  130. }
  131. if v := s.human; v != nil {
  132. params.Set("human", fmt.Sprint(*v))
  133. }
  134. if v := s.errorTrace; v != nil {
  135. params.Set("error_trace", fmt.Sprint(*v))
  136. }
  137. if len(s.filterPath) > 0 {
  138. params.Set("filter_path", strings.Join(s.filterPath, ","))
  139. }
  140. if s.allowNoIndices != nil {
  141. params.Set("allow_no_indices", fmt.Sprintf("%v", *s.allowNoIndices))
  142. }
  143. if s.expandWildcards != "" {
  144. params.Set("expand_wildcards", s.expandWildcards)
  145. }
  146. if len(s.fields) > 0 {
  147. params.Set("fields", strings.Join(s.fields, ","))
  148. }
  149. if s.ignoreUnavailable != nil {
  150. params.Set("ignore_unavailable", fmt.Sprintf("%v", *s.ignoreUnavailable))
  151. }
  152. return path, params, nil
  153. }
  154. // Validate checks if the operation is valid.
  155. func (s *FieldCapsService) Validate() error {
  156. return nil
  157. }
  158. // Do executes the operation.
  159. func (s *FieldCapsService) Do(ctx context.Context) (*FieldCapsResponse, error) {
  160. // Check pre-conditions
  161. if err := s.Validate(); err != nil {
  162. return nil, err
  163. }
  164. // Get URL for request
  165. path, params, err := s.buildURL()
  166. if err != nil {
  167. return nil, err
  168. }
  169. // Setup HTTP request body
  170. var body interface{}
  171. if s.bodyJson != nil {
  172. body = s.bodyJson
  173. } else {
  174. body = s.bodyString
  175. }
  176. // Get HTTP response
  177. res, err := s.client.PerformRequest(ctx, PerformRequestOptions{
  178. Method: "POST",
  179. Path: path,
  180. Params: params,
  181. Body: body,
  182. IgnoreErrors: []int{http.StatusNotFound},
  183. Headers: s.headers,
  184. })
  185. if err != nil {
  186. return nil, err
  187. }
  188. // TODO(oe): Is 404 really a valid response here?
  189. if res.StatusCode == http.StatusNotFound {
  190. return &FieldCapsResponse{}, nil
  191. }
  192. // Return operation response
  193. ret := new(FieldCapsResponse)
  194. if err := s.client.decoder.Decode(res.Body, ret); err != nil {
  195. return nil, err
  196. }
  197. return ret, nil
  198. }
  199. // -- Request --
  200. // FieldCapsRequest can be used to set up the body to be used in the
  201. // Field Capabilities API.
  202. type FieldCapsRequest struct {
  203. Fields []string `json:"fields"`
  204. }
  205. // -- Response --
  206. // FieldCapsResponse contains field capabilities.
  207. type FieldCapsResponse struct {
  208. Indices []string `json:"indices,omitempty"` // list of index names
  209. Fields map[string]FieldCapsType `json:"fields,omitempty"` // Name -> type -> caps
  210. }
  211. // FieldCapsType represents a mapping from type (e.g. keyword)
  212. // to capabilities.
  213. type FieldCapsType map[string]FieldCaps // type -> caps
  214. // FieldCaps contains capabilities of an individual field.
  215. type FieldCaps struct {
  216. Type string `json:"type"`
  217. Searchable bool `json:"searchable"`
  218. Aggregatable bool `json:"aggregatable"`
  219. Indices []string `json:"indices,omitempty"`
  220. NonSearchableIndices []string `json:"non_searchable_indices,omitempty"`
  221. NonAggregatableIndices []string `json:"non_aggregatable_indices,omitempty"`
  222. }