You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

indices_flush_synced.go 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. "encoding/json"
  8. "fmt"
  9. "net/http"
  10. "net/url"
  11. "strings"
  12. "github.com/olivere/elastic/v7/uritemplates"
  13. )
  14. // IndicesSyncedFlushService performs a normal flush, then adds a generated
  15. // unique marked (sync_id) to all shards.
  16. //
  17. // See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/indices-synced-flush.html
  18. // for details.
  19. type IndicesSyncedFlushService struct {
  20. client *Client
  21. pretty *bool // pretty format the returned JSON response
  22. human *bool // return human readable values for statistics
  23. errorTrace *bool // include the stack trace of returned errors
  24. filterPath []string // list of filters used to reduce the response
  25. headers http.Header // custom request-level HTTP headers
  26. index []string
  27. ignoreUnavailable *bool
  28. allowNoIndices *bool
  29. expandWildcards string
  30. }
  31. // NewIndicesSyncedFlushService creates a new IndicesSyncedFlushService.
  32. func NewIndicesSyncedFlushService(client *Client) *IndicesSyncedFlushService {
  33. return &IndicesSyncedFlushService{
  34. client: client,
  35. }
  36. }
  37. // Pretty tells Elasticsearch whether to return a formatted JSON response.
  38. func (s *IndicesSyncedFlushService) Pretty(pretty bool) *IndicesSyncedFlushService {
  39. s.pretty = &pretty
  40. return s
  41. }
  42. // Human specifies whether human readable values should be returned in
  43. // the JSON response, e.g. "7.5mb".
  44. func (s *IndicesSyncedFlushService) Human(human bool) *IndicesSyncedFlushService {
  45. s.human = &human
  46. return s
  47. }
  48. // ErrorTrace specifies whether to include the stack trace of returned errors.
  49. func (s *IndicesSyncedFlushService) ErrorTrace(errorTrace bool) *IndicesSyncedFlushService {
  50. s.errorTrace = &errorTrace
  51. return s
  52. }
  53. // FilterPath specifies a list of filters used to reduce the response.
  54. func (s *IndicesSyncedFlushService) FilterPath(filterPath ...string) *IndicesSyncedFlushService {
  55. s.filterPath = filterPath
  56. return s
  57. }
  58. // Header adds a header to the request.
  59. func (s *IndicesSyncedFlushService) Header(name string, value string) *IndicesSyncedFlushService {
  60. if s.headers == nil {
  61. s.headers = http.Header{}
  62. }
  63. s.headers.Add(name, value)
  64. return s
  65. }
  66. // Headers specifies the headers of the request.
  67. func (s *IndicesSyncedFlushService) Headers(headers http.Header) *IndicesSyncedFlushService {
  68. s.headers = headers
  69. return s
  70. }
  71. // Index is a list of index names; use `_all` or empty string for all indices.
  72. func (s *IndicesSyncedFlushService) Index(indices ...string) *IndicesSyncedFlushService {
  73. s.index = append(s.index, indices...)
  74. return s
  75. }
  76. // IgnoreUnavailable indicates whether specified concrete indices should be
  77. // ignored when unavailable (missing or closed).
  78. func (s *IndicesSyncedFlushService) IgnoreUnavailable(ignoreUnavailable bool) *IndicesSyncedFlushService {
  79. s.ignoreUnavailable = &ignoreUnavailable
  80. return s
  81. }
  82. // AllowNoIndices indicates whether to ignore if a wildcard indices expression
  83. // resolves into no concrete indices. (This includes `_all` string or when
  84. // no indices have been specified).
  85. func (s *IndicesSyncedFlushService) AllowNoIndices(allowNoIndices bool) *IndicesSyncedFlushService {
  86. s.allowNoIndices = &allowNoIndices
  87. return s
  88. }
  89. // ExpandWildcards specifies whether to expand wildcard expression to
  90. // concrete indices that are open, closed or both..
  91. func (s *IndicesSyncedFlushService) ExpandWildcards(expandWildcards string) *IndicesSyncedFlushService {
  92. s.expandWildcards = expandWildcards
  93. return s
  94. }
  95. // buildURL builds the URL for the operation.
  96. func (s *IndicesSyncedFlushService) buildURL() (string, url.Values, error) {
  97. // Build URL
  98. var err error
  99. var path string
  100. if len(s.index) > 0 {
  101. path, err = uritemplates.Expand("/{index}/_flush/synced", map[string]string{
  102. "index": strings.Join(s.index, ","),
  103. })
  104. } else {
  105. path = "/_flush/synced"
  106. }
  107. if err != nil {
  108. return "", url.Values{}, err
  109. }
  110. // Add query string parameters
  111. params := url.Values{}
  112. if v := s.pretty; v != nil {
  113. params.Set("pretty", fmt.Sprint(*v))
  114. }
  115. if v := s.human; v != nil {
  116. params.Set("human", fmt.Sprint(*v))
  117. }
  118. if v := s.errorTrace; v != nil {
  119. params.Set("error_trace", fmt.Sprint(*v))
  120. }
  121. if len(s.filterPath) > 0 {
  122. params.Set("filter_path", strings.Join(s.filterPath, ","))
  123. }
  124. if s.ignoreUnavailable != nil {
  125. params.Set("ignore_unavailable", fmt.Sprintf("%v", *s.ignoreUnavailable))
  126. }
  127. if s.allowNoIndices != nil {
  128. params.Set("allow_no_indices", fmt.Sprintf("%v", *s.allowNoIndices))
  129. }
  130. if s.expandWildcards != "" {
  131. params.Set("expand_wildcards", s.expandWildcards)
  132. }
  133. return path, params, nil
  134. }
  135. // Validate checks if the operation is valid.
  136. func (s *IndicesSyncedFlushService) Validate() error {
  137. return nil
  138. }
  139. // Do executes the service.
  140. func (s *IndicesSyncedFlushService) Do(ctx context.Context) (*IndicesSyncedFlushResponse, error) {
  141. // Check pre-conditions
  142. if err := s.Validate(); err != nil {
  143. return nil, err
  144. }
  145. // Get URL for request
  146. path, params, err := s.buildURL()
  147. if err != nil {
  148. return nil, err
  149. }
  150. // Get HTTP response
  151. res, err := s.client.PerformRequest(ctx, PerformRequestOptions{
  152. Method: "POST",
  153. Path: path,
  154. Params: params,
  155. Headers: s.headers,
  156. })
  157. if err != nil {
  158. return nil, err
  159. }
  160. // Return operation response
  161. ret := new(IndicesSyncedFlushResponse)
  162. if err := s.client.decoder.Decode(res.Body, ret); err != nil {
  163. return nil, err
  164. }
  165. return ret, nil
  166. }
  167. // -- Result of a flush request.
  168. // IndicesSyncedFlushResponse is the outcome of a synched flush call.
  169. type IndicesSyncedFlushResponse struct {
  170. Shards *ShardsInfo `json:"_shards"`
  171. Index map[string]*IndicesShardsSyncedFlushResult `json:"-"`
  172. // TODO Add information about the indices here from the root level
  173. // It looks like this:
  174. // {
  175. // "_shards" : {
  176. // "total" : 4,
  177. // "successful" : 4,
  178. // "failed" : 0
  179. // },
  180. // "elastic-test" : {
  181. // "total" : 1,
  182. // "successful" : 1,
  183. // "failed" : 0
  184. // },
  185. // "elastic-test2" : {
  186. // "total" : 1,
  187. // "successful" : 1,
  188. // "failed" : 0
  189. // },
  190. // "elastic-orders" : {
  191. // "total" : 1,
  192. // "successful" : 1,
  193. // "failed" : 0
  194. // },
  195. // "elastic-nosource-test" : {
  196. // "total" : 1,
  197. // "successful" : 1,
  198. // "failed" : 0
  199. // }
  200. // }
  201. }
  202. // IndicesShardsSyncedFlushResult represents synced flush information about
  203. // a specific index.
  204. type IndicesShardsSyncedFlushResult struct {
  205. Total int `json:"total"`
  206. Successful int `json:"successful"`
  207. Failed int `json:"failed"`
  208. Failures []IndicesShardsSyncedFlushResultFailure `json:"failures,omitempty"`
  209. }
  210. // IndicesShardsSyncedFlushResultFailure represents a failure of a synced
  211. // flush operation.
  212. type IndicesShardsSyncedFlushResultFailure struct {
  213. Shard int `json:"shard"`
  214. Reason string `json:"reason"`
  215. Routing struct {
  216. State string `json:"state"`
  217. Primary bool `json:"primary"`
  218. Node string `json:"node"`
  219. RelocatingNode *string `json:"relocating_node"`
  220. Shard int `json:"shard"`
  221. Index string `json:"index"`
  222. ExpectedShardSizeInBytes int64 `json:"expected_shard_size_in_bytes,omitempty"`
  223. // recoverySource
  224. // allocationId
  225. // unassignedInfo
  226. } `json:"routing"`
  227. }
  228. // UnmarshalJSON parses the output from Synced Flush API.
  229. func (resp *IndicesSyncedFlushResponse) UnmarshalJSON(data []byte) error {
  230. m := make(map[string]json.RawMessage)
  231. err := json.Unmarshal(data, &m)
  232. if err != nil {
  233. return err
  234. }
  235. resp.Index = make(map[string]*IndicesShardsSyncedFlushResult)
  236. for k, v := range m {
  237. if k == "_shards" {
  238. if err := json.Unmarshal(v, &resp.Shards); err != nil {
  239. return err
  240. }
  241. } else {
  242. ix := new(IndicesShardsSyncedFlushResult)
  243. if err := json.Unmarshal(v, &ix); err != nil {
  244. return err
  245. }
  246. resp.Index[k] = ix
  247. }
  248. }
  249. return nil
  250. }