search_shards.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. // SearchShardsService returns the indices and shards that a search request would be executed against.
  13. // See https://www.elastic.co/guide/en/elasticsearch/reference/6.0/search-shards.html
  14. type SearchShardsService struct {
  15. client *Client
  16. pretty bool
  17. index []string
  18. routing string
  19. local *bool
  20. preference string
  21. ignoreUnavailable *bool
  22. allowNoIndices *bool
  23. expandWildcards string
  24. }
  25. // NewSearchShardsService creates a new SearchShardsService.
  26. func NewSearchShardsService(client *Client) *SearchShardsService {
  27. return &SearchShardsService{
  28. client: client,
  29. }
  30. }
  31. // Index sets the names of the indices to restrict the results.
  32. func (s *SearchShardsService) Index(index ...string) *SearchShardsService {
  33. s.index = append(s.index, index...)
  34. return s
  35. }
  36. //A boolean value whether to read the cluster state locally in order to
  37. //determine where shards are allocated instead of using the Master node’s cluster state.
  38. func (s *SearchShardsService) Local(local bool) *SearchShardsService {
  39. s.local = &local
  40. return s
  41. }
  42. // Routing sets a specific routing value.
  43. func (s *SearchShardsService) Routing(routing string) *SearchShardsService {
  44. s.routing = routing
  45. return s
  46. }
  47. // Preference specifies the node or shard the operation should be performed on (default: random).
  48. func (s *SearchShardsService) Preference(preference string) *SearchShardsService {
  49. s.preference = preference
  50. return s
  51. }
  52. // Pretty indicates that the JSON response be indented and human readable.
  53. func (s *SearchShardsService) Pretty(pretty bool) *SearchShardsService {
  54. s.pretty = pretty
  55. return s
  56. }
  57. // IgnoreUnavailable indicates whether the specified concrete indices
  58. // should be ignored when unavailable (missing or closed).
  59. func (s *SearchShardsService) IgnoreUnavailable(ignoreUnavailable bool) *SearchShardsService {
  60. s.ignoreUnavailable = &ignoreUnavailable
  61. return s
  62. }
  63. // AllowNoIndices indicates whether to ignore if a wildcard indices
  64. // expression resolves into no concrete indices. (This includes `_all` string
  65. // or when no indices have been specified).
  66. func (s *SearchShardsService) AllowNoIndices(allowNoIndices bool) *SearchShardsService {
  67. s.allowNoIndices = &allowNoIndices
  68. return s
  69. }
  70. // ExpandWildcards indicates whether to expand wildcard expression to
  71. // concrete indices that are open, closed or both.
  72. func (s *SearchShardsService) ExpandWildcards(expandWildcards string) *SearchShardsService {
  73. s.expandWildcards = expandWildcards
  74. return s
  75. }
  76. // buildURL builds the URL for the operation.
  77. func (s *SearchShardsService) buildURL() (string, url.Values, error) {
  78. // Build URL
  79. path, err := uritemplates.Expand("/{index}/_search_shards", map[string]string{
  80. "index": strings.Join(s.index, ","),
  81. })
  82. if err != nil {
  83. return "", url.Values{}, err
  84. }
  85. // Add query string parameters
  86. params := url.Values{}
  87. if s.pretty {
  88. params.Set("pretty", "true")
  89. }
  90. if s.preference != "" {
  91. params.Set("preference", s.preference)
  92. }
  93. if s.local != nil {
  94. params.Set("local", fmt.Sprintf("%v", *s.local))
  95. }
  96. if s.routing != "" {
  97. params.Set("routing", s.routing)
  98. }
  99. if s.allowNoIndices != nil {
  100. params.Set("allow_no_indices", fmt.Sprintf("%v", *s.allowNoIndices))
  101. }
  102. if s.expandWildcards != "" {
  103. params.Set("expand_wildcards", s.expandWildcards)
  104. }
  105. if s.ignoreUnavailable != nil {
  106. params.Set("ignore_unavailable", fmt.Sprintf("%v", *s.ignoreUnavailable))
  107. }
  108. return path, params, nil
  109. }
  110. // Validate checks if the operation is valid.
  111. func (s *SearchShardsService) Validate() error {
  112. var invalid []string
  113. if len(s.index) < 1 {
  114. invalid = append(invalid, "Index")
  115. }
  116. if len(invalid) > 0 {
  117. return fmt.Errorf("missing required fields: %v", invalid)
  118. }
  119. return nil
  120. }
  121. // Do executes the operation.
  122. func (s *SearchShardsService) Do(ctx context.Context) (*SearchShardsResponse, error) {
  123. // Check pre-conditions
  124. if err := s.Validate(); err != nil {
  125. return nil, err
  126. }
  127. // Get URL for request
  128. path, params, err := s.buildURL()
  129. if err != nil {
  130. return nil, err
  131. }
  132. // Get HTTP response
  133. res, err := s.client.PerformRequest(ctx, "GET", path, params, nil)
  134. if err != nil {
  135. return nil, err
  136. }
  137. // Return operation response
  138. ret := new(SearchShardsResponse)
  139. if err := s.client.decoder.Decode(res.Body, ret); err != nil {
  140. return nil, err
  141. }
  142. return ret, nil
  143. }
  144. // SearchShardsResponse is the response of SearchShardsService.Do.
  145. type SearchShardsResponse struct {
  146. Nodes map[string]interface{} `json:"nodes"`
  147. Indices map[string]interface{} `json:"indices"`
  148. Shards [][]*SearchShardsResponseShardsInfo `json:"shards"`
  149. }
  150. type SearchShardsResponseShardsInfo struct {
  151. Index string `json:"index"`
  152. Node string `json:"node"`
  153. Primary bool `json:"primary"`
  154. Shard uint `json:"shard"`
  155. State string `json:"state"`
  156. AllocationId interface{} `json:"allocation_id"`
  157. RelocatingNode string `json:"relocating_node"`
  158. }