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") sess.Where("NOT repository.is_private")
} }
if q != "" { if q != "" {
sess.Where(x.FulltextMatch("message"), q) cond, params := x.FulltextMatch("message", q)
sess.Where(cond, params...)
} }
var countSess xorm.Session var countSess xorm.Session
countSess = *sess countSess = *sess

View File

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

View File

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

View File

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

View File

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