I'm using bleve (with a http server in front) to index and query documents.
I need to execute a query as below which should query documents matching channelID
value. How can escape query fields(in this case the value of channelID
) in the query syntax?
query.NewQueryStringQuery("channelID:http://example.com?some-params-with$-+badChars").
I can't just urlencode it because if I do it won't match the field from the documented indexed(which has the value unencoded).
Update(testcase)
package main
import (
"github.com/blevesearch/bleve"
//"github.com/blevesearch/bleve/search"
"github.com/blevesearch/bleve/search/query"
"bytes"
"flag"
log "github.com/golang/glog"
)
type Data struct {
ID string
Message string
URI string
}
func main() {
flag.Parse()
defer log.Flush()
mapping := bleve.NewIndexMapping()
index, err := bleve.New("example.bleve", mapping)
if err != nil {
panic(err)
}
d := Data{
ID: "someID",
Message: "Hello",
URI: `https://www.google.co.uk/search?q=google+search&oq={}///\}ie=UTF-8`,
}
if err := index.Index(d.ID, d); err != nil {
panic(err)
}
//index, _ := bleve.Open("example.bleve")
query := query.NewQueryStringQuery("URI:" + Escape(d.URI))
searchRequest := bleve.NewSearchRequest(query)
result, _ := index.Search(searchRequest)
log.Errorf("total hits %v", result.Total)
}
func Escape(s string) string {
ra := []string{"+", "-", "=",
"&", "|", ">",
"<", "!", "(", ")", "{",
"}", "[", "]", "^",
`~`, `*`, `?`, `:`, `\\`, `/`, `\`, ` `}
exists := func(v string) bool {
for _, s := range ra {
if v == s {
return true
}
}
return false
}
buf := bytes.NewBuffer(nil)
var prevBack bool
for _, v := range s {
if prevBack || !exists(string(v)) {
buf.WriteString(string(v))
prevBack = false
} else {
buf.WriteString(`\`)
buf.WriteString(string(v))
if string(v) == `\` {
prevBack = true
}
}
}
return buf.String()
}
You just put a backslash before the special characters. You find a list of all special characters in the docs (at the bottom under escaping): http://www.blevesearch.com/docs/Query-String-Query/