From d8846d65e49c845b10de392536c90e9b22dea8c8 Mon Sep 17 00:00:00 2001 From: divyam234 <47589864+divyam234@users.noreply.github.com> Date: Thu, 11 Jul 2024 19:16:25 +0530 Subject: [PATCH] refactor: add support for multilingual search using pgroonga extension --- README.md | 2 +- docker-compose.postgres.yml | 2 +- internal/database/migrations/20240711163538_search.sql | 8 ++++++++ pkg/services/file.go | 2 +- 4 files changed, 11 insertions(+), 3 deletions(-) create mode 100644 internal/database/migrations/20240711163538_search.sql diff --git a/README.md b/README.md index 270714e..e10cb52 100644 --- a/README.md +++ b/README.md @@ -99,7 +99,7 @@ services: db: condition: service_healthy db: - image: postgres:15 + image: groonga/pgroonga container_name: teldrive_db restart: always environment: diff --git a/docker-compose.postgres.yml b/docker-compose.postgres.yml index 3b673fe..8980e04 100644 --- a/docker-compose.postgres.yml +++ b/docker-compose.postgres.yml @@ -15,7 +15,7 @@ services: db: condition: service_healthy db: - image: postgres:15 + image: groonga/pgroonga container_name: teldrive_db restart: always environment: diff --git a/internal/database/migrations/20240711163538_search.sql b/internal/database/migrations/20240711163538_search.sql new file mode 100644 index 0000000..ae05e86 --- /dev/null +++ b/internal/database/migrations/20240711163538_search.sql @@ -0,0 +1,8 @@ +-- +goose Up +-- +goose StatementBegin +CREATE EXTENSION IF NOT EXISTS pgroonga; +DROP INDEX IF EXISTS teldrive.name_search_idx; +DROP FUNCTION IF EXISTS teldrive.get_tsquery; +DROP FUNCTION IF EXISTS teldrive.get_tsvector; +CREATE INDEX name_search_idx ON teldrive.files USING pgroonga (REGEXP_REPLACE(name, '[.,-_]', ' ', 'g')) WITH (tokenizer = 'TokenNgram'); +-- +goose StatementEnd \ No newline at end of file diff --git a/pkg/services/file.go b/pkg/services/file.go index 133e81d..6ef7826 100644 --- a/pkg/services/file.go +++ b/pkg/services/file.go @@ -261,7 +261,7 @@ func (fs *FileService) ListFiles(userId int64, fquery *schemas.FileQuery) (*sche } if fquery.Query != "" { - query.Where("teldrive.get_tsquery(?) @@ teldrive.get_tsvector(name)", fquery.Query) + query.Where("name &@~ REGEXP_REPLACE(?, '[.,-_]', ' ', 'g')", fquery.Query) } if fquery.Category != "" {