feat: improve article search ranking (#11640)

This commit is contained in:
Shivam Mishra
2025-06-03 12:01:17 +05:30
committed by GitHub
parent bae958334d
commit 02c4863d95
3 changed files with 15 additions and 25 deletions

View File

@@ -76,18 +76,24 @@ class Article < ApplicationRecord
scope :order_by_views, -> { reorder(views: :desc) }
# TODO: if text search slows down https://www.postgresql.org/docs/current/textsearch-features.html#TEXTSEARCH-UPDATE-TRIGGERS
# - the A, B and C are for weightage. See: https://github.com/Casecommons/pg_search#weighting
# - the normalization is for ensuring the long articles that mention the search term too many times are not ranked higher.
# it divides rank by log(document_length) to prevent longer articles from ranking higher just due to sizeSee: https://github.com/Casecommons/pg_search#normalization
# - the ranking is to ensure that articles with higher weightage are ranked higher
pg_search_scope(
:text_search,
against: %i[
title
description
content
],
against: {
title: 'A',
description: 'B',
content: 'C'
},
using: {
tsearch: {
prefix: true
prefix: true,
normalization: 2
}
}
},
ranked_by: ':tsearch'
)
def self.search(params)