Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(search): allow database fulltext index using custom parser #7074

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading