Files
multica/server/migrations/036_search_index_lower.up.sql
LinYushen 7620a5a7e9 fix(search): LOWER/LIKE for pg_bigm 1.2 index compatibility (#621)
* fix(search): use LOWER/LIKE instead of ILIKE for pg_bigm 1.2 compatibility

pg_bigm 1.2 on RDS does not support ILIKE index scans. Replace all
ILIKE expressions with LOWER(column) LIKE LOWER(pattern) so the GIN
indexes are utilized. Rebuild gin_bigm_ops indexes on LOWER() expressions.

Closes MUL-482

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* fix(search): lowercase pattern in Go, add buildSearchQuery unit tests

- Lowercase phrase/terms in Go (strings.ToLower) so SQL only needs
  LOWER() on the column side, avoiding redundant per-query LOWER() on
  the pattern
- Add 5 unit tests for buildSearchQuery asserting SQL shape: no ILIKE,
  LOWER on columns only, lowercased args, multi-term AND, number match,
  include-closed flag, special char escaping

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-10 14:29:00 +08:00

29 lines
1.1 KiB
SQL

-- Rebuild pg_bigm GIN indexes on LOWER() expressions so that
-- LOWER(column) LIKE pattern queries can utilize them.
-- pg_bigm 1.2 (RDS) does not support ILIKE index scans;
-- LOWER(col) LIKE LOWER(pattern) is the compatible alternative.
-- Drop old indexes that were built on raw (non-lowered) columns.
DROP INDEX IF EXISTS idx_issue_title_bigm;
DROP INDEX IF EXISTS idx_issue_description_bigm;
DROP INDEX IF EXISTS idx_comment_content_bigm;
-- Recreate indexes on LOWER() expressions.
-- Wrapped in exception handler so CI environments without pg_bigm still pass.
DO $$
BEGIN
CREATE INDEX idx_issue_title_bigm ON issue USING gin (LOWER(title) gin_bigm_ops);
CREATE INDEX idx_issue_description_bigm ON issue USING gin (LOWER(COALESCE(description, '')) gin_bigm_ops);
EXCEPTION WHEN OTHERS THEN
RAISE NOTICE 'skipping bigram indexes on issue (pg_bigm not installed)';
END
$$;
DO $$
BEGIN
CREATE INDEX idx_comment_content_bigm ON comment USING gin (LOWER(content) gin_bigm_ops);
EXCEPTION WHEN OTHERS THEN
RAISE NOTICE 'skipping bigram index on comment (pg_bigm not installed)';
END
$$;