Create fulltext index on commit.message

master
Vitaliy Filippov 2017-04-14 01:19:24 +03:00
parent 2055178f3c
commit 43a14306dc
7 changed files with 33 additions and 14 deletions

View File

@ -12,7 +12,7 @@ type Commit struct {
ID int64
RepoID int64 `xorm:"UNIQUE(commit_repo_sha)"`
Sha string `xorm:"VARCHAR(40) UNIQUE(commit_repo_sha)"`
Message string
Message string `xorm:"TEXT FULLTEXT(commit_message)"`
AuthorEmail string
AuthorName string
AuthorTime time.Time
@ -20,8 +20,3 @@ type Commit struct {
CommitterName string
CommitterTime time.Time
}
type CommitParent struct {
ParentId int64 `xorm:"pk"`
ChildId int64 `xorm:"pk"`
}

View File

@ -72,7 +72,7 @@ func init() {
new(Mirror), new(Release), new(LoginSource), new(Webhook), new(HookTask),
new(ProtectBranch), new(ProtectBranchWhitelist),
new(Team), new(OrgUser), new(TeamUser), new(TeamRepo),
new(Notice), new(EmailAddress), new(Commit), new(CommitParent))
new(Notice), new(EmailAddress), new(Commit))
gonicNames := []string{"SSL"}
for _, name := range gonicNames {

View File

@ -201,6 +201,8 @@ func (db *Base) CreateIndexSql(tableName string, index *Index) string {
var idxName string
if index.Type == UniqueType {
unique = " UNIQUE"
} else if index.Type == FulltextType {
unique = " FULLTEXT"
}
idxName = index.XName(tableName)
return fmt.Sprintf("CREATE%s INDEX %v ON %v (%v)", unique,

View File

@ -9,6 +9,7 @@ import (
const (
IndexType = iota + 1
UniqueType
FulltextType
)
// database index

View File

@ -909,7 +909,7 @@ func (engine *Engine) mapType(v reflect.Value) *core.Table {
}
indexNames := make(map[string]int)
var isIndex, isUnique bool
var isIndex, isUnique, isFulltext bool
var preKey string
for j, key := range tags {
k := strings.ToUpper(key)
@ -971,6 +971,11 @@ func (engine *Engine) mapType(v reflect.Value) *core.Table {
indexNames[indexName] = core.UniqueType
case k == "UNIQUE":
isUnique = true
case strings.HasPrefix(k, "FULLTEXT(") && strings.HasSuffix(k, ")"):
indexName := k[len("FULLTEXT")+1 : len(k)-1]
indexNames[indexName] = core.FulltextType
case k == "FULLTEXT":
isFulltext = true
case k == "NOTNULL":
col.Nullable = false
case k == "CACHE":
@ -1056,6 +1061,8 @@ func (engine *Engine) mapType(v reflect.Value) *core.Table {
if isUnique {
indexNames[col.Name] = core.UniqueType
} else if isFulltext {
indexNames[col.Name] = core.FulltextType
} else if isIndex {
indexNames[col.Name] = core.IndexType
}

View File

@ -879,6 +879,15 @@ func (db *postgres) TableCheckSql(tableName string) (string, []interface{}) {
" AND column_name = ?", args
}*/
func (db *postgres) CreateIndexSql(tableName string, index *core.Index) string {
if index.Type == core.FulltextType {
idxName := index.XName(tableName)
return fmt.Sprintf("CREATE INDEX %v ON %v USING GIN (to_tsvector(%v))",
db.Quote(idxName), db.Quote(tableName), db.Quote(strings.Join(index.Cols, db.Quote("||' '||"))))
}
return db.Base.CreateIndexSql(tableName, index)
}
func (db *postgres) ModifyColumnSql(tableName string, col *core.Column) string {
return fmt.Sprintf("alter table %s ALTER COLUMN %s TYPE %s",
tableName, col.Name, db.SqlType(col))
@ -1073,9 +1082,16 @@ func (db *postgres) GetIndexes(tableName string) (map[string]*core.Index, error)
} else {
indexType = core.IndexType
}
cs := strings.Split(indexdef, "(")
cs := strings.SplitN(indexdef, "(", 2)
colNames = strings.Split(cs[1][0:len(cs[1])-1], ",")
for i, colName := range colNames {
if strings.HasPrefix(colName, "to_tsvector(") {
colNames[i] = colName[len("to_tsvector("):len(colName)-1]
indexType = core.FulltextType
}
}
if strings.HasPrefix(indexName, "IDX_"+tableName) || strings.HasPrefix(indexName, "UQE_"+tableName) {
newIdxName := indexName[5+len(tableName):]
if newIdxName != "" {

View File

@ -1046,11 +1046,9 @@ func (statement *Statement) genCreateTableSQL() string {
func (statement *Statement) genIndexSQL() []string {
var sqls []string
tbName := statement.TableName()
quote := statement.Engine.Quote
for idxName, index := range statement.RefTable.Indexes {
if index.Type == core.IndexType {
sql := fmt.Sprintf("CREATE INDEX %v ON %v (%v);", quote(indexName(tbName, idxName)),
quote(tbName), quote(strings.Join(index.Cols, quote(","))))
for _, index := range statement.RefTable.Indexes {
if index.Type == core.IndexType || index.Type == core.FulltextType {
sql := statement.Engine.dialect.CreateIndexSql(tbName, index)
sqls = append(sqls, sql)
}
}