indices_segments.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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/url"
  10. "strings"
  11. "gopkg.in/olivere/elastic.v5/uritemplates"
  12. )
  13. // IndicesSegmentsService provides low level segments information that a
  14. // Lucene index (shard level) is built with. Allows to be used to provide
  15. // more information on the state of a shard and an index, possibly
  16. // optimization information, data "wasted" on deletes, and so on.
  17. //
  18. // Find further documentation at
  19. // https://www.elastic.co/guide/en/elasticsearch/reference/5.6/indices-segments.html.
  20. type IndicesSegmentsService struct {
  21. client *Client
  22. pretty bool
  23. index []string
  24. allowNoIndices *bool
  25. expandWildcards string
  26. ignoreUnavailable *bool
  27. human *bool
  28. operationThreading interface{}
  29. verbose *bool
  30. }
  31. // NewIndicesSegmentsService creates a new IndicesSegmentsService.
  32. func NewIndicesSegmentsService(client *Client) *IndicesSegmentsService {
  33. return &IndicesSegmentsService{
  34. client: client,
  35. }
  36. }
  37. // Index is a comma-separated list of index names; use `_all` or empty string
  38. // to perform the operation on all indices.
  39. func (s *IndicesSegmentsService) Index(indices ...string) *IndicesSegmentsService {
  40. s.index = append(s.index, indices...)
  41. return s
  42. }
  43. // AllowNoIndices indicates whether to ignore if a wildcard indices expression
  44. // resolves into no concrete indices. (This includes `_all` string or when
  45. // no indices have been specified).
  46. func (s *IndicesSegmentsService) AllowNoIndices(allowNoIndices bool) *IndicesSegmentsService {
  47. s.allowNoIndices = &allowNoIndices
  48. return s
  49. }
  50. // ExpandWildcards indicates whether to expand wildcard expression to concrete indices
  51. // that are open, closed or both.
  52. func (s *IndicesSegmentsService) ExpandWildcards(expandWildcards string) *IndicesSegmentsService {
  53. s.expandWildcards = expandWildcards
  54. return s
  55. }
  56. // IgnoreUnavailable indicates whether specified concrete indices should be
  57. // ignored when unavailable (missing or closed).
  58. func (s *IndicesSegmentsService) IgnoreUnavailable(ignoreUnavailable bool) *IndicesSegmentsService {
  59. s.ignoreUnavailable = &ignoreUnavailable
  60. return s
  61. }
  62. // Human, when set to true, returns time and byte-values in human-readable format.
  63. func (s *IndicesSegmentsService) Human(human bool) *IndicesSegmentsService {
  64. s.human = &human
  65. return s
  66. }
  67. // OperationThreading is undocumented in Elasticsearch as of now.
  68. func (s *IndicesSegmentsService) OperationThreading(operationThreading interface{}) *IndicesSegmentsService {
  69. s.operationThreading = operationThreading
  70. return s
  71. }
  72. // Verbose, when set to true, includes detailed memory usage by Lucene.
  73. func (s *IndicesSegmentsService) Verbose(verbose bool) *IndicesSegmentsService {
  74. s.verbose = &verbose
  75. return s
  76. }
  77. // Pretty indicates that the JSON response be indented and human readable.
  78. func (s *IndicesSegmentsService) Pretty(pretty bool) *IndicesSegmentsService {
  79. s.pretty = pretty
  80. return s
  81. }
  82. // buildURL builds the URL for the operation.
  83. func (s *IndicesSegmentsService) buildURL() (string, url.Values, error) {
  84. var err error
  85. var path string
  86. if len(s.index) > 0 {
  87. path, err = uritemplates.Expand("/{index}/_segments", map[string]string{
  88. "index": strings.Join(s.index, ","),
  89. })
  90. } else {
  91. path = "/_segments"
  92. }
  93. if err != nil {
  94. return "", url.Values{}, err
  95. }
  96. // Add query string parameters
  97. params := url.Values{}
  98. if s.pretty {
  99. params.Set("pretty", "true")
  100. }
  101. if s.allowNoIndices != nil {
  102. params.Set("allow_no_indices", fmt.Sprintf("%v", *s.allowNoIndices))
  103. }
  104. if s.expandWildcards != "" {
  105. params.Set("expand_wildcards", s.expandWildcards)
  106. }
  107. if s.ignoreUnavailable != nil {
  108. params.Set("ignore_unavailable", fmt.Sprintf("%v", *s.ignoreUnavailable))
  109. }
  110. if s.human != nil {
  111. params.Set("human", fmt.Sprintf("%v", *s.human))
  112. }
  113. if s.operationThreading != nil {
  114. params.Set("operation_threading", fmt.Sprintf("%v", s.operationThreading))
  115. }
  116. if s.verbose != nil {
  117. params.Set("verbose", fmt.Sprintf("%v", *s.verbose))
  118. }
  119. return path, params, nil
  120. }
  121. // Validate checks if the operation is valid.
  122. func (s *IndicesSegmentsService) Validate() error {
  123. return nil
  124. }
  125. // Do executes the operation.
  126. func (s *IndicesSegmentsService) Do(ctx context.Context) (*IndicesSegmentsResponse, 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.PerformRequestWithOptions(ctx, PerformRequestOptions{
  138. Method: "GET",
  139. Path: path,
  140. Params: params,
  141. })
  142. if err != nil {
  143. return nil, err
  144. }
  145. // Return operation response
  146. ret := new(IndicesSegmentsResponse)
  147. if err := json.Unmarshal(res.Body, ret); err != nil {
  148. return nil, err
  149. }
  150. return ret, nil
  151. }
  152. // IndicesSegmentsResponse is the response of IndicesSegmentsService.Do.
  153. type IndicesSegmentsResponse struct {
  154. // Shards provides information returned from shards.
  155. Shards *ShardsInfo `json:"_shards"`
  156. // Indices provides a map into the stats of an index.
  157. // The key of the map is the index name.
  158. Indices map[string]*IndexSegments `json:"indices,omitempty"`
  159. }
  160. type IndexSegments struct {
  161. // Shards provides a map into the shard related information of an index.
  162. // The key of the map is the number of a specific shard.
  163. Shards map[string][]*IndexSegmentsShards `json:"shards,omitempty"`
  164. }
  165. type IndexSegmentsShards struct {
  166. Routing *IndexSegmentsRouting `json:"routing,omitempty"`
  167. NumCommittedSegments int64 `json:"num_committed_segments,omitempty"`
  168. NumSearchSegments int64 `json:"num_search_segments"`
  169. // Segments provides a map into the segment related information of a shard.
  170. // The key of the map is the specific lucene segment id.
  171. Segments map[string]*IndexSegmentsDetails `json:"segments,omitempty"`
  172. }
  173. type IndexSegmentsRouting struct {
  174. State string `json:"state,omitempty"`
  175. Primary bool `json:"primary,omitempty"`
  176. Node string `json:"node,omitempty"`
  177. RelocatingNode string `json:"relocating_node,omitempty"`
  178. }
  179. type IndexSegmentsDetails struct {
  180. Generation int64 `json:"generation,omitempty"`
  181. NumDocs int64 `json:"num_docs,omitempty"`
  182. DeletedDocs int64 `json:"deleted_docs,omitempty"`
  183. Size string `json:"size,omitempty"`
  184. SizeInBytes int64 `json:"size_in_bytes,omitempty"`
  185. Memory string `json:"memory,omitempty"`
  186. MemoryInBytes int64 `json:"memory_in_bytes,omitempty"`
  187. Committed bool `json:"committed,omitempty"`
  188. Search bool `json:"search,omitempty"`
  189. Version string `json:"version,omitempty"`
  190. Compound bool `json:"compound,omitempty"`
  191. MergeId string `json:"merge_id,omitempty"`
  192. RAMTree []*IndexSegmentsRamTree `json:"ram_tree,omitempty"`
  193. }
  194. type IndexSegmentsRamTree struct {
  195. Description string `json:"description,omitempty"`
  196. Size string `json:"size,omitempty"`
  197. SizeInBytes int64 `json:"size_in_bytes,omitempty"`
  198. Children []*IndexSegmentsRamTree `json:"children,omitempty"`
  199. }