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
mtermvectors.go
mtermvectors_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
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_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
克隆/下载
count.go 8.32 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"
"gopkg.in/olivere/elastic.v3/uritemplates"
)
// CountService is a convenient service for determining the
// number of documents in an index. Use SearchService with
// a SearchType of count for counting with queries etc.
type CountService struct {
client *Client
pretty bool
index []string
typ []string
allowNoIndices *bool
analyzeWildcard *bool
analyzer string
defaultOperator string
df string
expandWildcards string
ignoreUnavailable *bool
lenient *bool
lowercaseExpandedTerms *bool
minScore interface{}
preference string
q string
query Query
routing string
bodyJson interface{}
bodyString string
}
// NewCountService creates a new CountService.
func NewCountService(client *Client) *CountService {
return &CountService{
client: client,
}
}
// Index sets the names of the indices to restrict the results.
func (s *CountService) Index(index ...string) *CountService {
if s.index == nil {
s.index = make([]string, 0)
}
s.index = append(s.index, index...)
return s
}
// Type sets the types to use to restrict the results.
func (s *CountService) Type(typ ...string) *CountService {
if s.typ == nil {
s.typ = make([]string, 0)
}
s.typ = append(s.typ, typ...)
return s
}
// AllowNoIndices indicates whether to ignore if a wildcard indices
// expression resolves into no concrete indices. (This includes "_all" string
// or when no indices have been specified).
func (s *CountService) AllowNoIndices(allowNoIndices bool) *CountService {
s.allowNoIndices = &allowNoIndices
return s
}
// AnalyzeWildcard specifies whether wildcard and prefix queries should be
// analyzed (default: false).
func (s *CountService) AnalyzeWildcard(analyzeWildcard bool) *CountService {
s.analyzeWildcard = &analyzeWildcard
return s
}
// Analyzer specifies the analyzer to use for the query string.
func (s *CountService) Analyzer(analyzer string) *CountService {
s.analyzer = analyzer
return s
}
// DefaultOperator specifies the default operator for query string query (AND or OR).
func (s *CountService) DefaultOperator(defaultOperator string) *CountService {
s.defaultOperator = defaultOperator
return s
}
// Df specifies the field to use as default where no field prefix is given
// in the query string.
func (s *CountService) Df(df string) *CountService {
s.df = df
return s
}
// ExpandWildcards indicates whether to expand wildcard expression to
// concrete indices that are open, closed or both.
func (s *CountService) ExpandWildcards(expandWildcards string) *CountService {
s.expandWildcards = expandWildcards
return s
}
// IgnoreUnavailable indicates whether specified concrete indices should be
// ignored when unavailable (missing or closed).
func (s *CountService) IgnoreUnavailable(ignoreUnavailable bool) *CountService {
s.ignoreUnavailable = &ignoreUnavailable
return s
}
// Lenient specifies whether format-based query failures (such as
// providing text to a numeric field) should be ignored.
func (s *CountService) Lenient(lenient bool) *CountService {
s.lenient = &lenient
return s
}
// LowercaseExpandedTerms specifies whether query terms should be lowercased.
func (s *CountService) LowercaseExpandedTerms(lowercaseExpandedTerms bool) *CountService {
s.lowercaseExpandedTerms = &lowercaseExpandedTerms
return s
}
// MinScore indicates to include only documents with a specific `_score`
// value in the result.
func (s *CountService) MinScore(minScore interface{}) *CountService {
s.minScore = minScore
return s
}
// Preference specifies the node or shard the operation should be
// performed on (default: random).
func (s *CountService) Preference(preference string) *CountService {
s.preference = preference
return s
}
// Q in the Lucene query string syntax. You can also use Query to pass
// a Query struct.
func (s *CountService) Q(q string) *CountService {
s.q = q
return s
}
// Query specifies the query to pass. You can also pass a query string with Q.
func (s *CountService) Query(query Query) *CountService {
s.query = query
return s
}
// Routing specifies the routing value.
func (s *CountService) Routing(routing string) *CountService {
s.routing = routing
return s
}
// Pretty indicates that the JSON response be indented and human readable.
func (s *CountService) Pretty(pretty bool) *CountService {
s.pretty = pretty
return s
}
// BodyJson specifies the query to restrict the results specified with the
// Query DSL (optional). The interface{} will be serialized to a JSON document,
// so use a map[string]interface{}.
func (s *CountService) BodyJson(body interface{}) *CountService {
s.bodyJson = body
return s
}
// Body specifies a query to restrict the results specified with
// the Query DSL (optional).
func (s *CountService) BodyString(body string) *CountService {
s.bodyString = body
return s
}
// buildURL builds the URL for the operation.
func (s *CountService) buildURL() (string, url.Values, error) {
var err error
var path string
if len(s.index) > 0 && len(s.typ) > 0 {
path, err = uritemplates.Expand("/{index}/{type}/_count", map[string]string{
"index": strings.Join(s.index, ","),
"type": strings.Join(s.typ, ","),
})
} else if len(s.index) > 0 {
path, err = uritemplates.Expand("/{index}/_count", map[string]string{
"index": strings.Join(s.index, ","),
})
} else if len(s.typ) > 0 {
path, err = uritemplates.Expand("/_all/{type}/_count", map[string]string{
"type": strings.Join(s.typ, ","),
})
} else {
path = "/_all/_count"
}
if err != nil {
return "", url.Values{}, err
}
// Add query string parameters
params := url.Values{}
if s.pretty {
params.Set("pretty", "1")
}
if s.allowNoIndices != nil {
params.Set("allow_no_indices", fmt.Sprintf("%v", *s.allowNoIndices))
}
if s.analyzeWildcard != nil {
params.Set("analyze_wildcard", fmt.Sprintf("%v", *s.analyzeWildcard))
}
if s.analyzer != "" {
params.Set("analyzer", s.analyzer)
}
if s.defaultOperator != "" {
params.Set("default_operator", s.defaultOperator)
}
if s.df != "" {
params.Set("df", s.df)
}
if s.expandWildcards != "" {
params.Set("expand_wildcards", s.expandWildcards)
}
if s.ignoreUnavailable != nil {
params.Set("ignore_unavailable", fmt.Sprintf("%v", *s.ignoreUnavailable))
}
if s.lenient != nil {
params.Set("lenient", fmt.Sprintf("%v", *s.lenient))
}
if s.lowercaseExpandedTerms != nil {
params.Set("lowercase_expanded_terms", fmt.Sprintf("%v", *s.lowercaseExpandedTerms))
}
if s.minScore != nil {
params.Set("min_score", fmt.Sprintf("%v", s.minScore))
}
if s.preference != "" {
params.Set("preference", s.preference)
}
if s.q != "" {
params.Set("q", s.q)
}
if s.routing != "" {
params.Set("routing", s.routing)
}
return path, params, nil
}
// Validate checks if the operation is valid.
func (s *CountService) Validate() error {
return nil
}
// Do executes the operation.
func (s *CountService) Do() (int64, error) {
return s.DoC(nil)
}
func (s *CountService) DoC(ctx context.Context) (int64, error) {
// Check pre-conditions
if err := s.Validate(); err != nil {
return 0, err
}
// Get URL for request
path, params, err := s.buildURL()
if err != nil {
return 0, err
}
// Setup HTTP request body
var body interface{}
if s.query != nil {
src, err := s.query.Source()
if err != nil {
return 0, err
}
query := make(map[string]interface{})
query["query"] = src
body = query
} else if s.bodyJson != nil {
body = s.bodyJson
} else if s.bodyString != "" {
body = s.bodyString
}
// Get HTTP response
res, err := s.client.PerformRequestC(ctx, "POST", path, params, body)
if err != nil {
return 0, err
}
// Return result
ret := new(CountResponse)
if err := s.client.decoder.Decode(res.Body, ret); err != nil {
return 0, err
}
if ret != nil {
return ret.Count, nil
}
return int64(0), nil
}
// CountResponse is the response of using the Count API.
type CountResponse struct {
Count int64 `json:"count"`
Shards shardsInfo `json:"_shards,omitempty"`
}
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.48

搜索帮助