Add FTS with sqlite
This commit is contained in:
parent
741488f40b
commit
aab93a4d3e
3 changed files with 29 additions and 3 deletions
22
yamdl_playground/core/apps.py
Normal file
22
yamdl_playground/core/apps.py
Normal 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])
|
|
@ -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")
|
||||||
|
|
|
@ -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 = [
|
||||||
|
|
Loading…
Reference in a new issue