up.sql 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. -- Your SQL goes here
  2. -- 创建表结构
  3. CREATE TABLE fts_texts (
  4. paragraph integer,
  5. book integer,
  6. wid varchar(50),
  7. -- 单个出现的黑体字,权重较大
  8. bold_single text,
  9. -- 成对出现的黑体字,权重一般
  10. bold_double text,
  11. -- 连续三个或三个以上的黑体字,权重较低
  12. bold_multiple text,
  13. content text
  14. );
  15. -- 创建全文检索配置 pali
  16. CREATE TEXT SEARCH CONFIGURATION pali ( parser = pg_catalog.default );
  17. -- 创建全文检索配置 pali_unaccent 无标音符号版
  18. CREATE TEXT SEARCH CONFIGURATION pali_unaccent ( parser = pg_catalog.default );
  19. -- 创建巴利语词形转换字典
  20. CREATE TEXT SEARCH DICTIONARY pali_stem (
  21. TEMPLATE = synonym,
  22. SYNONYMS = pali
  23. );
  24. -- 创建巴利语停用词字典
  25. CREATE TEXT SEARCH DICTIONARY pali_stopwords (
  26. TEMPLATE = pg_catalog.simple, STOPWORDS = pali,
  27. ACCEPT = true
  28. );
  29. -- 修改全文检索配置 pali 使用我们创建的字典
  30. ALTER TEXT SEARCH CONFIGURATION pali
  31. ADD MAPPING FOR asciiword, word, hword_part, hword_asciipart
  32. WITH pali_stem, pali_stopwords;
  33. -- 修改全文检索配置 pali_unaccent 使用我们创建的字典
  34. CREATE EXTENSION IF NOT EXISTS "unaccent";
  35. ALTER TEXT SEARCH CONFIGURATION pali_unaccent
  36. ADD MAPPING FOR asciiword, word, hword_part, hword_asciipart
  37. WITH unaccent, pali_stem, pali_stopwords;
  38. -- 添加自动更新的 TSVECTOR 字段
  39. ALTER TABLE fts_texts
  40. ADD COLUMN full_text_search_weighted TSVECTOR
  41. GENERATED ALWAYS AS (
  42. setweight(to_tsvector('pali', coalesce(content,'')), 'A') || ' ' ||
  43. setweight(to_tsvector('pali', coalesce(bold_single,'')), 'B') || ' ' ||
  44. setweight(to_tsvector('pali', coalesce(bold_double,'')), 'C') || ' ' ||
  45. setweight(to_tsvector('pali', coalesce(bold_multiple,'')), 'D')
  46. ) STORED;
  47. ALTER TABLE fts_texts
  48. ADD COLUMN full_text_search_weighted_unaccent TSVECTOR
  49. GENERATED ALWAYS AS (
  50. setweight(to_tsvector('pali_unaccent', coalesce(content,'')), 'A') || ' ' ||
  51. setweight(to_tsvector('pali_unaccent', coalesce(bold_single,'')), 'B') || ' ' ||
  52. setweight(to_tsvector('pali_unaccent', coalesce(bold_double,'')), 'C') || ' ' ||
  53. setweight(to_tsvector('pali_unaccent', coalesce(bold_multiple,'')), 'D')
  54. ) STORED;
  55. -- 为该字段创建索引
  56. CREATE INDEX full_text_search_weighted_idx
  57. ON fts_texts USING GIN (full_text_search_weighted);
  58. CREATE INDEX full_text_search_weighted__unaccent_idx
  59. ON fts_texts USING GIN (full_text_search_weighted_unaccent);
  60. -- 创建查询函数
  61. CREATE OR REPLACE FUNCTION query_pali(query_str TEXT)
  62. RETURNS TABLE(
  63. rank REAL,
  64. paragraph INTEGER,
  65. wid VARCHAR,
  66. bold_single TEXT,
  67. bold_double TEXT,
  68. bold_multiple TEXT,
  69. content TEXT,
  70. full_text_search_weighted TSVECTOR,
  71. full_text_search_weighted_unaccent TSVECTOR)
  72. AS $$
  73. SELECT
  74. ts_rank('{0.1, 0.2, 0.4, 1}',
  75. full_text_search_weighted,
  76. websearch_to_tsquery('pali', query_str)) +
  77. ts_rank('{0.1, 0.2, 0.4, 1}',
  78. full_text_search_weighted_unaccent,
  79. websearch_to_tsquery('pali_unaccent', query_str)), -- AS rank
  80. paragraph, wid, bold_single, bold_double, bold_multiple, content,
  81. full_text_search_weighted, full_text_search_weighted_unaccent
  82. FROM fts_texts
  83. WHERE
  84. full_text_search_weighted @@ websearch_to_tsquery('pali', query_str) OR
  85. full_text_search_weighted_unaccent @@ websearch_to_tsquery('pali_unaccent', query_str);
  86. $$ LANGUAGE SQL;