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