【发布时间】:2011-08-18 22:59:24
【问题描述】:
我的网络应用程序中有一个文本字段,允许用户按姓名查找其他人。您开始在框中输入内容,服务器会在您输入内容时发回可能的匹配项。我用一个简单的搜索索引设置了 Haystack / Solr 后端:
class UserIndex(SearchIndex):
text = NgramField(document=True, model_attr='get_full_name')
site.register(BerlinrUser, UserIndex)
然后我运行manage.py build_solr_schema,将schema复制到我的solf/conf目录,重启solr,最后运行manage.py update_index。
在我的 django 视图中,我有以下搜索代码:
q = request.GET.get("q")
search_result = SearchQuerySet().models(User).autocomplete(content=q)
for result in search_result:
# Collect user IDs, pull from database, and send back a JSON result
问题是自动完成没有返回我想要的。鉴于此用户集合:
John Smith
John Jingleheimer-Schmidt
Jenny Smith
这是我想要的行为:
Query: Expected result:
"John" "John Smith",
"John Jingleheimer-Schmidt"
"Smith" "John Smith",
"Jenny Smith"
"Smi" "John Smith",
"Jenny Smith"
"J" <Anybody with a first or last name that begins with "J">
"John Sm" "John Smith"
请注意,查询“ohn Smi”可以不返回任何内容,而不是匹配“John Smith”。
但是,使用 Haystack/Solr,“Smi”、“J”和“John Sm”根本不会返回任何结果。为了让 Haystack/Solr 返回任何东西,我必须使用整个单词。根据 Haystack 文档,我应该使用 NgramField 来匹配单词边界,但它似乎没有这样做。有什么想法吗?
【问题讨论】:
标签: django autocomplete django-haystack