If I have a simple Django model like:
from model_utils import Choices
class Art(models.Model):
CATEGORIES = Choices(
(1, 'DIGITAL_ART', 'Digital Art'),
(2, 'MULTIMEDIA', 'Multimedia'),
)
title = models.TextField()
category = models.PositiveSmallIntegerField(
db_index=True, choices=CATEGORIES, blank=True, null=True
)
How can I use SearchVector
in postgres to allow for searching on both the title and categories fields? E.g. so "Some Book Digital Art" would query over both the title and categories fields.
The problem is that at the database-level the choices field is stored as an integer, not text.
Is there a way I could just map the integer to the corresponding text value when creating my search vector?
The first solution I can think is using Conditional Expression to add the category text into the SearchVector:
The resuklt of this query will be similar to:
And the SQL generated for PostgreSQL like this:
Update
You can automatically build your query from your choices list: