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.

wildcard.go 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Copyright (c) 2014 Couchbase, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package query
  15. import (
  16. "regexp"
  17. "strings"
  18. "github.com/blevesearch/bleve/index"
  19. "github.com/blevesearch/bleve/mapping"
  20. "github.com/blevesearch/bleve/search"
  21. "github.com/blevesearch/bleve/search/searcher"
  22. )
  23. var wildcardRegexpReplacer = strings.NewReplacer(
  24. // characters in the wildcard that must
  25. // be escaped in the regexp
  26. "+", `\+`,
  27. "(", `\(`,
  28. ")", `\)`,
  29. "^", `\^`,
  30. "$", `\$`,
  31. ".", `\.`,
  32. "{", `\{`,
  33. "}", `\}`,
  34. "[", `\[`,
  35. "]", `\]`,
  36. `|`, `\|`,
  37. `\`, `\\`,
  38. // wildcard characters
  39. "*", ".*",
  40. "?", ".")
  41. type WildcardQuery struct {
  42. Wildcard string `json:"wildcard"`
  43. FieldVal string `json:"field,omitempty"`
  44. BoostVal *Boost `json:"boost,omitempty"`
  45. compiled *regexp.Regexp
  46. }
  47. // NewWildcardQuery creates a new Query which finds
  48. // documents containing terms that match the
  49. // specified wildcard. In the wildcard pattern '*'
  50. // will match any sequence of 0 or more characters,
  51. // and '?' will match any single character.
  52. func NewWildcardQuery(wildcard string) *WildcardQuery {
  53. return &WildcardQuery{
  54. Wildcard: wildcard,
  55. }
  56. }
  57. func (q *WildcardQuery) SetBoost(b float64) {
  58. boost := Boost(b)
  59. q.BoostVal = &boost
  60. }
  61. func (q *WildcardQuery) Boost() float64 {
  62. return q.BoostVal.Value()
  63. }
  64. func (q *WildcardQuery) SetField(f string) {
  65. q.FieldVal = f
  66. }
  67. func (q *WildcardQuery) Field() string {
  68. return q.FieldVal
  69. }
  70. func (q *WildcardQuery) Searcher(i index.IndexReader, m mapping.IndexMapping, options search.SearcherOptions) (search.Searcher, error) {
  71. field := q.FieldVal
  72. if q.FieldVal == "" {
  73. field = m.DefaultSearchField()
  74. }
  75. if q.compiled == nil {
  76. var err error
  77. q.compiled, err = q.convertToRegexp()
  78. if err != nil {
  79. return nil, err
  80. }
  81. }
  82. return searcher.NewRegexpSearcher(i, q.compiled, field, q.BoostVal.Value(), options)
  83. }
  84. func (q *WildcardQuery) Validate() error {
  85. var err error
  86. q.compiled, err = q.convertToRegexp()
  87. return err
  88. }
  89. func (q *WildcardQuery) convertToRegexp() (*regexp.Regexp, error) {
  90. regexpString := wildcardRegexpReplacer.Replace(q.Wildcard)
  91. return regexp.Compile(regexpString)
  92. }