2015-03-11 21:57:22 +01:00
|
|
|
// Copyright (c) 2014 Couchbase, Inc.
|
2016-10-02 16:13:14 +02:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
2015-03-11 21:57:22 +01:00
|
|
|
|
2016-09-20 03:12:36 +02:00
|
|
|
package query
|
2015-03-11 21:57:22 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"regexp"
|
2016-01-21 19:44:38 +01:00
|
|
|
"strings"
|
2015-03-11 21:57:22 +01:00
|
|
|
|
|
|
|
"github.com/blevesearch/bleve/index"
|
2016-09-18 15:33:18 +02:00
|
|
|
"github.com/blevesearch/bleve/mapping"
|
2015-03-11 21:57:22 +01:00
|
|
|
"github.com/blevesearch/bleve/search"
|
2016-10-01 23:20:59 +02:00
|
|
|
"github.com/blevesearch/bleve/search/searcher"
|
2015-03-11 21:57:22 +01:00
|
|
|
)
|
|
|
|
|
2016-09-20 03:12:36 +02:00
|
|
|
type RegexpQuery struct {
|
2016-09-23 01:49:31 +02:00
|
|
|
Regexp string `json:"regexp"`
|
2016-11-15 00:30:07 +01:00
|
|
|
FieldVal string `json:"field,omitempty"`
|
|
|
|
BoostVal *Boost `json:"boost,omitempty"`
|
2015-03-11 21:57:22 +01:00
|
|
|
compiled *regexp.Regexp
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewRegexpQuery creates a new Query which finds
|
|
|
|
// documents containing terms that match the
|
|
|
|
// specified regular expression.
|
2016-09-20 03:12:36 +02:00
|
|
|
func NewRegexpQuery(regexp string) *RegexpQuery {
|
|
|
|
return &RegexpQuery{
|
2016-09-23 01:49:31 +02:00
|
|
|
Regexp: regexp,
|
2015-03-11 21:57:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-23 01:49:31 +02:00
|
|
|
func (q *RegexpQuery) SetBoost(b float64) {
|
|
|
|
boost := Boost(b)
|
2016-11-15 00:30:07 +01:00
|
|
|
q.BoostVal = &boost
|
2015-03-11 21:57:22 +01:00
|
|
|
}
|
|
|
|
|
2017-01-06 02:49:45 +01:00
|
|
|
func (q *RegexpQuery) Boost() float64 {
|
2016-11-23 18:26:07 +01:00
|
|
|
return q.BoostVal.Value()
|
2015-03-11 21:57:22 +01:00
|
|
|
}
|
|
|
|
|
2016-11-15 00:30:07 +01:00
|
|
|
func (q *RegexpQuery) SetField(f string) {
|
|
|
|
q.FieldVal = f
|
2016-11-14 23:02:43 +01:00
|
|
|
}
|
|
|
|
|
2017-01-06 02:49:45 +01:00
|
|
|
func (q *RegexpQuery) Field() string {
|
2016-11-15 00:30:07 +01:00
|
|
|
return q.FieldVal
|
2016-11-14 23:02:43 +01:00
|
|
|
}
|
|
|
|
|
2017-01-06 02:49:45 +01:00
|
|
|
func (q *RegexpQuery) Searcher(i index.IndexReader, m mapping.IndexMapping, options search.SearcherOptions) (search.Searcher, error) {
|
2016-11-15 00:30:07 +01:00
|
|
|
field := q.FieldVal
|
|
|
|
if q.FieldVal == "" {
|
2016-09-18 15:33:18 +02:00
|
|
|
field = m.DefaultSearchField()
|
2015-03-11 21:57:22 +01:00
|
|
|
}
|
2016-01-21 23:26:24 +01:00
|
|
|
err := q.compile()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-01-06 02:49:45 +01:00
|
|
|
return searcher.NewRegexpSearcher(i, q.compiled, field, q.BoostVal.Value(), options)
|
2016-01-21 23:26:24 +01:00
|
|
|
}
|
|
|
|
|
2016-09-20 03:12:36 +02:00
|
|
|
func (q *RegexpQuery) Validate() error {
|
2016-01-21 23:26:24 +01:00
|
|
|
return q.compile()
|
|
|
|
}
|
|
|
|
|
2016-09-20 03:12:36 +02:00
|
|
|
func (q *RegexpQuery) compile() error {
|
2015-03-11 21:57:22 +01:00
|
|
|
if q.compiled == nil {
|
2016-01-21 19:44:38 +01:00
|
|
|
// require that pattern be anchored to start and end of term
|
|
|
|
actualRegexp := q.Regexp
|
|
|
|
if !strings.HasPrefix(actualRegexp, "^") {
|
|
|
|
actualRegexp = "^" + actualRegexp
|
|
|
|
}
|
|
|
|
if !strings.HasSuffix(actualRegexp, "$") {
|
|
|
|
actualRegexp = actualRegexp + "$"
|
|
|
|
}
|
2015-03-11 21:57:22 +01:00
|
|
|
var err error
|
2016-01-21 19:44:38 +01:00
|
|
|
q.compiled, err = regexp.Compile(actualRegexp)
|
2015-03-11 21:57:22 +01:00
|
|
|
if err != nil {
|
2016-01-21 23:26:24 +01:00
|
|
|
return err
|
2015-03-11 21:57:22 +01:00
|
|
|
}
|
|
|
|
}
|
2016-01-21 23:26:24 +01:00
|
|
|
return nil
|
2015-03-11 21:57:22 +01:00
|
|
|
}
|