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