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:
Jesús Espino
2021-03-16 11:19:13 +01:00
committed by GitHub
parent 2417fb265d
commit 7208952010
3 changed files with 24 additions and 6 deletions

View File

@@ -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"`

View File

@@ -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 {

View File

@@ -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, ", ") + ")"
}