I'd like to use full text search on my database and choose which columns to search through. Is there a way to search through a specific column (like only fruits) when I only I have one search_vector field in my Food model that contains all the columns (fruits, vegetables, dairy). I'd also like to search through all the columns so that's why I combined all the search vectors.
I am using Django 1.11
Here's my model:
class Food(models.Model):
fruits = models.CharField(max_length=50)
vegetables = models.CharField(max_length=50)
dairy = models.CharField(max_length=50)
search_vector = SearchVectorField(null=True)
Here's how I store the vectors:
vector= SearchVector('fruits', weight='A') + \
SearchVector('vegetables', weight='B') + \
SearchVector('dairy', weight='C') + \
Food.objects.annotate(document=vector).values_list('document', flat=True)
for f in Food.objects.annotate(document=vector):
f.search_vector = f.document
f.save(update_fields=['search_vector'])
Here's how I search:
search_result = Food.objects\
.annotate(rank=SearchRank(F('search_vector'), "apple"))\
.filter(search_vector="apple")\
Is there an easy way of only searching in fruit column in the search_vector that I set up, or would I have to set up a separate search vector for each column?