get_mapping.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. "log"
  9. "net/url"
  10. "strings"
  11. "github.com/olivere/elastic/uritemplates"
  12. )
  13. var (
  14. _ = fmt.Print
  15. _ = log.Print
  16. _ = strings.Index
  17. _ = uritemplates.Expand
  18. _ = url.Parse
  19. )
  20. // GetMappingService retrieves the mapping definitions for an index or
  21. // index/type. See at http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-get-mapping.html.
  22. type GetMappingService struct {
  23. client *Client
  24. pretty bool
  25. index []string
  26. typ []string
  27. local *bool
  28. ignoreUnavailable *bool
  29. allowNoIndices *bool
  30. expandWildcards string
  31. }
  32. // NewGetMappingService creates a new GetMappingService.
  33. func NewGetMappingService(client *Client) *GetMappingService {
  34. return &GetMappingService{
  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 *GetMappingService) Index(index ...string) *GetMappingService {
  42. s.index = append(s.index, index...)
  43. return s
  44. }
  45. // Type is a list of document types.
  46. func (s *GetMappingService) Type(typ ...string) *GetMappingService {
  47. s.typ = append(s.typ, typ...)
  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 *GetMappingService) AllowNoIndices(allowNoIndices bool) *GetMappingService {
  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 *GetMappingService) ExpandWildcards(expandWildcards string) *GetMappingService {
  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 *GetMappingService) Local(local bool) *GetMappingService {
  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 *GetMappingService) IgnoreUnavailable(ignoreUnavailable bool) *GetMappingService {
  72. s.ignoreUnavailable = &ignoreUnavailable
  73. return s
  74. }
  75. // Pretty indicates that the JSON response be indented and human readable.
  76. func (s *GetMappingService) Pretty(pretty bool) *GetMappingService {
  77. s.pretty = pretty
  78. return s
  79. }
  80. // buildURL builds the URL for the operation.
  81. func (s *GetMappingService) 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 *GetMappingService) Validate() error {
  122. return nil
  123. }
  124. // Do executes the operation. When successful, it returns a json.RawMessage.
  125. // If you specify an index, Elasticsearch returns HTTP status 404.
  126. // if you specify a type that does not exist, Elasticsearch returns
  127. // an empty map.
  128. func (s *GetMappingService) Do() (map[string]interface{}, error) {
  129. // Check pre-conditions
  130. if err := s.Validate(); err != nil {
  131. return nil, err
  132. }
  133. // Get URL for request
  134. path, params, err := s.buildURL()
  135. if err != nil {
  136. return nil, err
  137. }
  138. // Get HTTP response
  139. res, err := s.client.PerformRequest("GET", path, params, nil)
  140. if err != nil {
  141. return nil, err
  142. }
  143. // Return operation response
  144. var ret map[string]interface{}
  145. if err := json.Unmarshal(res.Body, &ret); err != nil {
  146. return nil, err
  147. }
  148. return ret, nil
  149. }