Full-text search using postgresql #162
40
extensions/migrations/0033_extensions_fts_20240603_1918.py
Normal file
40
extensions/migrations/0033_extensions_fts_20240603_1918.py
Normal file
@ -0,0 +1,40 @@
|
||||
# Generated by Django 4.2.11 on 2024-06-03 17:18
|
||||
|
||||
from django.db import migrations
|
||||
|
||||
|
||||
def create_indexes(apps, schema_editor):
|
||||
if schema_editor.connection.vendor != 'postgresql':
|
||||
return
|
||||
with schema_editor.connection.cursor() as cursor:
|
||||
cursor.execute(
|
||||
"""
|
||||
CREATE INDEX extensions_fts ON extensions_extension USING
|
||||
gin ((to_tsvector('english', name) || ' ' || to_tsvector('english', description)))
|
||||
"""
|
||||
)
|
||||
cursor.execute(
|
||||
"""
|
||||
CREATE INDEX extensions_trgm_gin ON extensions_extension USING
|
||||
gin((((name)::text || ' '::text) || description) gin_trgm_ops);
|
||||
"""
|
||||
)
|
||||
|
||||
|
||||
def delete_indexes(apps, schema_editor):
|
||||
if schema_editor.connection.vendor != 'postgresql':
|
||||
return
|
||||
with schema_editor.connection.cursor() as cursor:
|
||||
cursor.execute('drop index extensions_fts')
|
||||
cursor.execute('drop index extensions_trgm_gin')
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('extensions', '0032_extension_extensions__is_list_765936_idx_and_more'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RunPython(create_indexes, delete_indexes)
|
||||
]
|
@ -1,7 +1,6 @@
|
||||
from collections import OrderedDict
|
||||
import logging
|
||||
|
||||
from django.conf import settings
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.db import connection
|
||||
from django.db.models import Count, Q
|
||||
@ -111,8 +110,9 @@ class SearchView(ListedExtensionsView):
|
||||
return queryset
|
||||
|
||||
# WARNING: full-text search support only on postgres
|
||||
# using DEBUG as a shortcut for checking if we run on postgres vs sqlite
|
||||
if settings.DEBUG:
|
||||
if connection.vendor == 'postgresql':
|
||||
queryset = self.postgres_fts(queryset, search_query)
|
||||
else:
|
||||
filter = Q()
|
||||
for token in search_query.split():
|
||||
filter &= (
|
||||
@ -122,8 +122,6 @@ class SearchView(ListedExtensionsView):
|
||||
| Q(latest_version__tags__name__icontains=token)
|
||||
)
|
||||
queryset = queryset.filter(filter).distinct()
|
||||
else:
|
||||
queryset = self.postgres_fts(queryset, search_query)
|
||||
return queryset
|
||||
|
||||
def postgres_fts(self, queryset, search_query):
|
||||
|
Loading…
Reference in New Issue
Block a user