Improve file search results with postgres (#17112)
* Improve file search results with postgres * Adding channel id to search results * Fixing file info saves
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
2417fb265d
Коммит
7208952010
@@ -45,6 +45,7 @@ type FileInfo struct {
|
|||||||
Id string `json:"id"`
|
Id string `json:"id"`
|
||||||
CreatorId string `json:"user_id"`
|
CreatorId string `json:"user_id"`
|
||||||
PostId string `json:"post_id,omitempty"`
|
PostId string `json:"post_id,omitempty"`
|
||||||
|
ChannelId string `db:"-" json:"channel_id"`
|
||||||
CreateAt int64 `json:"create_at"`
|
CreateAt int64 `json:"create_at"`
|
||||||
UpdateAt int64 `json:"update_at"`
|
UpdateAt int64 `json:"update_at"`
|
||||||
DeleteAt int64 `json:"delete_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_postid_at", "FileInfo", "PostId")
|
||||||
fs.CreateIndexIfNotExists("idx_fileinfo_extension_at", "FileInfo", "Extension")
|
fs.CreateIndexIfNotExists("idx_fileinfo_extension_at", "FileInfo", "Extension")
|
||||||
fs.CreateFullTextIndexIfNotExists("idx_fileinfo_name_txt", "FileInfo", "Name")
|
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")
|
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) {
|
func (fs SqlFileInfoStore) GetByIds(ids []string) ([]*model.FileInfo, error) {
|
||||||
query := fs.getQueryBuilder().
|
query := fs.getQueryBuilder().
|
||||||
Select("*").
|
Select("FI.*, P.ChannelId").
|
||||||
From("FileInfo").
|
From("FileInfo as FI").
|
||||||
Where(sq.Eq{"Id": ids}).
|
LeftJoin("Posts as P ON FI.PostId=P.Id").
|
||||||
Where(sq.Eq{"DeleteAt": 0}).
|
Where(sq.Eq{"FI.Id": ids}).
|
||||||
OrderBy("CreateAt DESC")
|
Where(sq.Eq{"FI.DeleteAt": 0}).
|
||||||
|
OrderBy("FI.CreateAt DESC")
|
||||||
|
|
||||||
queryString, args, err := query.ToSql()
|
queryString, args, err := query.ToSql()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -430,7 +434,7 @@ func (fs SqlFileInfoStore) Search(paramsList []*model.SearchParams, userId, team
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
query := fs.getQueryBuilder().
|
query := fs.getQueryBuilder().
|
||||||
Select("FI.*").
|
Select("FI.*, P.ChannelId as ChannelId").
|
||||||
From("FileInfo AS FI").
|
From("FileInfo AS FI").
|
||||||
LeftJoin("Posts as P ON FI.PostId=P.Id").
|
LeftJoin("Posts as P ON FI.PostId=P.Id").
|
||||||
LeftJoin("Channels as C ON C.Id=P.ChannelId").
|
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{
|
query = query.Where(sq.Or{
|
||||||
sq.Expr("to_tsvector('english', FI.Name) @@ to_tsquery('english', ?)", queryTerms),
|
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),
|
sq.Expr("to_tsvector('english', FI.Content) @@ to_tsquery('english', ?)", queryTerms),
|
||||||
})
|
})
|
||||||
} else if fs.DriverName() == model.DATABASE_DRIVER_MYSQL {
|
} else if fs.DriverName() == model.DATABASE_DRIVER_MYSQL {
|
||||||
|
|||||||
@@ -43,6 +43,7 @@ type migrationDirection string
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
IndexTypeFullText = "full_text"
|
IndexTypeFullText = "full_text"
|
||||||
|
IndexTypeFullTextFunc = "full_text_func"
|
||||||
IndexTypeDefault = "default"
|
IndexTypeDefault = "default"
|
||||||
PGDupTableErrorCode = "42P07" // see https://github.com/lib/pq/blob/master/error.go#L268
|
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
|
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)
|
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 {
|
func (ss *SqlStore) createIndexIfNotExists(indexName string, tableName string, columnNames []string, indexType string, unique bool) bool {
|
||||||
|
|
||||||
uniqueStr := ""
|
uniqueStr := ""
|
||||||
@@ -898,6 +903,13 @@ func (ss *SqlStore) createIndexIfNotExists(indexName string, tableName string, c
|
|||||||
columnName := columnNames[0]
|
columnName := columnNames[0]
|
||||||
postgresColumnNames := convertMySQLFullTextColumnsToPostgres(columnName)
|
postgresColumnNames := convertMySQLFullTextColumnsToPostgres(columnName)
|
||||||
query = "CREATE INDEX " + indexName + " ON " + tableName + " USING gin(to_tsvector('english', " + postgresColumnNames + "))"
|
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 {
|
} else {
|
||||||
query = "CREATE " + uniqueStr + "INDEX " + indexName + " ON " + tableName + " (" + strings.Join(columnNames, ", ") + ")"
|
query = "CREATE " + uniqueStr + "INDEX " + indexName + " ON " + tableName + " (" + strings.Join(columnNames, ", ") + ")"
|
||||||
}
|
}
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user