2015-03-11 21:57:22 +01:00
|
|
|
// Copyright (c) 2014 Couchbase, Inc.
|
|
|
|
// 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.
|
|
|
|
|
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"
|
|
|
|
"github.com/blevesearch/bleve/search/searchers"
|
|
|
|
)
|
|
|
|
|
2016-09-20 03:12:36 +02:00
|
|
|
type RegexpQuery struct {
|
2016-09-23 01:49:31 +02:00
|
|
|
Regexp string `json:"regexp"`
|
|
|
|
Field string `json:"field,omitempty"`
|
|
|
|
Boost *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)
|
|
|
|
q.Boost = &boost
|
2015-03-11 21:57:22 +01:00
|
|
|
}
|
|
|
|
|
2016-09-23 01:49:31 +02:00
|
|
|
func (q *RegexpQuery) SetField(f string) {
|
|
|
|
q.Field = f
|
2015-03-11 21:57:22 +01:00
|
|
|
}
|
|
|
|
|
2016-09-20 03:12:36 +02:00
|
|
|
func (q *RegexpQuery) Searcher(i index.IndexReader, m mapping.IndexMapping, explain bool) (search.Searcher, error) {
|
2016-09-23 01:49:31 +02:00
|
|
|
field := q.Field
|
|
|
|
if q.Field == "" {
|
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
|
|
|
|
}
|
|
|
|
|
2016-09-23 01:49:31 +02:00
|
|
|
return searchers.NewRegexpSearcher(i, q.compiled, field, q.Boost.Value(), explain)
|
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
|
|
|
}
|