我正在玩一个相对简单的Whoosh 2.6和django-haystack 2.3.1的实现来搜索"联系"对象。但是,搜索" mary"只返回我的许多人中的一小部分" Mary"联系人。以下是相关文件:
from django.db.models import Q
from haystack import indexes
from apps.contact.models import Contact
class ContactIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.CharField(document=True, use_template=True)
full_name = indexes.CharField(model_attr='full_name', null=True)
email = indexes.CharField(model_attr='email', null=True)
phone = indexes.CharField(model_attr='phone', null=True)
content_auto = indexes.NgramField(use_template=True)
# several more indexed=False fields to avoid db hits later
def get_model(self):
return Contact
def index_queryset(self, **kwargs):
# one of full_name, email, phone has to be non-null
return self.get_model().objects.filter(
Q(full_name__isnull=False) | Q(email__isnull=False) | Q(phone__isnull=False))
def get_updated_field(self):
return 'date_modified'
{% if object.full_name %}{{ object.full_name }}{% else %}{% endif %}
{% if object.email %}{{ object.email }}{% else %}{% endif %}
{% if object.phone %}{{ object.phone }}{% else %}{% endif %}
def search(request):
sqs = SearchQuerySet()
form = SearchForm(request.POST, searchqueryset=sqs, load_all=False)
if form.is_valid():
return form.search()
相关说明:
select count(*) from contact_contact where lower(full_name) like
'%mary%';
返回97行,而搜索只返回5行。
同样,对于483" john"联系人,搜索返回19。