feat(server) #3: sort search results by updated date then score

pull/7968/head
Tayeb Chlyah 3 months ago
parent 317fa1bb92
commit b0c02fc81f

@ -20,6 +20,7 @@ module.exports = {
* @param {Object} opts Additional options
*/
async query(q, opts) {
const normalizedQuery = q.toLowerCase()
const results = await WIKI.models.pages.query()
.column('pages.id', 'title', 'description', 'path', 'localeCode as locale')
.withGraphJoined('tags') // Adding page tags since they can be used to check resource access permissions
@ -46,6 +47,21 @@ module.exports = {
}
})
})
.orderBy('pages.updatedAt', 'desc')
.orderByRaw(`
CASE
WHEN LOWER(title) = ? THEN 400
WHEN LOWER(title) LIKE ? THEN 300
WHEN LOWER(description) LIKE ? THEN 200
WHEN LOWER(path) LIKE ? THEN 100
ELSE 0
END DESC
`, [
normalizedQuery,
`%${normalizedQuery}%`,
`%${normalizedQuery}%`,
`%${normalizedQuery}%`
])
.limit(WIKI.config.search.maxHits)
return {
results,

@ -62,19 +62,21 @@ module.exports = {
try {
let suggestions = []
let qry = `
SELECT id, path, locale, title, description, content
FROM "pagesVector", to_tsquery(?,?) query
WHERE (query @@ "tokens" OR path ILIKE ?)
SELECT pv.id, pv.path, pv.locale, pv.title, pv.description, pv.content
FROM "pagesVector" pv
INNER JOIN "pages" p ON p.path = pv.path AND p."localeCode" = pv.locale
CROSS JOIN to_tsquery(?,?) query
WHERE (query @@ pv.tokens OR pv.path ILIKE ?)
`
let qryEnd = `ORDER BY ts_rank(tokens, query) DESC`
let qryEnd = `ORDER BY p."updatedAt" DESC, ts_rank(pv.tokens, query) DESC`
let qryParams = [this.config.dictLanguage, tsquery(q), `%${q.toLowerCase()}%`]
if (opts.locale) {
qry = `${qry} AND locale = ?`
qry = `${qry} AND pv.locale = ?`
qryParams.push(opts.locale)
}
if (opts.path) {
qry = `${qry} AND path ILIKE ?`
qry = `${qry} AND pv.path ILIKE ?`
qryParams.push(`%${opts.path}`)
}
const results = await WIKI.models.knex.raw(`

Loading…
Cancel
Save