【问题标题】:Autocomplete across word bounaries in django-haystack在 django-haystack 中跨单词边界自动完成
【发布时间】: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


    【解决方案1】:

    找出查询未按预期工作的原因。

    我的第一个问题是索引定义。它应该看起来像这样:

    class UserIndex(SearchIndex):
        text = CharField(document=True)
        name_auto = NgramField(model_attr='get_full_name')
    

    另一个问题是在我的 Solr schema.xml 文件中。 minGramSize 设置为“3”,这将阻止 3 个字符以下的查询工作。将其设置为“1”,重新启动 Solr,然后重建索引即可解决问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-06-24
      • 1970-01-01
      • 2015-05-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多