indices_get_mapping.go 4.6 KB

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