-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #78 from SADiLaR/feature/search-vector
added vector search
- Loading branch information
Showing
7 changed files
with
95 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
app/general/management/commands/dev_update_vector_search.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import os | ||
|
||
from django.core.management.base import BaseCommand | ||
|
||
from general.models import DocumentFile | ||
|
||
|
||
class Command(BaseCommand): | ||
help = "Updating the Vector Search index on document_file." | ||
|
||
def handle(self, *args, **options): | ||
os.system("clear") | ||
print("Querying the Vector Search index and Updating.") | ||
|
||
all_document_files = DocumentFile.objects.all() | ||
|
||
for document_file in all_document_files: | ||
document_file.save() # This line updates the vector search for the document file | ||
print(f"Updated {document_file.title}.") | ||
print() |
35 changes: 35 additions & 0 deletions
35
app/general/migrations/00010_documentfile_search_vector_trigger.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from django.contrib.postgres.search import SearchVector | ||
from django.db import migrations | ||
|
||
|
||
def compute_search_vector(apps, schema_editor): | ||
Quote = apps.get_model("general", "DocumentFile") | ||
Quote.objects.update(search_vector=SearchVector("document_data", "title")) | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("general", "0009_documentfile_search_vector_and_more"), | ||
] | ||
|
||
operations = [ | ||
migrations.RunSQL( | ||
sql=""" | ||
CREATE TRIGGER search_vector_trigger | ||
BEFORE INSERT OR UPDATE OF document_data, title, search_vector | ||
ON general_documentfile | ||
FOR EACH ROW EXECUTE PROCEDURE | ||
tsvector_update_trigger( | ||
search_vector, 'pg_catalog.english', document_data, title | ||
); | ||
UPDATE general_documentfile SET search_vector = NULL; | ||
""", | ||
reverse_sql=""" | ||
DROP TRIGGER IF EXISTS search_vector_trigger | ||
ON general_documentfile; | ||
""", | ||
), | ||
migrations.RunPython( | ||
compute_search_vector, reverse_code=migrations.RunPython.noop | ||
), | ||
] |
24 changes: 24 additions & 0 deletions
24
app/general/migrations/0009_documentfile_search_vector_and_more.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Generated by Django 5.0.2 on 2024-06-14 10:33 | ||
|
||
import django.contrib.postgres.indexes | ||
import django.contrib.postgres.search | ||
from django.db import migrations | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('general', '0008_documentfile_description_and_more'), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name='documentfile', | ||
name='search_vector', | ||
field=django.contrib.postgres.search.SearchVectorField(blank=True, null=True), | ||
), | ||
migrations.AddIndex( | ||
model_name='documentfile', | ||
index=django.contrib.postgres.indexes.GinIndex(fields=['search_vector'], name='general_doc_search__752b22_gin'), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters