1 Star 1 Fork 0

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

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
backoff
cluster-test
config
uritemplates
.gitignore
.travis.yml
CHANGELOG-3.0.md
CONTRIBUTING.md
CONTRIBUTORS
ISSUE_TEMPLATE.md
LICENSE
README.md
bulk.go
bulk_delete_request.go
bulk_delete_request_test.go
bulk_index_request.go
bulk_index_request_test.go
bulk_processor.go
bulk_processor_test.go
bulk_request.go
bulk_test.go
bulk_update_request.go
bulk_update_request_test.go
canonicalize.go
canonicalize_test.go
clear_scroll.go
clear_scroll_test.go
client.go
client_test.go
cluster_health.go
cluster_health_test.go
cluster_state.go
cluster_state_test.go
cluster_stats.go
cluster_stats_test.go
connection.go
count.go
count_test.go
decoder.go
decoder_test.go
delete.go
delete_by_query.go
delete_by_query_test.go
delete_template.go
delete_template_test.go
delete_test.go
doc.go
errors.go
errors_test.go
example_test.go
exists.go
exists_test.go
explain.go
explain_test.go
fetch_source_context.go
fetch_source_context_test.go
field_stats.go
field_stats_test.go
geo_point.go
geo_point_test.go
get.go
get_template.go
get_template_test.go
get_test.go
highlight.go
highlight_test.go
index.go
index_test.go
indices_analyze.go
indices_analyze_test.go
indices_close.go
indices_close_test.go
indices_create.go
indices_create_test.go
indices_delete.go
indices_delete_template.go
indices_delete_test.go
indices_delete_warmer.go
indices_delete_warmer_test.go
indices_exists.go
indices_exists_template.go
indices_exists_template_test.go
indices_exists_test.go
indices_exists_type.go
indices_exists_type_test.go
indices_flush.go
indices_flush_test.go
indices_forcemerge.go
indices_forcemerge_test.go
indices_get.go
indices_get_aliases.go
indices_get_aliases_test.go
indices_get_mapping.go
indices_get_mapping_test.go
indices_get_settings.go
indices_get_settings_test.go
indices_get_template.go
indices_get_template_test.go
indices_get_test.go
indices_get_warmer.go
indices_get_warmer_test.go
indices_open.go
indices_open_test.go
indices_put_alias.go
indices_put_alias_test.go
indices_put_mapping.go
indices_put_mapping_test.go
indices_put_settings.go
indices_put_settings_test.go
indices_put_template.go
indices_put_warmer.go
indices_put_warmer_test.go
indices_refresh.go
indices_refresh_test.go
indices_stats.go
indices_stats_test.go
inner_hit.go
inner_hit_test.go
logger.go
mget.go
mget_test.go
msearch.go
msearch_test.go
mtermvectors.go
mtermvectors_test.go
nodes_info.go
nodes_info_test.go
nodes_stats.go
nodes_stats_test.go
optimize.go
optimize_test.go
percolate.go
percolate_test.go
ping.go
ping_test.go
plugins.go
plugins_test.go
query.go
reindex.go
reindex_test.go
reindexer.go
reindexer_test.go
request.go
request_test.go
rescore.go
rescorer.go
response.go
run-es.sh
scan.go
scan_test.go
script.go
script_test.go
scroll.go
scroll_test.go
search.go
search_aggs.go
search_aggs_bucket_children.go
search_aggs_bucket_children_test.go
search_aggs_bucket_date_histogram.go
search_aggs_bucket_date_histogram_test.go
search_aggs_bucket_date_range.go
search_aggs_bucket_date_range_test.go
search_aggs_bucket_filter.go
search_aggs_bucket_filter_test.go
search_aggs_bucket_filters.go
search_aggs_bucket_filters_test.go
search_aggs_bucket_geo_distance.go
search_aggs_bucket_geo_distance_test.go
search_aggs_bucket_geohash_grid.go
search_aggs_bucket_geohash_grid_test.go
search_aggs_bucket_global.go
search_aggs_bucket_global_test.go
search_aggs_bucket_histogram.go
search_aggs_bucket_histogram_test.go
search_aggs_bucket_missing.go
search_aggs_bucket_missing_test.go
search_aggs_bucket_nested.go
search_aggs_bucket_nested_test.go
search_aggs_bucket_range.go
search_aggs_bucket_range_test.go
search_aggs_bucket_reverse_nested.go
search_aggs_bucket_reverse_nested_test.go
search_aggs_bucket_sampler.go
search_aggs_bucket_sampler_test.go
search_aggs_bucket_significant_terms.go
search_aggs_bucket_significant_terms_test.go
search_aggs_bucket_terms.go
search_aggs_bucket_terms_test.go
search_aggs_metrics_avg.go
search_aggs_metrics_avg_test.go
search_aggs_metrics_cardinality.go
search_aggs_metrics_cardinality_test.go
search_aggs_metrics_extended_stats.go
search_aggs_metrics_extended_stats_test.go
search_aggs_metrics_geo_bounds.go
search_aggs_metrics_geo_bounds_test.go
search_aggs_metrics_max.go
search_aggs_metrics_max_test.go
search_aggs_metrics_min.go
search_aggs_metrics_min_test.go
search_aggs_metrics_percentile_ranks.go
search_aggs_metrics_percentile_ranks_test.go
search_aggs_metrics_percentiles.go
search_aggs_metrics_percentiles_test.go
search_aggs_metrics_stats.go
search_aggs_metrics_stats_test.go
search_aggs_metrics_sum.go
search_aggs_metrics_sum_test.go
search_aggs_metrics_top_hits.go
search_aggs_metrics_top_hits_test.go
search_aggs_metrics_value_count.go
search_aggs_metrics_value_count_test.go
search_aggs_pipeline_avg_bucket.go
search_aggs_pipeline_avg_bucket_test.go
search_aggs_pipeline_bucket_script.go
search_aggs_pipeline_bucket_script_test.go
search_aggs_pipeline_bucket_selector.go
search_aggs_pipeline_bucket_selector_test.go
search_aggs_pipeline_cumulative_sum.go
search_aggs_pipeline_cumulative_sum_test.go
search_aggs_pipeline_derivative.go
search_aggs_pipeline_derivative_test.go
search_aggs_pipeline_max_bucket.go
search_aggs_pipeline_max_bucket_test.go
search_aggs_pipeline_min_bucket.go
search_aggs_pipeline_min_bucket_test.go
search_aggs_pipeline_mov_avg.go
search_aggs_pipeline_mov_avg_test.go
search_aggs_pipeline_serial_diff.go
search_aggs_pipeline_serial_diff_test.go
search_aggs_pipeline_stats_bucket.go
search_aggs_pipeline_stats_bucket_test.go
search_aggs_pipeline_sum_bucket.go
search_aggs_pipeline_sum_bucket_test.go
search_aggs_pipeline_test.go
search_aggs_test.go
search_queries_bool.go
search_queries_bool_test.go
search_queries_boosting.go
search_queries_boosting_test.go
search_queries_common_terms.go
search_queries_common_terms_test.go
search_queries_constant_score.go
search_queries_constant_score_test.go
search_queries_dis_max.go
search_queries_dis_max_test.go
search_queries_exists.go
search_queries_exists_test.go
search_queries_fsq.go
search_queries_fsq_score_funcs.go
search_queries_fsq_test.go
search_queries_fuzzy.go
search_queries_fuzzy_test.go
search_queries_geo_bounding_box.go
search_queries_geo_bounding_box_test.go
search_queries_geo_distance.go
search_queries_geo_distance_test.go
search_queries_geo_polygon.go
search_queries_geo_polygon_test.go
search_queries_has_child.go
search_queries_has_child_test.go
search_queries_has_parent.go
search_queries_has_parent_test.go
search_queries_ids.go
search_queries_ids_test.go
search_queries_indices.go
search_queries_indices_test.go
search_queries_match.go
search_queries_match_all.go
search_queries_match_all_test.go
search_queries_match_test.go
search_queries_missing.go
search_queries_missing_test.go
search_queries_more_like_this.go
search_queries_more_like_this_test.go
search_queries_multi_match.go
search_queries_multi_match_test.go
search_queries_nested.go
search_queries_nested_test.go
search_queries_not.go
search_queries_not_test.go
search_queries_prefix.go
search_queries_prefix_test.go
search_queries_query_string.go
search_queries_query_string_test.go
search_queries_range.go
search_queries_range_test.go
search_queries_raw_string.go
search_queries_raw_string_test.go
search_queries_regexp.go
search_queries_regexp_test.go
search_queries_script.go
search_queries_script_test.go
search_queries_simple_query_string.go
search_queries_simple_query_string_test.go
search_queries_template_query.go
search_queries_template_query_test.go
search_queries_term.go
search_queries_term_test.go
search_queries_terms.go
search_queries_terms_test.go
search_queries_type.go
search_queries_type_test.go
search_queries_wildcard.go
search_queries_wildcard_test.go
search_request.go
search_request_test.go
search_source.go
search_source_test.go
search_suggester_test.go
search_template.go
search_templates_test.go
search_test.go
setup_test.go
sort.go
sort_test.go
suggest.go
suggest_field.go
suggest_field_test.go
suggest_test.go
suggester.go
suggester_completion.go
suggester_completion_fuzzy.go
suggester_completion_fuzzy_test.go
suggester_completion_test.go
suggester_context.go
suggester_context_category.go
suggester_context_category_test.go
suggester_context_geo.go
suggester_context_geo_test.go
suggester_phrase.go
suggester_phrase_test.go
suggester_term.go
suggester_term_test.go
tasks_cancel.go
tasks_cancel_test.go
tasks_list.go
tasks_list_test.go
termvectors.go
termvectors_test.go
update.go
update_by_query.go
update_by_query_test.go
update_test.go
克隆/下载
indices_put_alias.go 7.15 KB
一键复制 编辑 原始数据 按行查看 历史
// 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 (
"fmt"
"net/url"
"strings"
"golang.org/x/net/context"
)
// -- Actions --
// AliasAction is an action to apply to an alias, e.g. "add" or "remove".
type AliasAction interface {
Source() (interface{}, error)
}
// AliasAddAction is an action to add to an alias.
type AliasAddAction struct {
index []string // index name(s)
alias string // alias name
filter Query
routing string
searchRouting string
indexRouting string
}
// NewAliasAddAction returns an action to add an alias.
func NewAliasAddAction(alias string) *AliasAddAction {
return &AliasAddAction{
alias: alias,
}
}
// Index associates one or more indices to the alias.
func (a *AliasAddAction) Index(index ...string) *AliasAddAction {
a.index = append(a.index, index...)
return a
}
func (a *AliasAddAction) removeBlankIndexNames() {
var indices []string
for _, index := range a.index {
if len(index) > 0 {
indices = append(indices, index)
}
}
a.index = indices
}
// Filter associates a filter to the alias.
func (a *AliasAddAction) Filter(filter Query) *AliasAddAction {
a.filter = filter
return a
}
// Routing associates a routing value to the alias.
// This basically sets index and search routing to the same value.
func (a *AliasAddAction) Routing(routing string) *AliasAddAction {
a.routing = routing
return a
}
// IndexRouting associates an index routing value to the alias.
func (a *AliasAddAction) IndexRouting(routing string) *AliasAddAction {
a.indexRouting = routing
return a
}
// SearchRouting associates a search routing value to the alias.
func (a *AliasAddAction) SearchRouting(routing ...string) *AliasAddAction {
a.searchRouting = strings.Join(routing, ",")
return a
}
// Validate checks if the operation is valid.
func (a *AliasAddAction) Validate() error {
var invalid []string
if len(a.alias) == 0 {
invalid = append(invalid, "Alias")
}
if len(a.index) == 0 {
invalid = append(invalid, "Index")
}
if len(invalid) > 0 {
return fmt.Errorf("missing required fields: %v", invalid)
}
return nil
}
// Source returns the JSON-serializable data.
func (a *AliasAddAction) Source() (interface{}, error) {
a.removeBlankIndexNames()
if err := a.Validate(); err != nil {
return nil, err
}
src := make(map[string]interface{})
act := make(map[string]interface{})
src["add"] = act
act["alias"] = a.alias
switch len(a.index) {
case 1:
act["index"] = a.index[0]
default:
act["indices"] = a.index
}
if a.filter != nil {
f, err := a.filter.Source()
if err != nil {
return nil, err
}
act["filter"] = f
}
if len(a.routing) > 0 {
act["routing"] = a.routing
}
if len(a.indexRouting) > 0 {
act["index_routing"] = a.indexRouting
}
if len(a.searchRouting) > 0 {
act["search_routing"] = a.searchRouting
}
return src, nil
}
// AliasRemoveAction is an action to remove an alias.
type AliasRemoveAction struct {
index []string // index name(s)
alias string // alias name
}
// NewAliasRemoveAction returns an action to remove an alias.
func NewAliasRemoveAction(alias string) *AliasRemoveAction {
return &AliasRemoveAction{
alias: alias,
}
}
// Index associates one or more indices to the alias.
func (a *AliasRemoveAction) Index(index ...string) *AliasRemoveAction {
a.index = append(a.index, index...)
return a
}
func (a *AliasRemoveAction) removeBlankIndexNames() {
var indices []string
for _, index := range a.index {
if len(index) > 0 {
indices = append(indices, index)
}
}
a.index = indices
}
// Validate checks if the operation is valid.
func (a *AliasRemoveAction) Validate() error {
var invalid []string
if len(a.alias) == 0 {
invalid = append(invalid, "Alias")
}
if len(a.index) == 0 {
invalid = append(invalid, "Index")
}
if len(invalid) > 0 {
return fmt.Errorf("missing required fields: %v", invalid)
}
return nil
}
// Source returns the JSON-serializable data.
func (a *AliasRemoveAction) Source() (interface{}, error) {
a.removeBlankIndexNames()
if err := a.Validate(); err != nil {
return nil, err
}
src := make(map[string]interface{})
act := make(map[string]interface{})
src["remove"] = act
act["alias"] = a.alias
switch len(a.index) {
case 1:
act["index"] = a.index[0]
default:
act["indices"] = a.index
}
return src, nil
}
// -- Service --
// AliasService enables users to add or remove an alias.
// See https://www.elastic.co/guide/en/elasticsearch/reference/2.3/indices-aliases.html
// for details.
type AliasService struct {
client *Client
actions []AliasAction
pretty bool
}
// NewAliasService implements a service to manage aliases.
func NewAliasService(client *Client) *AliasService {
builder := &AliasService{
client: client,
}
return builder
}
// Pretty asks Elasticsearch to indent the HTTP response.
func (s *AliasService) Pretty(pretty bool) *AliasService {
s.pretty = pretty
return s
}
// Add adds an alias to an index.
func (s *AliasService) Add(indexName string, aliasName string) *AliasService {
action := NewAliasAddAction(aliasName).Index(indexName)
s.actions = append(s.actions, action)
return s
}
// Add adds an alias to an index and associates a filter to the alias.
func (s *AliasService) AddWithFilter(indexName string, aliasName string, filter Query) *AliasService {
action := NewAliasAddAction(aliasName).Index(indexName).Filter(filter)
s.actions = append(s.actions, action)
return s
}
// Remove removes an alias.
func (s *AliasService) Remove(indexName string, aliasName string) *AliasService {
action := NewAliasRemoveAction(aliasName).Index(indexName)
s.actions = append(s.actions, action)
return s
}
// Action accepts one or more AliasAction instances which can be
// of type AliasAddAction or AliasRemoveAction.
func (s *AliasService) Action(action ...AliasAction) *AliasService {
s.actions = append(s.actions, action...)
return s
}
// buildURL builds the URL for the operation.
func (s *AliasService) buildURL() (string, url.Values, error) {
path := "/_aliases"
// Add query string parameters
params := url.Values{}
if s.pretty {
params.Set("pretty", fmt.Sprintf("%v", s.pretty))
}
return path, params, nil
}
// Do executes the command.
func (s *AliasService) Do() (*AliasResult, error) {
return s.DoC(nil)
}
// DoC executes the command.
func (s *AliasService) DoC(ctx context.Context) (*AliasResult, error) {
path, params, err := s.buildURL()
if err != nil {
return nil, err
}
// Body with actions
body := make(map[string]interface{})
var actions []interface{}
for _, action := range s.actions {
src, err := action.Source()
if err != nil {
return nil, err
}
actions = append(actions, src)
}
body["actions"] = actions
// Get response
res, err := s.client.PerformRequestC(ctx, "POST", path, params, body)
if err != nil {
return nil, err
}
// Return results
ret := new(AliasResult)
if err := s.client.decoder.Decode(res.Body, ret); err != nil {
return nil, err
}
return ret, nil
}
// -- Result of an alias request.
// AliasResult is the outcome of calling Do on AliasService.
type AliasResult struct {
Acknowledged bool `json:"acknowledged"`
}
Loading...
马建仓 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
v3.0.62

搜索帮助