| 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 |
// RefreshService explicitly refreshes one or more indices. |
| 17 |
// See https://www.elastic.co/guide/en/elasticsearch/reference/5.6/indices-refresh.html. |
| 18 |
type RefreshService struct { |
| 19 |
client *Client |
| 20 |
index []string |
| 21 |
pretty bool |
| 22 |
} |
| 23 |
|
| 24 |
// NewRefreshService creates a new instance of RefreshService. |
| 25 |
func NewRefreshService(client *Client) *RefreshService { |
| 26 |
builder := &RefreshService{ |
| 27 |
client: client, |
| 28 |
} |
| 29 |
return builder |
| 30 |
} |
| 31 |
|
| 32 |
// Index specifies the indices to refresh. |
| 33 |
func (s *RefreshService) Index(index ...string) *RefreshService { |
| 34 |
s.index = append(s.index, index...) |
| 35 |
return s |
| 36 |
} |
| 37 |
|
| 38 |
// Pretty asks Elasticsearch to return indented JSON. |
| 39 |
func (s *RefreshService) Pretty(pretty bool) *RefreshService { |
| 40 |
s.pretty = pretty |
| 41 |
return s |
| 42 |
} |
| 43 |
|
| 44 |
// buildURL builds the URL for the operation. |
| 45 |
func (s *RefreshService) buildURL() (string, url.Values, error) { |
| 46 |
var err error |
| 47 |
var path string |
| 48 |
|
| 49 |
if len(s.index) > 0 { |
| 50 |
path, err = uritemplates.Expand("/{index}/_refresh", map[string]string{ |
| 51 |
"index": strings.Join(s.index, ","), |
| 52 |
}) |
| 53 |
} else { |
| 54 |
path = "/_refresh" |
| 55 |
} |
| 56 |
if err != nil { |
| 57 |
return "", url.Values{}, err |
| 58 |
} |
| 59 |
|
| 60 |
// Add query string parameters |
| 61 |
params := url.Values{} |
| 62 |
if s.pretty { |
| 63 |
params.Set("pretty", fmt.Sprintf("%v", s.pretty)) |
| 64 |
} |
| 65 |
return path, params, nil |
| 66 |
} |
| 67 |
|
| 68 |
// Do executes the request. |
| 69 |
func (s *RefreshService) Do(ctx context.Context) (*RefreshResult, error) { |
| 70 |
path, params, err := s.buildURL() |
| 71 |
if err != nil { |
| 72 |
return nil, err |
| 73 |
} |
| 74 |
|
| 75 |
// Get response |
| 76 |
res, err := s.client.PerformRequest(ctx, "POST", path, params, nil) |
| 77 |
if err != nil { |
| 78 |
return nil, err |
| 79 |
} |
| 80 |
|
| 81 |
// Return result |
| 82 |
ret := new(RefreshResult) |
| 83 |
if err := s.client.decoder.Decode(res.Body, ret); err != nil { |
| 84 |
return nil, err |
| 85 |
} |
| 86 |
return ret, nil |
| 87 |
} |
| 88 |
|
| 89 |
// -- Result of a refresh request. |
| 90 |
|
| 91 |
// RefreshResult is the outcome of RefreshService.Do. |
| 92 |
type RefreshResult struct { |
| 93 |
Shards *ShardsInfo `json:"_shards,omitempty"` |
| 94 |
} |