【发布时间】:2021-12-29 15:37:47
【问题描述】:
假设我有一个像这样的 Person 模型:
class Person(models.Model):
title = models.CharField(max_length=300, blank=True, null=True)
first_name = models.CharField(max_length=300, blank=True, null=True)
last_name = models.CharField(max_length=300, blank=False, null=False)
还有一个像这样的 Haystack 搜索索引:
class NoteIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.CharField(document=True, use_template=True)
title = indexes.CharField(model_attr='title')
first_name = indexes.CharField(model_attr='first_name')
last_name = indexes.CharField(model_attr='last_name')
假设模板包含所有三个字段(名字、姓氏和标题)。如果用户应该能够选择要搜索的字段,我是否需要为所有可能的排列建立单独的索引?或者我可以将搜索范围缩小到三个字段中的一个或两个?
如果有怎么办?
我知道我可以使用 .filter() 过滤 SearchQuerySets。
SearchQuerySet().filter(first_name='foo')
但这会过滤集合以获得精确值。甚至__icontains 也仅限于一个字段过滤。
【问题讨论】:
标签: django search django-haystack