search_queries_wildcard.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. // WildcardQuery matches documents that have fields matching a wildcard
  6. // expression (not analyzed). Supported wildcards are *, which matches
  7. // any character sequence (including the empty one), and ?, which matches
  8. // any single character. Note this query can be slow, as it needs to iterate
  9. // over many terms. In order to prevent extremely slow wildcard queries,
  10. // a wildcard term should not start with one of the wildcards * or ?.
  11. // The wildcard query maps to Lucene WildcardQuery.
  12. //
  13. // For more details, see
  14. // http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-wildcard-query.html.
  15. type WildcardQuery struct {
  16. Query
  17. name string
  18. wildcard string
  19. boost float32
  20. rewrite string
  21. queryName string
  22. }
  23. // NewWildcardQuery creates a new wildcard query.
  24. func NewWildcardQuery(name, wildcard string) WildcardQuery {
  25. q := WildcardQuery{
  26. name: name,
  27. wildcard: wildcard,
  28. boost: -1.0,
  29. }
  30. return q
  31. }
  32. // Name is the name of the field name.
  33. func (q WildcardQuery) Name(name string) WildcardQuery {
  34. q.name = name
  35. return q
  36. }
  37. // Wildcard is the wildcard to be used in the query, e.g. ki*y??.
  38. func (q WildcardQuery) Wildcard(wildcard string) WildcardQuery {
  39. q.wildcard = wildcard
  40. return q
  41. }
  42. // Boost sets the boost for this query.
  43. func (q WildcardQuery) Boost(boost float32) WildcardQuery {
  44. q.boost = boost
  45. return q
  46. }
  47. // Rewrite controls the rewriting.
  48. // See http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-multi-term-rewrite.html
  49. // for details.
  50. func (q WildcardQuery) Rewrite(rewrite string) WildcardQuery {
  51. q.rewrite = rewrite
  52. return q
  53. }
  54. // QueryName sets the name of this query.
  55. func (q WildcardQuery) QueryName(queryName string) WildcardQuery {
  56. q.queryName = queryName
  57. return q
  58. }
  59. // Source returns the JSON serializable body of this query.
  60. func (q WildcardQuery) Source() interface{} {
  61. // {
  62. // "wildcard" : {
  63. // "user" : {
  64. // "wildcard" : "ki*y",
  65. // "boost" : 1.0
  66. // }
  67. // }
  68. source := make(map[string]interface{})
  69. query := make(map[string]interface{})
  70. source["wildcard"] = query
  71. wq := make(map[string]interface{})
  72. query[q.name] = wq
  73. wq["wildcard"] = q.wildcard
  74. if q.boost != -1.0 {
  75. wq["boost"] = q.boost
  76. }
  77. if q.rewrite != "" {
  78. wq["rewrite"] = q.rewrite
  79. }
  80. if q.queryName != "" {
  81. wq["_name"] = q.queryName
  82. }
  83. return source
  84. }