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_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
nodes_info.go
nodes_info_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
rescore.go
rescorer.go
response.go
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_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_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
克隆/下载
index.go 6.52 KB
一键复制 编辑 原始数据 按行查看 历史
// Copyright 2012-2015 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"
"gopkg.in/olivere/elastic.v3/uritemplates"
)
// IndexService adds or updates a typed JSON document in a specified index,
// making it searchable.
//
// See https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-index_.html
// for details.
type IndexService struct {
client *Client
pretty bool
id string
index string
typ string
parent string
replication string
routing string
timeout string
timestamp string
ttl string
version interface{}
opType string
versionType string
refresh *bool
consistency string
bodyJson interface{}
bodyString string
}
// NewIndexService creates a new IndexService.
func NewIndexService(client *Client) *IndexService {
return &IndexService{
client: client,
}
}
// Id is the document ID.
func (s *IndexService) Id(id string) *IndexService {
s.id = id
return s
}
// Index is the name of the index.
func (s *IndexService) Index(index string) *IndexService {
s.index = index
return s
}
// Type is the type of the document.
func (s *IndexService) Type(typ string) *IndexService {
s.typ = typ
return s
}
// Consistency is an explicit write consistency setting for the operation.
func (s *IndexService) Consistency(consistency string) *IndexService {
s.consistency = consistency
return s
}
// Refresh the index after performing the operation.
func (s *IndexService) Refresh(refresh bool) *IndexService {
s.refresh = &refresh
return s
}
// Ttl is an expiration time for the document.
func (s *IndexService) Ttl(ttl string) *IndexService {
s.ttl = ttl
return s
}
// TTL is an expiration time for the document (alias for Ttl).
func (s *IndexService) TTL(ttl string) *IndexService {
s.ttl = ttl
return s
}
// Version is an explicit version number for concurrency control.
func (s *IndexService) Version(version interface{}) *IndexService {
s.version = version
return s
}
// OpType is an explicit operation type, i.e. "create" or "index" (default).
func (s *IndexService) OpType(opType string) *IndexService {
s.opType = opType
return s
}
// Parent is the ID of the parent document.
func (s *IndexService) Parent(parent string) *IndexService {
s.parent = parent
return s
}
// Replication is a specific replication type.
func (s *IndexService) Replication(replication string) *IndexService {
s.replication = replication
return s
}
// Routing is a specific routing value.
func (s *IndexService) Routing(routing string) *IndexService {
s.routing = routing
return s
}
// Timeout is an explicit operation timeout.
func (s *IndexService) Timeout(timeout string) *IndexService {
s.timeout = timeout
return s
}
// Timestamp is an explicit timestamp for the document.
func (s *IndexService) Timestamp(timestamp string) *IndexService {
s.timestamp = timestamp
return s
}
// VersionType is a specific version type.
func (s *IndexService) VersionType(versionType string) *IndexService {
s.versionType = versionType
return s
}
// Pretty indicates that the JSON response be indented and human readable.
func (s *IndexService) Pretty(pretty bool) *IndexService {
s.pretty = pretty
return s
}
// BodyJson is the document as a serializable JSON interface.
func (s *IndexService) BodyJson(body interface{}) *IndexService {
s.bodyJson = body
return s
}
// BodyString is the document encoded as a string.
func (s *IndexService) BodyString(body string) *IndexService {
s.bodyString = body
return s
}
// buildURL builds the URL for the operation.
func (s *IndexService) buildURL() (string, string, url.Values, error) {
var err error
var method, path string
if s.id != "" {
// Create document with manual id
method = "PUT"
path, err = uritemplates.Expand("/{index}/{type}/{id}", map[string]string{
"id": s.id,
"index": s.index,
"type": s.typ,
})
} else {
// Automatic ID generation
// See https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-index_.html#index-creation
method = "POST"
path, err = uritemplates.Expand("/{index}/{type}/", map[string]string{
"index": s.index,
"type": s.typ,
})
}
if err != nil {
return "", "", url.Values{}, err
}
// Add query string parameters
params := url.Values{}
if s.pretty {
params.Set("pretty", "1")
}
if s.consistency != "" {
params.Set("consistency", s.consistency)
}
if s.refresh != nil {
params.Set("refresh", fmt.Sprintf("%v", *s.refresh))
}
if s.opType != "" {
params.Set("op_type", s.opType)
}
if s.parent != "" {
params.Set("parent", s.parent)
}
if s.replication != "" {
params.Set("replication", s.replication)
}
if s.routing != "" {
params.Set("routing", s.routing)
}
if s.timeout != "" {
params.Set("timeout", s.timeout)
}
if s.timestamp != "" {
params.Set("timestamp", s.timestamp)
}
if s.ttl != "" {
params.Set("ttl", s.ttl)
}
if s.version != nil {
params.Set("version", fmt.Sprintf("%v", s.version))
}
if s.versionType != "" {
params.Set("version_type", s.versionType)
}
return method, path, params, nil
}
// Validate checks if the operation is valid.
func (s *IndexService) Validate() error {
var invalid []string
if s.index == "" {
invalid = append(invalid, "Index")
}
if s.typ == "" {
invalid = append(invalid, "Type")
}
if s.bodyString == "" && s.bodyJson == nil {
invalid = append(invalid, "BodyJson")
}
if len(invalid) > 0 {
return fmt.Errorf("missing required fields: %v", invalid)
}
return nil
}
// Do executes the operation.
func (s *IndexService) Do() (*IndexResponse, error) {
// Check pre-conditions
if err := s.Validate(); err != nil {
return nil, err
}
// Get URL for request
method, path, params, err := s.buildURL()
if err != nil {
return nil, err
}
// Setup HTTP request body
var body interface{}
if s.bodyJson != nil {
body = s.bodyJson
} else {
body = s.bodyString
}
// Get HTTP response
res, err := s.client.PerformRequest(method, path, params, body)
if err != nil {
return nil, err
}
// Return operation response
ret := new(IndexResponse)
if err := s.client.decoder.Decode(res.Body, ret); err != nil {
return nil, err
}
return ret, nil
}
// IndexResponse is the result of indexing a document in Elasticsearch.
type IndexResponse struct {
// TODO _shards { total, failed, successful }
Index string `json:"_index"`
Type string `json:"_type"`
Id string `json:"_id"`
Version int `json:"_version"`
Created bool `json:"created"`
}
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.40

搜索帮助