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.
159 lines
3.4 KiB
159 lines
3.4 KiB
8 years ago
|
// Copyright 2012-present Oliver Eilhard. All rights reserved.
|
||
9 years ago
|
// Use of this source code is governed by a MIT-license.
|
||
|
// See http://olivere.mit-license.org/license.txt for details.
|
||
|
|
||
|
package elastic
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"net/url"
|
||
|
"strings"
|
||
|
|
||
8 years ago
|
"golang.org/x/net/context"
|
||
|
|
||
|
"gopkg.in/olivere/elastic.v5/uritemplates"
|
||
9 years ago
|
)
|
||
|
|
||
8 years ago
|
// AliasesService returns the aliases associated with one or more indices.
|
||
|
// See http://www.elastic.co/guide/en/elasticsearch/reference/5.2/indices-aliases.html.
|
||
9 years ago
|
type AliasesService struct {
|
||
8 years ago
|
client *Client
|
||
|
index []string
|
||
|
pretty bool
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
// NewAliasesService instantiates a new AliasesService.
|
||
9 years ago
|
func NewAliasesService(client *Client) *AliasesService {
|
||
|
builder := &AliasesService{
|
||
8 years ago
|
client: client,
|
||
9 years ago
|
}
|
||
|
return builder
|
||
|
}
|
||
|
|
||
8 years ago
|
// Pretty asks Elasticsearch to indent the returned JSON.
|
||
9 years ago
|
func (s *AliasesService) Pretty(pretty bool) *AliasesService {
|
||
|
s.pretty = pretty
|
||
|
return s
|
||
|
}
|
||
|
|
||
8 years ago
|
// Index adds one or more indices.
|
||
|
func (s *AliasesService) Index(index ...string) *AliasesService {
|
||
|
s.index = append(s.index, index...)
|
||
9 years ago
|
return s
|
||
|
}
|
||
|
|
||
8 years ago
|
// buildURL builds the URL for the operation.
|
||
|
func (s *AliasesService) buildURL() (string, url.Values, error) {
|
||
9 years ago
|
var err error
|
||
8 years ago
|
var path string
|
||
9 years ago
|
|
||
8 years ago
|
if len(s.index) > 0 {
|
||
|
path, err = uritemplates.Expand("/{index}/_aliases", map[string]string{
|
||
|
"index": strings.Join(s.index, ","),
|
||
9 years ago
|
})
|
||
8 years ago
|
} else {
|
||
|
path = "/_aliases"
|
||
|
}
|
||
|
if err != nil {
|
||
|
return "", url.Values{}, err
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
// Add query string parameters
|
||
|
params := url.Values{}
|
||
9 years ago
|
if s.pretty {
|
||
|
params.Set("pretty", fmt.Sprintf("%v", s.pretty))
|
||
|
}
|
||
8 years ago
|
return path, params, nil
|
||
|
}
|
||
|
|
||
|
func (s *AliasesService) Do(ctx context.Context) (*AliasesResult, error) {
|
||
|
path, params, err := s.buildURL()
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
9 years ago
|
|
||
|
// Get response
|
||
8 years ago
|
res, err := s.client.PerformRequest(ctx, "GET", path, params, nil)
|
||
9 years ago
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
// {
|
||
|
// "indexName" : {
|
||
|
// "aliases" : {
|
||
|
// "alias1" : { },
|
||
|
// "alias2" : { }
|
||
|
// }
|
||
|
// },
|
||
|
// "indexName2" : {
|
||
|
// ...
|
||
|
// },
|
||
|
// }
|
||
|
indexMap := make(map[string]interface{})
|
||
|
if err := s.client.decoder.Decode(res.Body, &indexMap); err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
// Each (indexName, _)
|
||
|
ret := &AliasesResult{
|
||
|
Indices: make(map[string]indexResult),
|
||
|
}
|
||
|
for indexName, indexData := range indexMap {
|
||
|
indexOut, found := ret.Indices[indexName]
|
||
|
if !found {
|
||
|
indexOut = indexResult{Aliases: make([]aliasResult, 0)}
|
||
|
}
|
||
|
|
||
|
// { "aliases" : { ... } }
|
||
|
indexDataMap, ok := indexData.(map[string]interface{})
|
||
|
if ok {
|
||
|
aliasesData, ok := indexDataMap["aliases"].(map[string]interface{})
|
||
|
if ok {
|
||
|
for aliasName, _ := range aliasesData {
|
||
|
aliasRes := aliasResult{AliasName: aliasName}
|
||
|
indexOut.Aliases = append(indexOut.Aliases, aliasRes)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
ret.Indices[indexName] = indexOut
|
||
|
}
|
||
|
|
||
|
return ret, nil
|
||
|
}
|
||
|
|
||
|
// -- Result of an alias request.
|
||
|
|
||
|
type AliasesResult struct {
|
||
|
Indices map[string]indexResult
|
||
|
}
|
||
|
|
||
|
type indexResult struct {
|
||
|
Aliases []aliasResult
|
||
|
}
|
||
|
|
||
|
type aliasResult struct {
|
||
|
AliasName string
|
||
|
}
|
||
|
|
||
|
func (ar AliasesResult) IndicesByAlias(aliasName string) []string {
|
||
8 years ago
|
var indices []string
|
||
9 years ago
|
for indexName, indexInfo := range ar.Indices {
|
||
|
for _, aliasInfo := range indexInfo.Aliases {
|
||
|
if aliasInfo.AliasName == aliasName {
|
||
|
indices = append(indices, indexName)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return indices
|
||
|
}
|
||
|
|
||
|
func (ir indexResult) HasAlias(aliasName string) bool {
|
||
|
for _, alias := range ir.Aliases {
|
||
|
if alias.AliasName == aliasName {
|
||
|
return true
|
||
|
}
|
||
|
}
|
||
|
return false
|
||
|
}
|