Full-text search using postgresql #162

Merged
Oleg-Komarov merged 10 commits from fts into main 2024-06-03 20:07:23 +02:00
Showing only changes of commit 890c96986b - Show all commits

View File

@ -129,8 +129,9 @@ class SearchView(ListedExtensionsView):
def postgres_fts(self, queryset, search_query): def postgres_fts(self, queryset, search_query):
"""Postgres full text search (fast) and a fuzzy trigram search (slow) as a fallback. """Postgres full text search (fast) and a fuzzy trigram search (slow) as a fallback.
Searches Extension name and description only. If we need to extend the functionality, Searches Extension name and description only, ranking name matches higher.
it's better to consider using a different approach, e.g. introduce meilisearch. If we need to extend the functionality, it's better to consider using a different approach,
e.g. introduce meilisearch.
Limits the results size to 32 items (2 pages), assuming that nobody will click through many Limits the results size to 32 items (2 pages), assuming that nobody will click through many
pages if we failed to present the vital results on the first page. pages if we failed to present the vital results on the first page.
@ -150,6 +151,10 @@ class SearchView(ListedExtensionsView):
(to_tsvector('english', name) || ' ' || to_tsvector('english', description)) (to_tsvector('english', name) || ' ' || to_tsvector('english', description))
@@ websearch_to_tsquery('english', %(query)s) @@ websearch_to_tsquery('english', %(query)s)
) and is_listed ) and is_listed
order by ts_rank(
to_tsvector('english', name),
websearch_to_tsquery('english', %(query)s)
) desc
limit 32""" limit 32"""
cursor.execute(sql, {'query': search_query}) cursor.execute(sql, {'query': search_query})
pks = [row[0] for row in cursor.fetchall()] pks = [row[0] for row in cursor.fetchall()]