Skip to content

Commit 407aacf

Browse files
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 <[email protected]>
1 parent 54d21ae commit 407aacf

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

server/modules/search/postgres/engine.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ module.exports = {
2323
async init() {
2424
WIKI.logger.info(`(SEARCH/POSTGRES) Initializing...`)
2525

26+
// -> Ensure pg_trgm extension is available (required for similarity search)
27+
await WIKI.models.knex.raw('CREATE EXTENSION IF NOT EXISTS pg_trgm')
28+
2629
// -> Create Search Index
2730
const indexExists = await WIKI.models.knex.schema.hasTable('pagesVector')
2831
if (!indexExists) {
@@ -45,7 +48,6 @@ module.exports = {
4548
CREATE TABLE "pagesWords" AS SELECT word FROM ts_stat(
4649
'SELECT to_tsvector(''simple'', "title") || to_tsvector(''simple'', "description") || to_tsvector(''simple'', "content") FROM "pagesVector"'
4750
)`)
48-
await WIKI.models.knex.raw('CREATE EXTENSION IF NOT EXISTS pg_trgm')
4951
await WIKI.models.knex.raw(`CREATE INDEX "pageWords_idx" ON "pagesWords" USING GIN (word gin_trgm_ops)`)
5052
}
5153

@@ -81,8 +83,12 @@ module.exports = {
8183
${qryEnd}
8284
`, qryParams)
8385
if (results.rows.length < 5) {
84-
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])
85-
suggestions = suggestResults.rows.map(r => r.word)
86+
try {
87+
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])
88+
suggestions = suggestResults.rows.map(r => r.word)
89+
} catch (err) {
90+
WIKI.logger.warn(`Search Engine Suggestion Error (pg_trgm extension may be missing): ${err.message}`)
91+
}
8692
}
8793
return {
8894
results: results.rows,

0 commit comments

Comments
 (0)