1 Star 1 Fork 0

宇宙蒙面侠X/github.com-olivere-elastic

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
indices_get_aliases.go 3.86 KB
一键复制 编辑 原始数据 按行查看 历史
Igor Dubinskiy 提交于 2018-05-10 08:47 . Allow querying of indices by alias
// Copyright 2012-present Oliver Eilhard. All rights reserved.
// Use of this source code is governed by a MIT-license.
// See http://olivere.mit-license.org/license.txt for details.
package elastic
import (
"context"
"fmt"
"net/url"
"strings"
"github.com/olivere/elastic/uritemplates"
)
// AliasesService returns the aliases associated with one or more indices, or the
// indices associated with one or more aliases, or a combination of those filters.
// See http://www.elastic.co/guide/en/elasticsearch/reference/6.2/indices-aliases.html.
type AliasesService struct {
client *Client
index []string
alias []string
pretty bool
}
// NewAliasesService instantiates a new AliasesService.
func NewAliasesService(client *Client) *AliasesService {
builder := &AliasesService{
client: client,
}
return builder
}
// Pretty asks Elasticsearch to indent the returned JSON.
func (s *AliasesService) Pretty(pretty bool) *AliasesService {
s.pretty = pretty
return s
}
// Index adds one or more indices.
func (s *AliasesService) Index(index ...string) *AliasesService {
s.index = append(s.index, index...)
return s
}
// Alias adds one or more aliases.
func (s *AliasesService) Alias(alias ...string) *AliasesService {
s.alias = append(s.alias, alias...)
return s
}
// buildURL builds the URL for the operation.
func (s *AliasesService) buildURL() (string, url.Values, error) {
var err error
var path string
if len(s.index) > 0 {
path, err = uritemplates.Expand("/{index}/_alias/{alias}", map[string]string{
"index": strings.Join(s.index, ","),
"alias": strings.Join(s.alias, ","),
})
} else {
path, err = uritemplates.Expand("/_alias/{alias}", map[string]string{
"alias": strings.Join(s.alias, ","),
})
}
if err != nil {
return "", url.Values{}, err
}
path = strings.TrimSuffix(path, "/")
// Add query string parameters
params := url.Values{}
if s.pretty {
params.Set("pretty", fmt.Sprintf("%v", s.pretty))
}
return path, params, nil
}
func (s *AliasesService) Do(ctx context.Context) (*AliasesResult, error) {
path, params, err := s.buildURL()
if err != nil {
return nil, err
}
// Get response
res, err := s.client.PerformRequest(ctx, PerformRequestOptions{
Method: "GET",
Path: path,
Params: params,
})
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 {
var indices []string
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
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/awol2010ex/github.com-olivere-elastic.git
git@gitee.com:awol2010ex/github.com-olivere-elastic.git
awol2010ex
github.com-olivere-elastic
github.com-olivere-elastic
v6.1.21

搜索帮助