indices_create.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. "errors"
  8. "net/url"
  9. "gopkg.in/olivere/elastic.v5/uritemplates"
  10. )
  11. // IndicesCreateService creates a new index.
  12. //
  13. // See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/indices-create-index.html
  14. // for details.
  15. type IndicesCreateService struct {
  16. client *Client
  17. pretty bool
  18. index string
  19. timeout string
  20. masterTimeout string
  21. bodyJson interface{}
  22. bodyString string
  23. }
  24. // NewIndicesCreateService returns a new IndicesCreateService.
  25. func NewIndicesCreateService(client *Client) *IndicesCreateService {
  26. return &IndicesCreateService{client: client}
  27. }
  28. // Index is the name of the index to create.
  29. func (b *IndicesCreateService) Index(index string) *IndicesCreateService {
  30. b.index = index
  31. return b
  32. }
  33. // Timeout the explicit operation timeout, e.g. "5s".
  34. func (s *IndicesCreateService) Timeout(timeout string) *IndicesCreateService {
  35. s.timeout = timeout
  36. return s
  37. }
  38. // MasterTimeout specifies the timeout for connection to master.
  39. func (s *IndicesCreateService) MasterTimeout(masterTimeout string) *IndicesCreateService {
  40. s.masterTimeout = masterTimeout
  41. return s
  42. }
  43. // Body specifies the configuration of the index as a string.
  44. // It is an alias for BodyString.
  45. func (b *IndicesCreateService) Body(body string) *IndicesCreateService {
  46. b.bodyString = body
  47. return b
  48. }
  49. // BodyString specifies the configuration of the index as a string.
  50. func (b *IndicesCreateService) BodyString(body string) *IndicesCreateService {
  51. b.bodyString = body
  52. return b
  53. }
  54. // BodyJson specifies the configuration of the index. The interface{} will
  55. // be serializes as a JSON document, so use a map[string]interface{}.
  56. func (b *IndicesCreateService) BodyJson(body interface{}) *IndicesCreateService {
  57. b.bodyJson = body
  58. return b
  59. }
  60. // Pretty indicates that the JSON response be indented and human readable.
  61. func (b *IndicesCreateService) Pretty(pretty bool) *IndicesCreateService {
  62. b.pretty = pretty
  63. return b
  64. }
  65. // Do executes the operation.
  66. func (b *IndicesCreateService) Do(ctx context.Context) (*IndicesCreateResult, error) {
  67. if b.index == "" {
  68. return nil, errors.New("missing index name")
  69. }
  70. // Build url
  71. path, err := uritemplates.Expand("/{index}", map[string]string{
  72. "index": b.index,
  73. })
  74. if err != nil {
  75. return nil, err
  76. }
  77. params := make(url.Values)
  78. if b.pretty {
  79. params.Set("pretty", "1")
  80. }
  81. if b.masterTimeout != "" {
  82. params.Set("master_timeout", b.masterTimeout)
  83. }
  84. if b.timeout != "" {
  85. params.Set("timeout", b.timeout)
  86. }
  87. // Setup HTTP request body
  88. var body interface{}
  89. if b.bodyJson != nil {
  90. body = b.bodyJson
  91. } else {
  92. body = b.bodyString
  93. }
  94. // Get response
  95. res, err := b.client.PerformRequest(ctx, "PUT", path, params, body)
  96. if err != nil {
  97. return nil, err
  98. }
  99. ret := new(IndicesCreateResult)
  100. if err := b.client.decoder.Decode(res.Body, ret); err != nil {
  101. return nil, err
  102. }
  103. return ret, nil
  104. }
  105. // -- Result of a create index request.
  106. // IndicesCreateResult is the outcome of creating a new index.
  107. type IndicesCreateResult struct {
  108. Acknowledged bool `json:"acknowledged"`
  109. ShardsAcknowledged bool `json:"shards_acknowledged"`
  110. Index string `json:"index"`
  111. }