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.
650 lines
20 KiB
650 lines
20 KiB
9 years ago
|
// 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"
|
||
|
|
||
8 years ago
|
"golang.org/x/net/context"
|
||
|
|
||
|
"gopkg.in/olivere/elastic.v5/uritemplates"
|
||
9 years ago
|
)
|
||
|
|
||
8 years ago
|
// DeleteByQueryService deletes documents that match a query.
|
||
|
// See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/docs-delete-by-query.html.
|
||
|
type DeleteByQueryService struct {
|
||
9 years ago
|
client *Client
|
||
|
index []string
|
||
|
typ []string
|
||
8 years ago
|
query Query
|
||
|
body interface{}
|
||
9 years ago
|
xSource []string
|
||
|
xSourceExclude []string
|
||
|
xSourceInclude []string
|
||
|
analyzer string
|
||
8 years ago
|
analyzeWildcard *bool
|
||
|
allowNoIndices *bool
|
||
9 years ago
|
conflicts string
|
||
|
defaultOperator string
|
||
|
df string
|
||
8 years ago
|
docvalueFields []string
|
||
9 years ago
|
expandWildcards string
|
||
|
explain *bool
|
||
|
from *int
|
||
|
ignoreUnavailable *bool
|
||
|
lenient *bool
|
||
|
lowercaseExpandedTerms *bool
|
||
|
preference string
|
||
|
q string
|
||
8 years ago
|
refresh string
|
||
9 years ago
|
requestCache *bool
|
||
8 years ago
|
requestsPerSecond *int
|
||
9 years ago
|
routing []string
|
||
|
scroll string
|
||
|
scrollSize *int
|
||
|
searchTimeout string
|
||
|
searchType string
|
||
|
size *int
|
||
|
sort []string
|
||
|
stats []string
|
||
8 years ago
|
storedFields []string
|
||
9 years ago
|
suggestField string
|
||
|
suggestMode string
|
||
|
suggestSize *int
|
||
|
suggestText string
|
||
|
terminateAfter *int
|
||
|
timeout string
|
||
|
trackScores *bool
|
||
|
version *bool
|
||
8 years ago
|
waitForActiveShards string
|
||
9 years ago
|
waitForCompletion *bool
|
||
8 years ago
|
pretty bool
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
// NewDeleteByQueryService creates a new DeleteByQueryService.
|
||
|
// You typically use the client's DeleteByQuery to get a reference to
|
||
|
// the service.
|
||
|
func NewDeleteByQueryService(client *Client) *DeleteByQueryService {
|
||
|
builder := &DeleteByQueryService{
|
||
|
client: client,
|
||
9 years ago
|
}
|
||
8 years ago
|
return builder
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
// Index sets the indices on which to perform the delete operation.
|
||
|
func (s *DeleteByQueryService) Index(index ...string) *DeleteByQueryService {
|
||
|
s.index = append(s.index, index...)
|
||
9 years ago
|
return s
|
||
|
}
|
||
|
|
||
8 years ago
|
// Type limits the delete operation to the given types.
|
||
|
func (s *DeleteByQueryService) Type(typ ...string) *DeleteByQueryService {
|
||
|
s.typ = append(s.typ, typ...)
|
||
9 years ago
|
return s
|
||
|
}
|
||
|
|
||
|
// XSource is true or false to return the _source field or not,
|
||
|
// or a list of fields to return.
|
||
8 years ago
|
func (s *DeleteByQueryService) XSource(xSource ...string) *DeleteByQueryService {
|
||
9 years ago
|
s.xSource = append(s.xSource, xSource...)
|
||
|
return s
|
||
|
}
|
||
|
|
||
|
// XSourceExclude represents a list of fields to exclude from the returned _source field.
|
||
8 years ago
|
func (s *DeleteByQueryService) XSourceExclude(xSourceExclude ...string) *DeleteByQueryService {
|
||
9 years ago
|
s.xSourceExclude = append(s.xSourceExclude, xSourceExclude...)
|
||
|
return s
|
||
|
}
|
||
|
|
||
|
// XSourceInclude represents a list of fields to extract and return from the _source field.
|
||
8 years ago
|
func (s *DeleteByQueryService) XSourceInclude(xSourceInclude ...string) *DeleteByQueryService {
|
||
9 years ago
|
s.xSourceInclude = append(s.xSourceInclude, xSourceInclude...)
|
||
|
return s
|
||
|
}
|
||
|
|
||
8 years ago
|
// Analyzer to use for the query string.
|
||
|
func (s *DeleteByQueryService) Analyzer(analyzer string) *DeleteByQueryService {
|
||
|
s.analyzer = analyzer
|
||
9 years ago
|
return s
|
||
|
}
|
||
|
|
||
|
// AnalyzeWildcard specifies whether wildcard and prefix queries should be
|
||
|
// analyzed (default: false).
|
||
8 years ago
|
func (s *DeleteByQueryService) AnalyzeWildcard(analyzeWildcard bool) *DeleteByQueryService {
|
||
9 years ago
|
s.analyzeWildcard = &analyzeWildcard
|
||
|
return s
|
||
|
}
|
||
|
|
||
8 years ago
|
// AllowNoIndices indicates whether to ignore if a wildcard indices
|
||
|
// expression resolves into no concrete indices (including the _all string
|
||
|
// or when no indices have been specified).
|
||
|
func (s *DeleteByQueryService) AllowNoIndices(allow bool) *DeleteByQueryService {
|
||
|
s.allowNoIndices = &allow
|
||
9 years ago
|
return s
|
||
|
}
|
||
|
|
||
|
// Conflicts indicates what to do when the process detects version conflicts.
|
||
|
// Possible values are "proceed" and "abort".
|
||
8 years ago
|
func (s *DeleteByQueryService) Conflicts(conflicts string) *DeleteByQueryService {
|
||
9 years ago
|
s.conflicts = conflicts
|
||
|
return s
|
||
|
}
|
||
|
|
||
|
// AbortOnVersionConflict aborts the request on version conflicts.
|
||
|
// It is an alias to setting Conflicts("abort").
|
||
8 years ago
|
func (s *DeleteByQueryService) AbortOnVersionConflict() *DeleteByQueryService {
|
||
9 years ago
|
s.conflicts = "abort"
|
||
|
return s
|
||
|
}
|
||
|
|
||
|
// ProceedOnVersionConflict aborts the request on version conflicts.
|
||
|
// It is an alias to setting Conflicts("proceed").
|
||
8 years ago
|
func (s *DeleteByQueryService) ProceedOnVersionConflict() *DeleteByQueryService {
|
||
9 years ago
|
s.conflicts = "proceed"
|
||
|
return s
|
||
|
}
|
||
|
|
||
8 years ago
|
// DefaultOperator for query string query (AND or OR).
|
||
|
func (s *DeleteByQueryService) DefaultOperator(defaultOperator string) *DeleteByQueryService {
|
||
9 years ago
|
s.defaultOperator = defaultOperator
|
||
|
return s
|
||
|
}
|
||
|
|
||
8 years ago
|
// DF is the field to use as default where no field prefix is given in the query string.
|
||
|
func (s *DeleteByQueryService) DF(defaultField string) *DeleteByQueryService {
|
||
|
s.df = defaultField
|
||
9 years ago
|
return s
|
||
|
}
|
||
|
|
||
8 years ago
|
// DefaultField is the field to use as default where no field prefix is given in the query string.
|
||
|
// It is an alias to the DF func.
|
||
|
func (s *DeleteByQueryService) DefaultField(defaultField string) *DeleteByQueryService {
|
||
|
s.df = defaultField
|
||
9 years ago
|
return s
|
||
|
}
|
||
|
|
||
8 years ago
|
// DocvalueFields specifies the list of fields to return as the docvalue representation of a field for each hit.
|
||
|
func (s *DeleteByQueryService) DocvalueFields(docvalueFields ...string) *DeleteByQueryService {
|
||
|
s.docvalueFields = docvalueFields
|
||
9 years ago
|
return s
|
||
|
}
|
||
|
|
||
8 years ago
|
// ExpandWildcards indicates whether to expand wildcard expression to
|
||
|
// concrete indices that are open, closed or both. It can be "open" or "closed".
|
||
|
func (s *DeleteByQueryService) ExpandWildcards(expand string) *DeleteByQueryService {
|
||
|
s.expandWildcards = expand
|
||
9 years ago
|
return s
|
||
|
}
|
||
|
|
||
8 years ago
|
// Explain specifies whether to return detailed information about score
|
||
|
// computation as part of a hit.
|
||
|
func (s *DeleteByQueryService) Explain(explain bool) *DeleteByQueryService {
|
||
|
s.explain = &explain
|
||
9 years ago
|
return s
|
||
|
}
|
||
|
|
||
|
// From is the starting offset (default: 0).
|
||
8 years ago
|
func (s *DeleteByQueryService) From(from int) *DeleteByQueryService {
|
||
9 years ago
|
s.from = &from
|
||
|
return s
|
||
|
}
|
||
|
|
||
|
// IgnoreUnavailable indicates whether specified concrete indices should be
|
||
|
// ignored when unavailable (missing or closed).
|
||
8 years ago
|
func (s *DeleteByQueryService) IgnoreUnavailable(ignore bool) *DeleteByQueryService {
|
||
|
s.ignoreUnavailable = &ignore
|
||
9 years ago
|
return s
|
||
|
}
|
||
|
|
||
|
// Lenient specifies whether format-based query failures
|
||
|
// (such as providing text to a numeric field) should be ignored.
|
||
8 years ago
|
func (s *DeleteByQueryService) Lenient(lenient bool) *DeleteByQueryService {
|
||
9 years ago
|
s.lenient = &lenient
|
||
|
return s
|
||
|
}
|
||
|
|
||
|
// LowercaseExpandedTerms specifies whether query terms should be lowercased.
|
||
8 years ago
|
func (s *DeleteByQueryService) LowercaseExpandedTerms(lowercaseExpandedTerms bool) *DeleteByQueryService {
|
||
9 years ago
|
s.lowercaseExpandedTerms = &lowercaseExpandedTerms
|
||
|
return s
|
||
|
}
|
||
|
|
||
|
// Preference specifies the node or shard the operation should be performed on
|
||
|
// (default: random).
|
||
8 years ago
|
func (s *DeleteByQueryService) Preference(preference string) *DeleteByQueryService {
|
||
9 years ago
|
s.preference = preference
|
||
|
return s
|
||
|
}
|
||
|
|
||
8 years ago
|
// Q specifies the query in Lucene query string syntax. You can also use
|
||
|
// Query to programmatically specify the query.
|
||
|
func (s *DeleteByQueryService) Q(query string) *DeleteByQueryService {
|
||
|
s.q = query
|
||
|
return s
|
||
|
}
|
||
|
|
||
|
// QueryString is an alias to Q. Notice that you can also use Query to
|
||
|
// programmatically set the query.
|
||
|
func (s *DeleteByQueryService) QueryString(query string) *DeleteByQueryService {
|
||
|
s.q = query
|
||
|
return s
|
||
|
}
|
||
|
|
||
|
// Query sets the query programmatically.
|
||
|
func (s *DeleteByQueryService) Query(query Query) *DeleteByQueryService {
|
||
|
s.query = query
|
||
9 years ago
|
return s
|
||
|
}
|
||
|
|
||
|
// Refresh indicates whether the effected indexes should be refreshed.
|
||
8 years ago
|
func (s *DeleteByQueryService) Refresh(refresh string) *DeleteByQueryService {
|
||
|
s.refresh = refresh
|
||
9 years ago
|
return s
|
||
|
}
|
||
|
|
||
|
// RequestCache specifies if request cache should be used for this request
|
||
|
// or not, defaults to index level setting.
|
||
8 years ago
|
func (s *DeleteByQueryService) RequestCache(requestCache bool) *DeleteByQueryService {
|
||
9 years ago
|
s.requestCache = &requestCache
|
||
|
return s
|
||
|
}
|
||
|
|
||
8 years ago
|
// RequestsPerSecond sets the throttle on this request in sub-requests per second.
|
||
|
// -1 means set no throttle as does "unlimited" which is the only non-float this accepts.
|
||
|
func (s *DeleteByQueryService) RequestsPerSecond(requestsPerSecond int) *DeleteByQueryService {
|
||
|
s.requestsPerSecond = &requestsPerSecond
|
||
|
return s
|
||
|
}
|
||
|
|
||
9 years ago
|
// Routing is a list of specific routing values.
|
||
8 years ago
|
func (s *DeleteByQueryService) Routing(routing ...string) *DeleteByQueryService {
|
||
9 years ago
|
s.routing = append(s.routing, routing...)
|
||
|
return s
|
||
|
}
|
||
|
|
||
|
// Scroll specifies how long a consistent view of the index should be maintained
|
||
|
// for scrolled search.
|
||
8 years ago
|
func (s *DeleteByQueryService) Scroll(scroll string) *DeleteByQueryService {
|
||
9 years ago
|
s.scroll = scroll
|
||
|
return s
|
||
|
}
|
||
|
|
||
|
// ScrollSize is the size on the scroll request powering the update_by_query.
|
||
8 years ago
|
func (s *DeleteByQueryService) ScrollSize(scrollSize int) *DeleteByQueryService {
|
||
9 years ago
|
s.scrollSize = &scrollSize
|
||
|
return s
|
||
|
}
|
||
|
|
||
|
// SearchTimeout defines an explicit timeout for each search request.
|
||
|
// Defaults to no timeout.
|
||
8 years ago
|
func (s *DeleteByQueryService) SearchTimeout(searchTimeout string) *DeleteByQueryService {
|
||
9 years ago
|
s.searchTimeout = searchTimeout
|
||
|
return s
|
||
|
}
|
||
|
|
||
|
// SearchType is the search operation type. Possible values are
|
||
|
// "query_then_fetch" and "dfs_query_then_fetch".
|
||
8 years ago
|
func (s *DeleteByQueryService) SearchType(searchType string) *DeleteByQueryService {
|
||
9 years ago
|
s.searchType = searchType
|
||
|
return s
|
||
|
}
|
||
|
|
||
|
// Size represents the number of hits to return (default: 10).
|
||
8 years ago
|
func (s *DeleteByQueryService) Size(size int) *DeleteByQueryService {
|
||
9 years ago
|
s.size = &size
|
||
|
return s
|
||
|
}
|
||
|
|
||
|
// Sort is a list of <field>:<direction> pairs.
|
||
8 years ago
|
func (s *DeleteByQueryService) Sort(sort ...string) *DeleteByQueryService {
|
||
9 years ago
|
s.sort = append(s.sort, sort...)
|
||
|
return s
|
||
|
}
|
||
|
|
||
|
// SortByField adds a sort order.
|
||
8 years ago
|
func (s *DeleteByQueryService) SortByField(field string, ascending bool) *DeleteByQueryService {
|
||
9 years ago
|
if ascending {
|
||
|
s.sort = append(s.sort, fmt.Sprintf("%s:asc", field))
|
||
|
} else {
|
||
|
s.sort = append(s.sort, fmt.Sprintf("%s:desc", field))
|
||
|
}
|
||
|
return s
|
||
|
}
|
||
|
|
||
|
// Stats specifies specific tag(s) of the request for logging and statistical purposes.
|
||
8 years ago
|
func (s *DeleteByQueryService) Stats(stats ...string) *DeleteByQueryService {
|
||
9 years ago
|
s.stats = append(s.stats, stats...)
|
||
|
return s
|
||
|
}
|
||
|
|
||
8 years ago
|
// StoredFields specifies the list of stored fields to return as part of a hit.
|
||
|
func (s *DeleteByQueryService) StoredFields(storedFields ...string) *DeleteByQueryService {
|
||
|
s.storedFields = storedFields
|
||
|
return s
|
||
|
}
|
||
|
|
||
9 years ago
|
// SuggestField specifies which field to use for suggestions.
|
||
8 years ago
|
func (s *DeleteByQueryService) SuggestField(suggestField string) *DeleteByQueryService {
|
||
9 years ago
|
s.suggestField = suggestField
|
||
|
return s
|
||
|
}
|
||
|
|
||
|
// SuggestMode specifies the suggest mode. Possible values are
|
||
|
// "missing", "popular", and "always".
|
||
8 years ago
|
func (s *DeleteByQueryService) SuggestMode(suggestMode string) *DeleteByQueryService {
|
||
9 years ago
|
s.suggestMode = suggestMode
|
||
|
return s
|
||
|
}
|
||
|
|
||
|
// SuggestSize specifies how many suggestions to return in response.
|
||
8 years ago
|
func (s *DeleteByQueryService) SuggestSize(suggestSize int) *DeleteByQueryService {
|
||
9 years ago
|
s.suggestSize = &suggestSize
|
||
|
return s
|
||
|
}
|
||
|
|
||
|
// SuggestText specifies the source text for which the suggestions should be returned.
|
||
8 years ago
|
func (s *DeleteByQueryService) SuggestText(suggestText string) *DeleteByQueryService {
|
||
9 years ago
|
s.suggestText = suggestText
|
||
|
return s
|
||
|
}
|
||
|
|
||
|
// TerminateAfter indicates the maximum number of documents to collect
|
||
|
// for each shard, upon reaching which the query execution will terminate early.
|
||
8 years ago
|
func (s *DeleteByQueryService) TerminateAfter(terminateAfter int) *DeleteByQueryService {
|
||
9 years ago
|
s.terminateAfter = &terminateAfter
|
||
|
return s
|
||
|
}
|
||
|
|
||
|
// Timeout is the time each individual bulk request should wait for shards
|
||
|
// that are unavailable.
|
||
8 years ago
|
func (s *DeleteByQueryService) Timeout(timeout string) *DeleteByQueryService {
|
||
9 years ago
|
s.timeout = timeout
|
||
|
return s
|
||
|
}
|
||
|
|
||
|
// TimeoutInMillis sets the timeout in milliseconds.
|
||
8 years ago
|
func (s *DeleteByQueryService) TimeoutInMillis(timeoutInMillis int) *DeleteByQueryService {
|
||
9 years ago
|
s.timeout = fmt.Sprintf("%dms", timeoutInMillis)
|
||
|
return s
|
||
|
}
|
||
|
|
||
|
// TrackScores indicates whether to calculate and return scores even if
|
||
|
// they are not used for sorting.
|
||
8 years ago
|
func (s *DeleteByQueryService) TrackScores(trackScores bool) *DeleteByQueryService {
|
||
9 years ago
|
s.trackScores = &trackScores
|
||
|
return s
|
||
|
}
|
||
|
|
||
|
// Version specifies whether to return document version as part of a hit.
|
||
8 years ago
|
func (s *DeleteByQueryService) Version(version bool) *DeleteByQueryService {
|
||
9 years ago
|
s.version = &version
|
||
|
return s
|
||
|
}
|
||
|
|
||
8 years ago
|
// WaitForActiveShards sets the number of shard copies that must be active before proceeding
|
||
|
// with the update by query operation. Defaults to 1, meaning the primary shard only.
|
||
|
// Set to `all` for all shard copies, otherwise set to any non-negative value less than or equal
|
||
|
// to the total number of copies for the shard (number of replicas + 1).
|
||
|
func (s *DeleteByQueryService) WaitForActiveShards(waitForActiveShards string) *DeleteByQueryService {
|
||
|
s.waitForActiveShards = waitForActiveShards
|
||
9 years ago
|
return s
|
||
|
}
|
||
|
|
||
|
// WaitForCompletion indicates if the request should block until the reindex is complete.
|
||
8 years ago
|
func (s *DeleteByQueryService) WaitForCompletion(waitForCompletion bool) *DeleteByQueryService {
|
||
9 years ago
|
s.waitForCompletion = &waitForCompletion
|
||
|
return s
|
||
|
}
|
||
|
|
||
8 years ago
|
// Pretty indents the JSON output from Elasticsearch.
|
||
|
func (s *DeleteByQueryService) Pretty(pretty bool) *DeleteByQueryService {
|
||
9 years ago
|
s.pretty = pretty
|
||
|
return s
|
||
|
}
|
||
|
|
||
8 years ago
|
// Body specifies the body of the request. It overrides data being specified via SearchService.
|
||
|
func (s *DeleteByQueryService) Body(body string) *DeleteByQueryService {
|
||
|
s.body = body
|
||
9 years ago
|
return s
|
||
|
}
|
||
|
|
||
|
// buildURL builds the URL for the operation.
|
||
8 years ago
|
func (s *DeleteByQueryService) buildURL() (string, url.Values, error) {
|
||
9 years ago
|
// Build URL
|
||
|
var err error
|
||
|
var path string
|
||
8 years ago
|
if len(s.typ) > 0 {
|
||
|
path, err = uritemplates.Expand("/{index}/{type}/_delete_by_query", map[string]string{
|
||
9 years ago
|
"index": strings.Join(s.index, ","),
|
||
|
"type": strings.Join(s.typ, ","),
|
||
|
})
|
||
8 years ago
|
} else {
|
||
|
path, err = uritemplates.Expand("/{index}/_delete_by_query", map[string]string{
|
||
9 years ago
|
"index": strings.Join(s.index, ","),
|
||
|
})
|
||
|
}
|
||
|
if err != nil {
|
||
|
return "", url.Values{}, err
|
||
|
}
|
||
|
|
||
|
// Add query string parameters
|
||
|
params := url.Values{}
|
||
|
if len(s.xSource) > 0 {
|
||
|
params.Set("_source", strings.Join(s.xSource, ","))
|
||
|
}
|
||
|
if len(s.xSourceExclude) > 0 {
|
||
|
params.Set("_source_exclude", strings.Join(s.xSourceExclude, ","))
|
||
|
}
|
||
|
if len(s.xSourceInclude) > 0 {
|
||
|
params.Set("_source_include", strings.Join(s.xSourceInclude, ","))
|
||
|
}
|
||
|
if s.analyzer != "" {
|
||
|
params.Set("analyzer", s.analyzer)
|
||
|
}
|
||
8 years ago
|
if s.analyzeWildcard != nil {
|
||
|
params.Set("analyze_wildcard", fmt.Sprintf("%v", *s.analyzeWildcard))
|
||
9 years ago
|
}
|
||
|
if s.defaultOperator != "" {
|
||
|
params.Set("default_operator", s.defaultOperator)
|
||
|
}
|
||
|
if s.df != "" {
|
||
|
params.Set("df", s.df)
|
||
|
}
|
||
|
if s.explain != nil {
|
||
|
params.Set("explain", fmt.Sprintf("%v", *s.explain))
|
||
|
}
|
||
8 years ago
|
if len(s.storedFields) > 0 {
|
||
|
params.Set("stored_fields", strings.Join(s.storedFields, ","))
|
||
9 years ago
|
}
|
||
8 years ago
|
if len(s.docvalueFields) > 0 {
|
||
|
params.Set("docvalue_fields", strings.Join(s.docvalueFields, ","))
|
||
9 years ago
|
}
|
||
|
if s.from != nil {
|
||
|
params.Set("from", fmt.Sprintf("%d", *s.from))
|
||
|
}
|
||
|
if s.ignoreUnavailable != nil {
|
||
|
params.Set("ignore_unavailable", fmt.Sprintf("%v", *s.ignoreUnavailable))
|
||
|
}
|
||
8 years ago
|
if s.allowNoIndices != nil {
|
||
|
params.Set("allow_no_indices", fmt.Sprintf("%v", *s.allowNoIndices))
|
||
|
}
|
||
|
if s.conflicts != "" {
|
||
|
params.Set("conflicts", s.conflicts)
|
||
|
}
|
||
|
if s.expandWildcards != "" {
|
||
|
params.Set("expand_wildcards", s.expandWildcards)
|
||
|
}
|
||
9 years ago
|
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.preference != "" {
|
||
|
params.Set("preference", s.preference)
|
||
|
}
|
||
|
if s.q != "" {
|
||
|
params.Set("q", s.q)
|
||
|
}
|
||
|
if len(s.routing) > 0 {
|
||
|
params.Set("routing", strings.Join(s.routing, ","))
|
||
|
}
|
||
|
if s.scroll != "" {
|
||
|
params.Set("scroll", s.scroll)
|
||
|
}
|
||
8 years ago
|
if s.searchType != "" {
|
||
|
params.Set("search_type", s.searchType)
|
||
9 years ago
|
}
|
||
|
if s.searchTimeout != "" {
|
||
|
params.Set("search_timeout", s.searchTimeout)
|
||
|
}
|
||
|
if s.size != nil {
|
||
|
params.Set("size", fmt.Sprintf("%d", *s.size))
|
||
|
}
|
||
|
if len(s.sort) > 0 {
|
||
|
params.Set("sort", strings.Join(s.sort, ","))
|
||
|
}
|
||
8 years ago
|
if s.terminateAfter != nil {
|
||
|
params.Set("terminate_after", fmt.Sprintf("%v", *s.terminateAfter))
|
||
|
}
|
||
9 years ago
|
if len(s.stats) > 0 {
|
||
|
params.Set("stats", strings.Join(s.stats, ","))
|
||
|
}
|
||
|
if s.suggestField != "" {
|
||
|
params.Set("suggest_field", s.suggestField)
|
||
|
}
|
||
|
if s.suggestMode != "" {
|
||
|
params.Set("suggest_mode", s.suggestMode)
|
||
|
}
|
||
|
if s.suggestSize != nil {
|
||
|
params.Set("suggest_size", fmt.Sprintf("%v", *s.suggestSize))
|
||
|
}
|
||
|
if s.suggestText != "" {
|
||
|
params.Set("suggest_text", s.suggestText)
|
||
|
}
|
||
|
if s.timeout != "" {
|
||
|
params.Set("timeout", s.timeout)
|
||
|
}
|
||
|
if s.trackScores != nil {
|
||
|
params.Set("track_scores", fmt.Sprintf("%v", *s.trackScores))
|
||
|
}
|
||
|
if s.version != nil {
|
||
|
params.Set("version", fmt.Sprintf("%v", *s.version))
|
||
|
}
|
||
8 years ago
|
if s.requestCache != nil {
|
||
|
params.Set("request_cache", fmt.Sprintf("%v", *s.requestCache))
|
||
|
}
|
||
|
if s.refresh != "" {
|
||
|
params.Set("refresh", s.refresh)
|
||
|
}
|
||
|
if s.waitForActiveShards != "" {
|
||
|
params.Set("wait_for_active_shards", s.waitForActiveShards)
|
||
|
}
|
||
|
if s.scrollSize != nil {
|
||
|
params.Set("scroll_size", fmt.Sprintf("%d", *s.scrollSize))
|
||
9 years ago
|
}
|
||
|
if s.waitForCompletion != nil {
|
||
|
params.Set("wait_for_completion", fmt.Sprintf("%v", *s.waitForCompletion))
|
||
|
}
|
||
8 years ago
|
if s.requestsPerSecond != nil {
|
||
|
params.Set("requests_per_second", fmt.Sprintf("%v", *s.requestsPerSecond))
|
||
|
}
|
||
|
if s.pretty {
|
||
|
params.Set("pretty", fmt.Sprintf("%v", s.pretty))
|
||
|
}
|
||
9 years ago
|
return path, params, nil
|
||
|
}
|
||
|
|
||
|
// Validate checks if the operation is valid.
|
||
8 years ago
|
func (s *DeleteByQueryService) Validate() error {
|
||
|
var invalid []string
|
||
|
if len(s.index) == 0 {
|
||
|
invalid = append(invalid, "Index")
|
||
9 years ago
|
}
|
||
8 years ago
|
if len(invalid) > 0 {
|
||
|
return fmt.Errorf("missing required fields: %v", invalid)
|
||
9 years ago
|
}
|
||
8 years ago
|
return nil
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
// Do executes the delete-by-query operation.
|
||
|
func (s *DeleteByQueryService) Do(ctx context.Context) (*BulkIndexByScrollResponse, error) {
|
||
9 years ago
|
// Check pre-conditions
|
||
|
if err := s.Validate(); err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
// Get URL for request
|
||
|
path, params, err := s.buildURL()
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
8 years ago
|
// Set body if there is a query set
|
||
|
var body interface{}
|
||
|
if s.body != nil {
|
||
|
body = s.body
|
||
|
} else if s.query != nil {
|
||
|
src, err := s.query.Source()
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
body = map[string]interface{}{
|
||
|
"query": src,
|
||
|
}
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
// Get response
|
||
|
res, err := s.client.PerformRequest(ctx, "POST", path, params, body)
|
||
9 years ago
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
8 years ago
|
// Return result
|
||
|
ret := new(BulkIndexByScrollResponse)
|
||
9 years ago
|
if err := s.client.decoder.Decode(res.Body, ret); err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
return ret, nil
|
||
|
}
|
||
|
|
||
8 years ago
|
// BulkIndexByScrollResponse is the outcome of executing Do with
|
||
|
// DeleteByQueryService and UpdateByQueryService.
|
||
|
type BulkIndexByScrollResponse struct {
|
||
|
Took int64 `json:"took"`
|
||
|
TimedOut bool `json:"timed_out"`
|
||
|
Total int64 `json:"total"`
|
||
|
Updated int64 `json:"updated"`
|
||
|
Created int64 `json:"created"`
|
||
|
Deleted int64 `json:"deleted"`
|
||
|
Batches int64 `json:"batches"`
|
||
|
VersionConflicts int64 `json:"version_conflicts"`
|
||
|
Noops int64 `json:"noops"`
|
||
|
Retries struct {
|
||
|
Bulk int64 `json:"bulk"`
|
||
|
Search int64 `json:"search"`
|
||
|
} `json:"retries"`
|
||
|
Throttled string `json:"throttled"`
|
||
|
ThrottledMillis int64 `json:"throttled_millis"`
|
||
|
RequestsPerSecond float64 `json:"requests_per_second"`
|
||
|
Canceled string `json:"canceled"`
|
||
|
ThrottledUntil string `json:"throttled_until"`
|
||
|
ThrottledUntilMillis int64 `json:"throttled_until_millis"`
|
||
|
Failures []bulkIndexByScrollResponseFailure `json:"failures"`
|
||
|
}
|
||
|
|
||
|
type bulkIndexByScrollResponseFailure struct {
|
||
|
Index string `json:"index,omitempty"`
|
||
|
Type string `json:"type,omitempty"`
|
||
|
Id string `json:"id,omitempty"`
|
||
|
Status int `json:"status,omitempty"`
|
||
|
Shard int `json:"shard,omitempty"`
|
||
|
Node int `json:"node,omitempty"`
|
||
|
// TOOD "cause" contains exception details
|
||
|
// TOOD "reason" contains exception details
|
||
9 years ago
|
}
|