validate.go 7.6 KB

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