mirror of
https://github.com/mattermost/mattermost.git
synced 2025-02-25 18:55:24 -06:00
Improve file search results with postgres (#17112)
* Improve file search results with postgres * Adding channel id to search results * Fixing file info saves
This commit is contained in:
@@ -45,6 +45,7 @@ type FileInfo struct {
|
||||
Id string `json:"id"`
|
||||
CreatorId string `json:"user_id"`
|
||||
PostId string `json:"post_id,omitempty"`
|
||||
ChannelId string `db:"-" json:"channel_id"`
|
||||
CreateAt int64 `json:"create_at"`
|
||||
UpdateAt int64 `json:"update_at"`
|
||||
DeleteAt int64 `json:"delete_at"`
|
||||
|
||||
@@ -79,6 +79,9 @@ func (fs SqlFileInfoStore) createIndexesIfNotExists() {
|
||||
fs.CreateIndexIfNotExists("idx_fileinfo_postid_at", "FileInfo", "PostId")
|
||||
fs.CreateIndexIfNotExists("idx_fileinfo_extension_at", "FileInfo", "Extension")
|
||||
fs.CreateFullTextIndexIfNotExists("idx_fileinfo_name_txt", "FileInfo", "Name")
|
||||
if fs.DriverName() == model.DATABASE_DRIVER_POSTGRES {
|
||||
fs.CreateFullTextFuncIndexIfNotExists("idx_fileinfo_name_splitted", "FileInfo", "Translate(Name, '.,-', ' ')")
|
||||
}
|
||||
fs.CreateFullTextIndexIfNotExists("idx_fileinfo_content_txt", "FileInfo", "Content")
|
||||
}
|
||||
|
||||
@@ -96,11 +99,12 @@ func (fs SqlFileInfoStore) Save(info *model.FileInfo) (*model.FileInfo, error) {
|
||||
|
||||
func (fs SqlFileInfoStore) GetByIds(ids []string) ([]*model.FileInfo, error) {
|
||||
query := fs.getQueryBuilder().
|
||||
Select("*").
|
||||
From("FileInfo").
|
||||
Where(sq.Eq{"Id": ids}).
|
||||
Where(sq.Eq{"DeleteAt": 0}).
|
||||
OrderBy("CreateAt DESC")
|
||||
Select("FI.*, P.ChannelId").
|
||||
From("FileInfo as FI").
|
||||
LeftJoin("Posts as P ON FI.PostId=P.Id").
|
||||
Where(sq.Eq{"FI.Id": ids}).
|
||||
Where(sq.Eq{"FI.DeleteAt": 0}).
|
||||
OrderBy("FI.CreateAt DESC")
|
||||
|
||||
queryString, args, err := query.ToSql()
|
||||
if err != nil {
|
||||
@@ -430,7 +434,7 @@ func (fs SqlFileInfoStore) Search(paramsList []*model.SearchParams, userId, team
|
||||
return nil, err
|
||||
}
|
||||
query := fs.getQueryBuilder().
|
||||
Select("FI.*").
|
||||
Select("FI.*, P.ChannelId as ChannelId").
|
||||
From("FileInfo AS FI").
|
||||
LeftJoin("Posts as P ON FI.PostId=P.Id").
|
||||
LeftJoin("Channels as C ON C.Id=P.ChannelId").
|
||||
@@ -538,6 +542,7 @@ func (fs SqlFileInfoStore) Search(paramsList []*model.SearchParams, userId, team
|
||||
|
||||
query = query.Where(sq.Or{
|
||||
sq.Expr("to_tsvector('english', FI.Name) @@ to_tsquery('english', ?)", queryTerms),
|
||||
sq.Expr("to_tsvector('english', Translate(FI.Name, '.,-', ' ')) @@ to_tsquery('english', ?)", queryTerms),
|
||||
sq.Expr("to_tsvector('english', FI.Content) @@ to_tsquery('english', ?)", queryTerms),
|
||||
})
|
||||
} else if fs.DriverName() == model.DATABASE_DRIVER_MYSQL {
|
||||
|
||||
@@ -43,6 +43,7 @@ type migrationDirection string
|
||||
|
||||
const (
|
||||
IndexTypeFullText = "full_text"
|
||||
IndexTypeFullTextFunc = "full_text_func"
|
||||
IndexTypeDefault = "default"
|
||||
PGDupTableErrorCode = "42P07" // see https://github.com/lib/pq/blob/master/error.go#L268
|
||||
MySQLDupTableErrorCode = uint16(1050) // see https://dev.mysql.com/doc/mysql-errors/5.7/en/server-error-reference.html#error_er_table_exists_error
|
||||
@@ -875,6 +876,10 @@ func (ss *SqlStore) CreateFullTextIndexIfNotExists(indexName string, tableName s
|
||||
return ss.createIndexIfNotExists(indexName, tableName, []string{columnName}, IndexTypeFullText, false)
|
||||
}
|
||||
|
||||
func (ss *SqlStore) CreateFullTextFuncIndexIfNotExists(indexName string, tableName string, function string) bool {
|
||||
return ss.createIndexIfNotExists(indexName, tableName, []string{function}, IndexTypeFullTextFunc, false)
|
||||
}
|
||||
|
||||
func (ss *SqlStore) createIndexIfNotExists(indexName string, tableName string, columnNames []string, indexType string, unique bool) bool {
|
||||
|
||||
uniqueStr := ""
|
||||
@@ -898,6 +903,13 @@ func (ss *SqlStore) createIndexIfNotExists(indexName string, tableName string, c
|
||||
columnName := columnNames[0]
|
||||
postgresColumnNames := convertMySQLFullTextColumnsToPostgres(columnName)
|
||||
query = "CREATE INDEX " + indexName + " ON " + tableName + " USING gin(to_tsvector('english', " + postgresColumnNames + "))"
|
||||
} else if indexType == IndexTypeFullTextFunc {
|
||||
if len(columnNames) != 1 {
|
||||
mlog.Critical("Unable to create multi column full text index")
|
||||
os.Exit(ExitCreateIndexPostgres)
|
||||
}
|
||||
columnName := columnNames[0]
|
||||
query = "CREATE INDEX " + indexName + " ON " + tableName + " USING gin(to_tsvector('english', " + columnName + "))"
|
||||
} else {
|
||||
query = "CREATE " + uniqueStr + "INDEX " + indexName + " ON " + tableName + " (" + strings.Join(columnNames, ", ") + ")"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user