search for all words in mysql match

master
Vitaliy Filippov 2017-04-28 01:43:59 +03:00
parent b1c98f2199
commit 0570df3267
5 changed files with 14 additions and 10 deletions

View File

@ -51,7 +51,8 @@ func FulltextSearchCommits(userid int64, q string, limit int, offset int) ([]*Co
sess.Where("NOT repository.is_private")
}
if q != "" {
sess.Where(x.FulltextMatch("message"), q)
cond, params := x.FulltextMatch("message", q)
sess.Where(cond, params...)
}
var countSess xorm.Session
countSess = *sess

View File

@ -52,7 +52,7 @@ type Dialect interface {
IndexOnTable() bool
ShowCreateNull() bool
FulltextMatch(column string) string
FulltextMatch(column string, query string) (string, []interface{})
IndexCheckSql(tableName, idxName string) (string, []interface{})
TableCheckSql(tableName string) (string, []interface{})
@ -141,8 +141,8 @@ func (b *Base) EqStr() string {
return "="
}
func (db *Base) FulltextMatch(column string) string {
return "? = ''"
func (db *Base) FulltextMatch(column string, query string) (string, []interface{}) {
return "1=0", nil
}
func (db *Base) RollBackStr() string {

View File

@ -123,8 +123,8 @@ func (engine *Engine) QuoteStr() string {
return engine.dialect.QuoteStr()
}
func (engine *Engine) FulltextMatch(column string) string {
return engine.dialect.FulltextMatch(column)
func (engine *Engine) FulltextMatch(column string, query string) (string, []interface{}) {
return engine.dialect.FulltextMatch(column, query)
}
// Quote Use QuoteStr quote the string sql

View File

@ -10,6 +10,7 @@ import (
"strconv"
"strings"
"time"
"regexp"
"github.com/go-xorm/core"
)
@ -275,8 +276,10 @@ func (db *mysql) IndexOnTable() bool {
return true
}
func (db *mysql) FulltextMatch(column string) string {
return fmt.Sprintf("match(%s) against (? in boolean mode)", db.Quote(column))
func (db *mysql) FulltextMatch(column string, query string) (string, []interface{}) {
reg := regexp.MustCompile(`\s+`)
return fmt.Sprintf("match(%s) against (? in boolean mode)", db.Quote(column)),
[]interface{}{ reg.ReplaceAllLiteralString(query, " +") }
}
func (db *mysql) IndexCheckSql(tableName, idxName string) (string, []interface{}) {

View File

@ -862,8 +862,8 @@ func (db *postgres) IndexOnTable() bool {
return false
}
func (db *postgres) FulltextMatch(column string) string {
return fmt.Sprintf("to_tsvector(%s) @@ to_tsquery(?)", db.Quote(column))
func (db *postgres) FulltextMatch(column string, query string) (string, []interface{}) {
return fmt.Sprintf("to_tsvector(%s) @@ to_tsquery(?)", db.Quote(column)), []interface{} { query }
}
func (db *postgres) IndexCheckSql(tableName, idxName string) (string, []interface{}) {