【发布时间】:2014-07-24 14:53:35
【问题描述】:
我正在尝试在我的服务器上进行自动完成以进行搜索。这是我的索引器类之一的示例:
class ArtistIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.CharField(document=True, use_template=True)
artist_name = indexes.CharField(model_attr='clean_artist_name', null=True)
submitted_date = indexes.DateTimeField(model_attr='submitted_date')
total_count = indexes.IntegerField(model_attr='total_count')
# This is used for autocomplete
content_auto = indexes.NgramField(use_template=True)
def get_model(self):
return Artist
def index_queryset(self, using=None):
""" Used when the entire index of a model is updated. """
return self.get_model().objects.filter(date_submitted__lte=datetime.now())
def get_updated_field(self):
return "last_data_change"
text 和 content_auto 字段使用模板填充,在 Artsts 的情况下,模板只是艺术家姓名。根据文档,这样的东西应该适用于自动完成:
objResultSet = SearchQuerySet().models(Artist).autocomplete(content_auto=search_term)
但是,使用字符串“bill w”尝试此操作会返回 Bill Stephney 作为顶部结果,然后是 Bill Withers 作为第二个结果。这是因为 Bill Stephney 在数据库中有更多记录,但 Stephney 不应该匹配这个查询:一旦检测到“w”,它应该只匹配 Bill Withers(和其他 Bill Ws)。我也尝试过通配符:
objResultSet = SearchQuerySet().models(Artist).filter(content_auto=search_term + '*')
和
objResultSet = SearchQuerySet().models(Artist).filter(text=AutoQuery(search_term + '*'))
但通配符似乎会导致大量问题,由于带有神秘堆栈跟踪的Write Failed: Broken Pipe 错误,开发服务器挂起并最终停止,所有这些都在 Python 框架内。有没有人设法让它正常工作? NgramField 是正确使用的类型吗?我尝试过使用 EdgeNgramField,但结果相似。
【问题讨论】:
标签: python django autocomplete django-haystack xapian