Django field implementation for PostgreSQL tsvector.
Project description
django-tsvector-field is a drop-in replacement for Django’s django.contrib.postgres.search.SearchVectorField and manages the database triggers to keep your tsvector columns updated automatically.
Installation
Python 3+, Django 1.11+ and psycopg2 are the only requirements:
Install django-tsvector-field with your favorite python tool, e.g. pip install django-tsvector-field.
Add tsvector to your INSTALLED_APPS setting.
Usage
tsvector.SearchVectorField works like any other Django field: you add it to your model, run makemigrations to add the AddField operation to your migrations and when you migrate tsvector will take care to create the necessary postgres trigger and stored procedure.
Let’s create a TextDocument model with a search field holding our tsvector and having postgres automatically update it with title and body as inputs.
from django.db import models
import tsvector
class TextDocument(models.Model):
title = models.CharField(max_length=128)
body = models.TextField()
search = tsvector.SearchVectorField([
tsvector.WeightedColumn('title', 'A'),
tsvector.WeightedColumn('body', 'D'),
], 'english')
After you’ve migrated you can create some TextDocument records and see that postgres keeps it synchronized in the background. Specifically, because the search column is set at the database level, you need to call refresh_from_db() to get the updated search vector.
>>> doc = TextDocument.objects.create(
... title="My hovercraft is full of spam.",
... body="It's what eels love!"
... )
>>> doc.search
>>> doc.refresh_from_db()
>>> doc.search
"'eel':10 'full':4A 'hovercraft':2A 'love':11 'spam':6A"
Note that spam is recorded with 6A, this will be important later. Let’s continue with the previous session and create another document.
>>> doc = TextDocument.objects.create(
... title="What do eels eat?",
... body="Spam, spam, spam, they love spam!"
... )
>>> doc.refresh_from_db()
>>> doc.search
"'eat':4A 'eel':3A 'love':9 'spam':5,6,7,10"
No we have two documents: first document has just one spam with weight A and the second document has 4 spam with lower weight. If we search for spam and apply a search rank then the A weight on the first document will cause that document to appear higher in the results.
>>> matches = TextDocument.objects\
... .annotate(rank=SearchRank(F('search'), SearchQuery('spam')))\
... .order_by('-rank')\
... .values_list('rank', 'title', 'body')
>>> for match in matches:
... print(match)
...
(0.607927, 'My hovercraft is full of spam.', "It's what eels love!")
(0.0865452, 'What do eels eat?', 'Spam, spam, spam, they love spam!')
If you are only interested in getting a list of possible matches without ranking you can filter directly on the search column like so:
>>> TextDocument.objects.filter(search='spam')
<QuerySet [<TextDocument: TextDocument object>, <TextDocument: TextDocument object>]>
For more information, see the Django documentation on Full Text Search:
https://docs.djangoproject.com/en/dev/ref/contrib/postgres/search/
0.9.0
Initial release.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for django-tsvector-field-0.9.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 587f627949fd1dd046584ef3197fa844e5fe7c32a8b6d30d3bfba112ad51db91 |
|
MD5 | 5ae9a1001f499be116b5b1bb644c6aec |
|
BLAKE2b-256 | d998878473435798221ab18ad898b77cd73d9fbfccab9445d59f8cdbd8f1b0db |
Hashes for django_tsvector_field-0.9.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3d7c930e3ccf749c5ae8eef997de06c4c8e5bcf00c764cc3a7c06bd128df5fd5 |
|
MD5 | 701fb3dc182da836758068dc09248524 |
|
BLAKE2b-256 | 16a47070e2c6dd5ad3897758c788e79a85197653bdeadde9c57fc3756d6b9403 |