| 1 |
// Copyright 2012-present Oliver Eilhard. All rights reserved. |
| 2 |
// Use of this source code is governed by a MIT-license. |
| 3 |
// See http://olivere.mit-license.org/license.txt for details. |
| 4 |
|
| 5 |
package elastic |
| 6 |
|
| 7 |
import ( |
| 8 |
"context" |
| 9 |
"fmt" |
| 10 |
"net/url" |
| 11 |
"strings" |
| 12 |
|
| 13 |
"gopkg.in/olivere/elastic.v5/uritemplates" |
| 14 |
) |
| 15 |
|
| 16 |
// ValidateService allows a user to validate a potentially |
| 17 |
// expensive query without executing it. |
| 18 |
// See https://www.elastic.co/guide/en/elasticsearch/reference/5.6/search-validate.html. |
| 19 |
type ValidateService struct { |
| 20 |
client *Client |
| 21 |
pretty bool |
| 22 |
index []string |
| 23 |
typ []string |
| 24 |
q string |
| 25 |
explain *bool |
| 26 |
rewrite *bool |
| 27 |
allShards *bool |
| 28 |
lenient *bool |
| 29 |
analyzer string |
| 30 |
df string |
| 31 |
analyzeWildcard *bool |
| 32 |
defaultOperator string |
| 33 |
ignoreUnavailable *bool |
| 34 |
allowNoIndices *bool |
| 35 |
expandWildcards string |
| 36 |
bodyJson interface{} |
| 37 |
bodyString string |
| 38 |
} |
| 39 |
|
| 40 |
// NewValidateService creates a new ValidateService. |
| 41 |
func NewValidateService(client *Client) *ValidateService { |
| 42 |
return &ValidateService{ |
| 43 |
client: client, |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
// Index sets the names of the indices to use for search. |
| 48 |
func (s *ValidateService) Index(index ...string) *ValidateService { |
| 49 |
s.index = append(s.index, index...) |
| 50 |
return s |
| 51 |
} |
| 52 |
|
| 53 |
// Types adds search restrictions for a list of types. |
| 54 |
func (s *ValidateService) Type(typ ...string) *ValidateService { |
| 55 |
s.typ = append(s.typ, typ...) |
| 56 |
return s |
| 57 |
} |
| 58 |
|
| 59 |
// Lenient specifies whether format-based query failures |
| 60 |
// (such as providing text to a numeric field) should be ignored. |
| 61 |
func (s *ValidateService) Lenient(lenient bool) *ValidateService { |
| 62 |
s.lenient = &lenient |
| 63 |
return s |
| 64 |
} |
| 65 |
|
| 66 |
// Query in the Lucene query string syntax. |
| 67 |
func (s *ValidateService) Q(q string) *ValidateService { |
| 68 |
s.q = q |
| 69 |
return s |
| 70 |
} |
| 71 |
|
| 72 |
// An explain parameter can be specified to get more detailed information about why a query failed. |
| 73 |
func (s *ValidateService) Explain(explain *bool) *ValidateService { |
| 74 |
s.explain = explain |
| 75 |
return s |
| 76 |
} |
| 77 |
|
| 78 |
// Provide a more detailed explanation showing the actual Lucene query that will be executed. |
| 79 |
func (s *ValidateService) Rewrite(rewrite *bool) *ValidateService { |
| 80 |
s.rewrite = rewrite |
| 81 |
return s |
| 82 |
} |
| 83 |
|
| 84 |
// Execute validation on all shards instead of one random shard per index. |
| 85 |
func (s *ValidateService) AllShards(allShards *bool) *ValidateService { |
| 86 |
s.allShards = allShards |
| 87 |
return s |
| 88 |
} |
| 89 |
|
| 90 |
// AnalyzeWildcard specifies whether wildcards and prefix queries |
| 91 |
// in the query string query should be analyzed (default: false). |
| 92 |
func (s *ValidateService) AnalyzeWildcard(analyzeWildcard bool) *ValidateService { |
| 93 |
s.analyzeWildcard = &analyzeWildcard |
| 94 |
return s |
| 95 |
} |
| 96 |
|
| 97 |
// Analyzer is the analyzer for the query string query. |
| 98 |
func (s *ValidateService) Analyzer(analyzer string) *ValidateService { |
| 99 |
s.analyzer = analyzer |
| 100 |
return s |
| 101 |
} |
| 102 |
|
| 103 |
// Df is the default field for query string query (default: _all). |
| 104 |
func (s *ValidateService) Df(df string) *ValidateService { |
| 105 |
s.df = df |
| 106 |
return s |
| 107 |
} |
| 108 |
|
| 109 |
// DefaultOperator is the default operator for query string query (AND or OR). |
| 110 |
func (s *ValidateService) DefaultOperator(defaultOperator string) *ValidateService { |
| 111 |
s.defaultOperator = defaultOperator |
| 112 |
return s |
| 113 |
} |
| 114 |
|
| 115 |
// Pretty indicates that the JSON response be indented and human readable. |
| 116 |
func (s *ValidateService) Pretty(pretty bool) *ValidateService { |
| 117 |
s.pretty = pretty |
| 118 |
return s |
| 119 |
} |
| 120 |
|
| 121 |
// Query sets a query definition using the Query DSL. |
| 122 |
func (s *ValidateService) Query(query Query) *ValidateService { |
| 123 |
src, err := query.Source() |
| 124 |
if err != nil { |
| 125 |
// Do nothing in case of an error |
| 126 |
return s |
| 127 |
} |
| 128 |
body := make(map[string]interface{}) |
| 129 |
body["query"] = src |
| 130 |
s.bodyJson = body |
| 131 |
return s |
| 132 |
} |
| 133 |
|
| 134 |
// IgnoreUnavailable indicates whether the specified concrete indices |
| 135 |
// should be ignored when unavailable (missing or closed). |
| 136 |
func (s *ValidateService) IgnoreUnavailable(ignoreUnavailable bool) *ValidateService { |
| 137 |
s.ignoreUnavailable = &ignoreUnavailable |
| 138 |
return s |
| 139 |
} |
| 140 |
|
| 141 |
// AllowNoIndices indicates whether to ignore if a wildcard indices |
| 142 |
// expression resolves into no concrete indices. (This includes `_all` string |
| 143 |
// or when no indices have been specified). |
| 144 |
func (s *ValidateService) AllowNoIndices(allowNoIndices bool) *ValidateService { |
| 145 |
s.allowNoIndices = &allowNoIndices |
| 146 |
return s |
| 147 |
} |
| 148 |
|
| 149 |
// ExpandWildcards indicates whether to expand wildcard expression to |
| 150 |
// concrete indices that are open, closed or both. |
| 151 |
func (s *ValidateService) ExpandWildcards(expandWildcards string) *ValidateService { |
| 152 |
s.expandWildcards = expandWildcards |
| 153 |
return s |
| 154 |
} |
| 155 |
|
| 156 |
// BodyJson sets the query definition using the Query DSL. |
| 157 |
func (s *ValidateService) BodyJson(body interface{}) *ValidateService { |
| 158 |
s.bodyJson = body |
| 159 |
return s |
| 160 |
} |
| 161 |
|
| 162 |
// BodyString sets the query definition using the Query DSL as a string. |
| 163 |
func (s *ValidateService) BodyString(body string) *ValidateService { |
| 164 |
s.bodyString = body |
| 165 |
return s |
| 166 |
} |
| 167 |
|
| 168 |
// buildURL builds the URL for the operation. |
| 169 |
func (s *ValidateService) buildURL() (string, url.Values, error) { |
| 170 |
var err error |
| 171 |
var path string |
| 172 |
// Build URL |
| 173 |
if len(s.index) > 0 && len(s.typ) > 0 { |
| 174 |
path, err = uritemplates.Expand("/{index}/{type}/_validate/query", map[string]string{ |
| 175 |
"index": strings.Join(s.index, ","), |
| 176 |
"type": strings.Join(s.typ, ","), |
| 177 |
}) |
| 178 |
} else if len(s.index) > 0 { |
| 179 |
path, err = uritemplates.Expand("/{index}/_validate/query", map[string]string{ |
| 180 |
"index": strings.Join(s.index, ","), |
| 181 |
}) |
| 182 |
} else { |
| 183 |
path, err = uritemplates.Expand("/_validate/query", map[string]string{ |
| 184 |
"type": strings.Join(s.typ, ","), |
| 185 |
}) |
| 186 |
} |
| 187 |
if err != nil { |
| 188 |
return "", url.Values{}, err |
| 189 |
} |
| 190 |
|
| 191 |
// Add query string parameters |
| 192 |
params := url.Values{} |
| 193 |
if s.pretty { |
| 194 |
params.Set("pretty", "true") |
| 195 |
} |
| 196 |
if s.explain != nil { |
| 197 |
params.Set("explain", fmt.Sprintf("%v", *s.explain)) |
| 198 |
} |
| 199 |
if s.rewrite != nil { |
| 200 |
params.Set("rewrite", fmt.Sprintf("%v", *s.rewrite)) |
| 201 |
} |
| 202 |
if s.allShards != nil { |
| 203 |
params.Set("all_shards", fmt.Sprintf("%v", *s.allShards)) |
| 204 |
} |
| 205 |
if s.defaultOperator != "" { |
| 206 |
params.Set("default_operator", s.defaultOperator) |
| 207 |
} |
| 208 |
if s.lenient != nil { |
| 209 |
params.Set("lenient", fmt.Sprintf("%v", *s.lenient)) |
| 210 |
} |
| 211 |
if s.q != "" { |
| 212 |
params.Set("q", s.q) |
| 213 |
} |
| 214 |
if s.analyzeWildcard != nil { |
| 215 |
params.Set("analyze_wildcard", fmt.Sprintf("%v", *s.analyzeWildcard)) |
| 216 |
} |
| 217 |
if s.analyzer != "" { |
| 218 |
params.Set("analyzer", s.analyzer) |
| 219 |
} |
| 220 |
if s.df != "" { |
| 221 |
params.Set("df", s.df) |
| 222 |
} |
| 223 |
if s.allowNoIndices != nil { |
| 224 |
params.Set("allow_no_indices", fmt.Sprintf("%v", *s.allowNoIndices)) |
| 225 |
} |
| 226 |
if s.expandWildcards != "" { |
| 227 |
params.Set("expand_wildcards", s.expandWildcards) |
| 228 |
} |
| 229 |
if s.ignoreUnavailable != nil { |
| 230 |
params.Set("ignore_unavailable", fmt.Sprintf("%v", *s.ignoreUnavailable)) |
| 231 |
} |
| 232 |
return path, params, nil |
| 233 |
} |
| 234 |
|
| 235 |
// Validate checks if the operation is valid. |
| 236 |
func (s *ValidateService) Validate() error { |
| 237 |
return nil |
| 238 |
} |
| 239 |
|
| 240 |
// Do executes the operation. |
| 241 |
func (s *ValidateService) Do(ctx context.Context) (*ValidateResponse, error) { |
| 242 |
// Check pre-conditions |
| 243 |
if err := s.Validate(); err != nil { |
| 244 |
return nil, err |
| 245 |
} |
| 246 |
|
| 247 |
// Get URL for request |
| 248 |
path, params, err := s.buildURL() |
| 249 |
if err != nil { |
| 250 |
return nil, err |
| 251 |
} |
| 252 |
|
| 253 |
// Setup HTTP request body |
| 254 |
var body interface{} |
| 255 |
if s.bodyJson != nil { |
| 256 |
body = s.bodyJson |
| 257 |
} else { |
| 258 |
body = s.bodyString |
| 259 |
} |
| 260 |
|
| 261 |
// Get HTTP response |
| 262 |
res, err := s.client.PerformRequest(ctx, "GET", path, params, body) |
| 263 |
if err != nil { |
| 264 |
return nil, err |
| 265 |
} |
| 266 |
|
| 267 |
// Return operation response |
| 268 |
ret := new(ValidateResponse) |
| 269 |
if err := s.client.decoder.Decode(res.Body, ret); err != nil { |
| 270 |
return nil, err |
| 271 |
} |
| 272 |
return ret, nil |
| 273 |
} |
| 274 |
|
| 275 |
// ValidateResponse is the response of ValidateService.Do. |
| 276 |
type ValidateResponse struct { |
| 277 |
Valid bool `json:"valid"` |
| 278 |
Shards map[string]interface{} `json:"_shards"` |
| 279 |
Explanations []interface{} `json:"explanations"` |
| 280 |
} |