Debian Codegraph @

search_shards.go

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