1
Fork 0

Add FTS with sqlite

This commit is contained in:
Jake Howard 2023-12-08 14:53:17 +00:00
parent 741488f40b
commit aab93a4d3e
Signed by: jake
GPG key ID: 57AFB45680EDD477
3 changed files with 29 additions and 3 deletions

View file

@ -0,0 +1,22 @@
from django.apps import AppConfig
from django.db import connections
from django.db.models.signals import post_save
class CoreConfig(AppConfig):
name = "yamdl_playground.core"
loaded = False
def ready(self):
if not self.loaded:
connection = connections["default"]
with connection.cursor() as cursor:
cursor.execute("CREATE VIRTUAL TABLE search_index USING fts5(body);")
post_save.connect(self.post_save)
self.loaded = True
def post_save(self, sender, instance, created, **kwargs):
with connections["default"].cursor() as cursor:
cursor.execute("INSERT INTO search_index(body, rowid) VALUES (%s, %s)", [instance.content, instance.pk])

View file

@ -1,6 +1,10 @@
from .models import Page from .models import Page
from django.http import HttpResponse from django.http import HttpResponse
import os from django.db import connections
def test(request): def test(request):
return HttpResponse(f"{Page.objects.count()} {os.getpid()}") with connections["default"].cursor() as cursor:
cursor.execute("SELECT rowid FROM search_index WHERE search_index = %s;", ["content"])
row = cursor.fetchone()
pages = Page.objects.filter(id__in=row)
return HttpResponse(str(pages), content_type="text/plain")

View file

@ -32,8 +32,8 @@ ALLOWED_HOSTS = []
INSTALLED_APPS = [ INSTALLED_APPS = [
'django.contrib.staticfiles', 'django.contrib.staticfiles',
"yamdl_playground.core",
"yamdl", "yamdl",
"yamdl_playground.core"
] ]
MIDDLEWARE = [ MIDDLEWARE = [