我正在尝试在我的服务器上进行自动完成以进行搜索。这是我的索引器类之一的示例:
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"
和字段使用模板填充,在 Artsts 的情况下,模板只是艺术家姓名text
。content_auto
根据文档,这样的东西应该适用于自动完成:
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 但这给了我类似的结果。