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.

81 lines
2.1 KiB

  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. // 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. // https://www.elastic.co/guide/en/elasticsearch/reference/7.0/query-dsl-wildcard-query.html
  15. type WildcardQuery struct {
  16. name string
  17. wildcard string
  18. boost *float64
  19. rewrite string
  20. queryName string
  21. }
  22. // NewWildcardQuery creates and initializes a new WildcardQuery.
  23. func NewWildcardQuery(name, wildcard string) *WildcardQuery {
  24. return &WildcardQuery{
  25. name: name,
  26. wildcard: wildcard,
  27. }
  28. }
  29. // Boost sets the boost for this query.
  30. func (q *WildcardQuery) Boost(boost float64) *WildcardQuery {
  31. q.boost = &boost
  32. return q
  33. }
  34. func (q *WildcardQuery) Rewrite(rewrite string) *WildcardQuery {
  35. q.rewrite = rewrite
  36. return q
  37. }
  38. // QueryName sets the name of this query.
  39. func (q *WildcardQuery) QueryName(queryName string) *WildcardQuery {
  40. q.queryName = queryName
  41. return q
  42. }
  43. // Source returns the JSON serializable body of this query.
  44. func (q *WildcardQuery) Source() (interface{}, error) {
  45. // {
  46. // "wildcard" : {
  47. // "user" : {
  48. // "wildcard" : "ki*y",
  49. // "boost" : 1.0
  50. // }
  51. // }
  52. source := make(map[string]interface{})
  53. query := make(map[string]interface{})
  54. source["wildcard"] = query
  55. wq := make(map[string]interface{})
  56. query[q.name] = wq
  57. wq["wildcard"] = q.wildcard
  58. if q.boost != nil {
  59. wq["boost"] = *q.boost
  60. }
  61. if q.rewrite != "" {
  62. wq["rewrite"] = q.rewrite
  63. }
  64. if q.queryName != "" {
  65. wq["_name"] = q.queryName
  66. }
  67. return source, nil
  68. }