Test
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.

search_queries_regexp.go 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. // RegexpQuery allows you to use regular expression term queries.
  6. //
  7. // For more details, see
  8. // https://www.elastic.co/guide/en/elasticsearch/reference/6.2/query-dsl-regexp-query.html
  9. type RegexpQuery struct {
  10. name string
  11. regexp string
  12. flags string
  13. boost *float64
  14. rewrite string
  15. queryName string
  16. maxDeterminizedStates *int
  17. }
  18. // NewRegexpQuery creates and initializes a new RegexpQuery.
  19. func NewRegexpQuery(name string, regexp string) *RegexpQuery {
  20. return &RegexpQuery{name: name, regexp: regexp}
  21. }
  22. // Flags sets the regexp flags.
  23. func (q *RegexpQuery) Flags(flags string) *RegexpQuery {
  24. q.flags = flags
  25. return q
  26. }
  27. // MaxDeterminizedStates protects against complex regular expressions.
  28. func (q *RegexpQuery) MaxDeterminizedStates(maxDeterminizedStates int) *RegexpQuery {
  29. q.maxDeterminizedStates = &maxDeterminizedStates
  30. return q
  31. }
  32. // Boost sets the boost for this query.
  33. func (q *RegexpQuery) Boost(boost float64) *RegexpQuery {
  34. q.boost = &boost
  35. return q
  36. }
  37. func (q *RegexpQuery) Rewrite(rewrite string) *RegexpQuery {
  38. q.rewrite = rewrite
  39. return q
  40. }
  41. // QueryName sets the query name for the filter that can be used
  42. // when searching for matched_filters per hit
  43. func (q *RegexpQuery) QueryName(queryName string) *RegexpQuery {
  44. q.queryName = queryName
  45. return q
  46. }
  47. // Source returns the JSON-serializable query data.
  48. func (q *RegexpQuery) Source() (interface{}, error) {
  49. source := make(map[string]interface{})
  50. query := make(map[string]interface{})
  51. source["regexp"] = query
  52. x := make(map[string]interface{})
  53. x["value"] = q.regexp
  54. if q.flags != "" {
  55. x["flags"] = q.flags
  56. }
  57. if q.maxDeterminizedStates != nil {
  58. x["max_determinized_states"] = *q.maxDeterminizedStates
  59. }
  60. if q.boost != nil {
  61. x["boost"] = *q.boost
  62. }
  63. if q.rewrite != "" {
  64. x["rewrite"] = q.rewrite
  65. }
  66. if q.queryName != "" {
  67. x["name"] = q.queryName
  68. }
  69. query[q.name] = x
  70. return source, nil
  71. }