Skip to content

Commit

Permalink
feat: allow database fulltext parser
Browse files Browse the repository at this point in the history
  • Loading branch information
Mmx233 committed Aug 24, 2024
1 parent 868b0ec commit 0232f9c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
1 change: 1 addition & 0 deletions internal/conf/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type Database struct {
TablePrefix string `json:"table_prefix" env:"TABLE_PREFIX"`
SSLMode string `json:"ssl_mode" env:"SSL_MODE"`
DSN string `json:"dsn" env:"DSN"`
Parser string `json:"parser" env:"PARSER"`
}

type Meilisearch struct {
Expand Down
11 changes: 9 additions & 2 deletions internal/search/db/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,26 @@ var config = searcher.Config{
func init() {
searcher.RegisterSearcher(config, func() (searcher.Searcher, error) {
db := db.GetDb()
var parser string
switch conf.Conf.Database.Type {
case "mysql":
if conf.Conf.Database.Parser != "" {
parser = fmt.Sprintf(" WITH PARSER %s", conf.Conf.Database.Parser)
}
tableName := fmt.Sprintf("%ssearch_nodes", conf.Conf.Database.TablePrefix)
tx := db.Exec(fmt.Sprintf("CREATE FULLTEXT INDEX idx_%s_name_fulltext ON %s(name);", tableName, tableName))
tx := db.Exec(fmt.Sprintf("CREATE FULLTEXT INDEX idx_%s_name_fulltext%s ON %s(name);", tableName, parser, tableName))
if err := tx.Error; err != nil && !strings.Contains(err.Error(), "Error 1061 (42000)") { // duplicate error
log.Errorf("failed to create full text index: %v", err)
return nil, err
}
case "postgres":
if conf.Conf.Database.Parser != "" {
parser = fmt.Sprintf(" gin_%s", conf.Conf.Database.Parser)
}
db.Exec("CREATE EXTENSION pg_trgm;")
db.Exec("CREATE EXTENSION btree_gin;")
tableName := fmt.Sprintf("%ssearch_nodes", conf.Conf.Database.TablePrefix)
tx := db.Exec(fmt.Sprintf("CREATE INDEX idx_%s_name ON %s USING GIN (name);", tableName, tableName))
tx := db.Exec(fmt.Sprintf("CREATE INDEX idx_%s_name ON %s USING GIN (name%s);", tableName, tableName, parser))
if err := tx.Error; err != nil && !strings.Contains(err.Error(), "SQLSTATE 42P07") {
log.Errorf("failed to create index using GIN: %v", err)
return nil, err
Expand Down

0 comments on commit 0232f9c

Please sign in to comment.