0
0
Fork 0

properly anchor regexp patterns to end of term

added integration tests for regexp anchoring
fixes #329
This commit is contained in:
Marty Schoch 2016-01-21 13:44:38 -05:00
parent fc34a97875
commit 0bddafb9e1
2 changed files with 44 additions and 1 deletions

View File

@ -11,6 +11,7 @@ package bleve
import (
"regexp"
"strings"
"github.com/blevesearch/bleve/index"
"github.com/blevesearch/bleve/search"
@ -58,8 +59,16 @@ func (q *regexpQuery) Searcher(i index.IndexReader, m *IndexMapping, explain boo
field = m.DefaultField
}
if q.compiled == nil {
// 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 + "$"
}
var err error
q.compiled, err = regexp.Compile(q.Regexp)
q.compiled, err = regexp.Compile(actualRegexp)
if err != nil {
return nil, err
}

View File

@ -582,5 +582,39 @@
}
]
}
},
{
"comment": "test regexp matching term",
"search": {
"from": 0,
"size": 10,
"query": {
"field": "name",
"regexp": "mar.*"
}
},
"result": {
"total_hits": 1,
"hits": [
{
"id": "a"
}
]
}
},
{
"comment": "test regexp that should not match when properly anchored",
"search": {
"from": 0,
"size": 10,
"query": {
"field": "name",
"regexp": "mar."
}
},
"result": {
"total_hits": 0,
"hits": []
}
}
]