在 django-haystack 中自动完成单词边界
我的网络应用程序中有一个文本字段,允许用户按姓名查找其他人。您开始在框中键入内容,服务器会在您键入时发回可能的匹配项。我使用简单的搜索索引设置了 Haystack/Solr 后端:
class UserIndex(SearchIndex):
text = NgramField(document=True, model_attr='get_full_name')
site.register(BerlinrUser, UserIndex)
然后运行manage.py build_solr_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 来跨单词边界进行匹配,但它似乎没有这样做。有什么想法吗?
I have a text field in my web app that allows users to find other people by name. You start typing in the box and the server sends back possible matches as you type. I set up Haystack / Solr backend with a simple search index:
class UserIndex(SearchIndex):
text = NgramField(document=True, model_attr='get_full_name')
site.register(BerlinrUser, UserIndex)
Then I run manage.py build_solr_schema, copy the schema to my solf/conf directory, restart solr, then finally run manage.py update_index.
In my django view, I have the following search code:
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
The problem is that the autocomplete does not return what I want. Given this collection of users:
John Smith
John Jingleheimer-Schmidt
Jenny Smith
Here is the behavior that I want:
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"
Note, it is acceptable for the query "ohn Smi" to not return anything, as opposed to matching "John Smith".
However, using Haystack/Solr, "Smi", "J", and "John Sm" return no results at all. To get Haystack/Solr to return anything, I have to use whole words. According to the Haystack documentation, I should use the NgramField to match across word boundaries, but it doesn't appear to be doing that. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
找出查询未按预期工作的原因。
我的第一个问题是索引定义。它应该看起来像这样:
另一个问题出在我的 Solr schema.xml 文件中。 minGramSize 设置为“3”,这将阻止 3 个字符以下的查询运行。将其设置为“1”,重新启动 Solr,并重建索引修复了问题。
Found out why the query wasn't working as expected.
My first problem was in the index definition. It should have looked like this:
Another problem was in my Solr schema.xml file. The minGramSize was set to "3", which would prevent queries under 3 characters from working. Setting it to "1", restarting Solr, and rebuilding the index fixed the problem.