mirror of
https://github.com/requarks/wiki.git
synced 2026-08-26 05:07:30 -05:00
fix(search): always ensure pg_trgm extension availability for PostgreSQL search (#7845)
* fix(search): ensure pg_trgm extension availability for PostgreSQL search - Move pg_trgm extension creation to initialization phase to ensure availability - Add error handling for similarity search queries that depend on pg_trgm - Add proper logging for debugging search-related issues Fixes issues where PostgreSQL search suggestions fail due to missing or improperly initialized pg_trgm extension, particularly in containerized environments where extension creation timing matters. * fix: Simplify error handling in search suggestion logic Refactor error handling for search suggestions to simplify code. --------- Co-authored-by: Nicolas Giard <github@ngpixel.com>
This commit is contained in:
co-authored by
Nicolas Giard
parent
54d21ae538
commit
407aacfa19
@@ -23,6 +23,9 @@ module.exports = {
|
||||
async init() {
|
||||
WIKI.logger.info(`(SEARCH/POSTGRES) Initializing...`)
|
||||
|
||||
// -> Ensure pg_trgm extension is available (required for similarity search)
|
||||
await WIKI.models.knex.raw('CREATE EXTENSION IF NOT EXISTS pg_trgm')
|
||||
|
||||
// -> Create Search Index
|
||||
const indexExists = await WIKI.models.knex.schema.hasTable('pagesVector')
|
||||
if (!indexExists) {
|
||||
@@ -45,7 +48,6 @@ module.exports = {
|
||||
CREATE TABLE "pagesWords" AS SELECT word FROM ts_stat(
|
||||
'SELECT to_tsvector(''simple'', "title") || to_tsvector(''simple'', "description") || to_tsvector(''simple'', "content") FROM "pagesVector"'
|
||||
)`)
|
||||
await WIKI.models.knex.raw('CREATE EXTENSION IF NOT EXISTS pg_trgm')
|
||||
await WIKI.models.knex.raw(`CREATE INDEX "pageWords_idx" ON "pagesWords" USING GIN (word gin_trgm_ops)`)
|
||||
}
|
||||
|
||||
@@ -81,8 +83,12 @@ module.exports = {
|
||||
${qryEnd}
|
||||
`, qryParams)
|
||||
if (results.rows.length < 5) {
|
||||
const suggestResults = await WIKI.models.knex.raw(`SELECT word, word <-> ? AS rank FROM "pagesWords" WHERE similarity(word, ?) > 0.2 ORDER BY rank LIMIT 5;`, [q, q])
|
||||
suggestions = suggestResults.rows.map(r => r.word)
|
||||
try {
|
||||
const suggestResults = await WIKI.models.knex.raw(`SELECT word, word <-> ? AS rank FROM "pagesWords" WHERE similarity(word, ?) > 0.2 ORDER BY rank LIMIT 5;`, [q, q])
|
||||
suggestions = suggestResults.rows.map(r => r.word)
|
||||
} catch (err) {
|
||||
WIKI.logger.warn(`Search Engine Suggestion Error (pg_trgm extension may be missing): ${err.message}`)
|
||||
}
|
||||
}
|
||||
return {
|
||||
results: results.rows,
|
||||
|
||||
Reference in New Issue
Block a user