indices_get_field_mapping.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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/url"
  9. "strings"
  10. "gopkg.in/olivere/elastic.v5/uritemplates"
  11. )
  12. // IndicesGetFieldMappingService retrieves the mapping definitions for the fields in an index
  13. // or index/type.
  14. //
  15. // See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/indices-get-field-mapping.html
  16. // for details.
  17. type IndicesGetFieldMappingService struct {
  18. client *Client
  19. pretty bool
  20. index []string
  21. typ []string
  22. field []string
  23. local *bool
  24. ignoreUnavailable *bool
  25. allowNoIndices *bool
  26. expandWildcards string
  27. }
  28. // NewGetFieldMappingService is an alias for NewIndicesGetFieldMappingService.
  29. // Use NewIndicesGetFieldMappingService.
  30. func NewGetFieldMappingService(client *Client) *IndicesGetFieldMappingService {
  31. return NewIndicesGetFieldMappingService(client)
  32. }
  33. // NewIndicesGetFieldMappingService creates a new IndicesGetFieldMappingService.
  34. func NewIndicesGetFieldMappingService(client *Client) *IndicesGetFieldMappingService {
  35. return &IndicesGetFieldMappingService{
  36. client: client,
  37. }
  38. }
  39. // Index is a list of index names.
  40. func (s *IndicesGetFieldMappingService) Index(indices ...string) *IndicesGetFieldMappingService {
  41. s.index = append(s.index, indices...)
  42. return s
  43. }
  44. // Type is a list of document types.
  45. func (s *IndicesGetFieldMappingService) Type(types ...string) *IndicesGetFieldMappingService {
  46. s.typ = append(s.typ, types...)
  47. return s
  48. }
  49. // Field is a list of fields.
  50. func (s *IndicesGetFieldMappingService) Field(fields ...string) *IndicesGetFieldMappingService {
  51. s.field = append(s.field, fields...)
  52. return s
  53. }
  54. // AllowNoIndices indicates whether to ignore if a wildcard indices
  55. // expression resolves into no concrete indices.
  56. // This includes `_all` string or when no indices have been specified.
  57. func (s *IndicesGetFieldMappingService) AllowNoIndices(allowNoIndices bool) *IndicesGetFieldMappingService {
  58. s.allowNoIndices = &allowNoIndices
  59. return s
  60. }
  61. // ExpandWildcards indicates whether to expand wildcard expression to
  62. // concrete indices that are open, closed or both..
  63. func (s *IndicesGetFieldMappingService) ExpandWildcards(expandWildcards string) *IndicesGetFieldMappingService {
  64. s.expandWildcards = expandWildcards
  65. return s
  66. }
  67. // Local indicates whether to return local information, do not retrieve
  68. // the state from master node (default: false).
  69. func (s *IndicesGetFieldMappingService) Local(local bool) *IndicesGetFieldMappingService {
  70. s.local = &local
  71. return s
  72. }
  73. // IgnoreUnavailable indicates whether specified concrete indices should be
  74. // ignored when unavailable (missing or closed).
  75. func (s *IndicesGetFieldMappingService) IgnoreUnavailable(ignoreUnavailable bool) *IndicesGetFieldMappingService {
  76. s.ignoreUnavailable = &ignoreUnavailable
  77. return s
  78. }
  79. // Pretty indicates that the JSON response be indented and human readable.
  80. func (s *IndicesGetFieldMappingService) Pretty(pretty bool) *IndicesGetFieldMappingService {
  81. s.pretty = pretty
  82. return s
  83. }
  84. // buildURL builds the URL for the operation.
  85. func (s *IndicesGetFieldMappingService) buildURL() (string, url.Values, error) {
  86. var index, typ, field []string
  87. if len(s.index) > 0 {
  88. index = s.index
  89. } else {
  90. index = []string{"_all"}
  91. }
  92. if len(s.typ) > 0 {
  93. typ = s.typ
  94. } else {
  95. typ = []string{"_all"}
  96. }
  97. if len(s.field) > 0 {
  98. field = s.field
  99. } else {
  100. field = []string{"*"}
  101. }
  102. // Build URL
  103. path, err := uritemplates.Expand("/{index}/_mapping/{type}/field/{field}", map[string]string{
  104. "index": strings.Join(index, ","),
  105. "type": strings.Join(typ, ","),
  106. "field": strings.Join(field, ","),
  107. })
  108. if err != nil {
  109. return "", url.Values{}, err
  110. }
  111. // Add query string parameters
  112. params := url.Values{}
  113. if s.pretty {
  114. params.Set("pretty", "1")
  115. }
  116. if s.ignoreUnavailable != nil {
  117. params.Set("ignore_unavailable", fmt.Sprintf("%v", *s.ignoreUnavailable))
  118. }
  119. if s.allowNoIndices != nil {
  120. params.Set("allow_no_indices", fmt.Sprintf("%v", *s.allowNoIndices))
  121. }
  122. if s.expandWildcards != "" {
  123. params.Set("expand_wildcards", s.expandWildcards)
  124. }
  125. if s.local != nil {
  126. params.Set("local", fmt.Sprintf("%v", *s.local))
  127. }
  128. return path, params, nil
  129. }
  130. // Validate checks if the operation is valid.
  131. func (s *IndicesGetFieldMappingService) Validate() error {
  132. return nil
  133. }
  134. // Do executes the operation. It returns mapping definitions for an index
  135. // or index/type.
  136. func (s *IndicesGetFieldMappingService) Do(ctx context.Context) (map[string]interface{}, error) {
  137. var ret map[string]interface{}
  138. // Check pre-conditions
  139. if err := s.Validate(); err != nil {
  140. return nil, err
  141. }
  142. // Get URL for request
  143. path, params, err := s.buildURL()
  144. if err != nil {
  145. return nil, err
  146. }
  147. // Get HTTP response
  148. res, err := s.client.PerformRequest(ctx, "GET", path, params, nil)
  149. if err != nil {
  150. return nil, err
  151. }
  152. // Return operation response
  153. if err := s.client.decoder.Decode(res.Body, &ret); err != nil {
  154. return nil, err
  155. }
  156. return ret, nil
  157. }