search_queries_regexp.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Copyright 2012-2015 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. // For more details, see
  7. // http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-regexp-query.html.
  8. type RegexpQuery struct {
  9. Query
  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 a new regexp query.
  19. func NewRegexpQuery(name string, regexp string) RegexpQuery {
  20. return RegexpQuery{name: name, regexp: regexp}
  21. }
  22. // Flags sets the regexp flags.
  23. // See http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-regexp-query.html#_optional_operators
  24. // for details.
  25. func (q RegexpQuery) Flags(flags string) RegexpQuery {
  26. q.flags = &flags
  27. return q
  28. }
  29. func (q RegexpQuery) MaxDeterminizedStates(maxDeterminizedStates int) RegexpQuery {
  30. q.maxDeterminizedStates = &maxDeterminizedStates
  31. return q
  32. }
  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. func (q RegexpQuery) QueryName(queryName string) RegexpQuery {
  42. q.queryName = &queryName
  43. return q
  44. }
  45. // Source returns the JSON-serializable query data.
  46. func (q RegexpQuery) Source() interface{} {
  47. // {
  48. // "regexp" : {
  49. // "name.first" : {
  50. // "value" : "s.*y",
  51. // "boost" : 1.2
  52. // }
  53. // }
  54. // }
  55. source := make(map[string]interface{})
  56. query := make(map[string]interface{})
  57. source["regexp"] = query
  58. x := make(map[string]interface{})
  59. x["value"] = q.regexp
  60. if q.flags != nil {
  61. x["flags"] = *q.flags
  62. }
  63. if q.maxDeterminizedStates != nil {
  64. x["max_determinized_states"] = *q.maxDeterminizedStates
  65. }
  66. if q.boost != nil {
  67. x["boost"] = *q.boost
  68. }
  69. if q.rewrite != nil {
  70. x["rewrite"] = *q.rewrite
  71. }
  72. if q.queryName != nil {
  73. x["name"] = *q.queryName
  74. }
  75. query[q.name] = x
  76. return source
  77. }