您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

regexp.go 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. type RegexpQuery struct {
  24. Regexp string `json:"regexp"`
  25. FieldVal string `json:"field,omitempty"`
  26. BoostVal *Boost `json:"boost,omitempty"`
  27. compiled *regexp.Regexp
  28. }
  29. // NewRegexpQuery creates a new Query which finds
  30. // documents containing terms that match the
  31. // specified regular expression. The regexp pattern
  32. // SHOULD NOT include ^ or $ modifiers, the search
  33. // will only match entire terms even without them.
  34. func NewRegexpQuery(regexp string) *RegexpQuery {
  35. return &RegexpQuery{
  36. Regexp: regexp,
  37. }
  38. }
  39. func (q *RegexpQuery) SetBoost(b float64) {
  40. boost := Boost(b)
  41. q.BoostVal = &boost
  42. }
  43. func (q *RegexpQuery) Boost() float64 {
  44. return q.BoostVal.Value()
  45. }
  46. func (q *RegexpQuery) SetField(f string) {
  47. q.FieldVal = f
  48. }
  49. func (q *RegexpQuery) Field() string {
  50. return q.FieldVal
  51. }
  52. func (q *RegexpQuery) Searcher(i index.IndexReader, m mapping.IndexMapping, options search.SearcherOptions) (search.Searcher, error) {
  53. field := q.FieldVal
  54. if q.FieldVal == "" {
  55. field = m.DefaultSearchField()
  56. }
  57. err := q.compile()
  58. if err != nil {
  59. return nil, err
  60. }
  61. return searcher.NewRegexpSearcher(i, q.compiled, field, q.BoostVal.Value(), options)
  62. }
  63. func (q *RegexpQuery) Validate() error {
  64. return q.compile()
  65. }
  66. func (q *RegexpQuery) compile() error {
  67. if q.compiled == nil {
  68. // require that pattern NOT be anchored to start and end of term
  69. actualRegexp := q.Regexp
  70. if strings.HasPrefix(actualRegexp, "^") {
  71. actualRegexp = actualRegexp[1:] // remove leading ^
  72. }
  73. // do not attempt to remove trailing $, it's presence is not
  74. // known to interfere with LiteralPrefix() the way ^ does
  75. // and removing $ introduces possible ambiguities with escaped \$, \\$, etc
  76. var err error
  77. q.compiled, err = regexp.Compile(actualRegexp)
  78. if err != nil {
  79. return err
  80. }
  81. }
  82. return nil
  83. }