Django Haystack 自动完成功能无法按预期工作

标签 django elasticsearch autocomplete django-haystack

Django==1.8.13, Elasticsearch ==1.7.1

我正在名称字段上实现自动完成。

sqs = sqs.autocomplete(name=query)

名称搜索索引:
name = indexes.CharField(model_attr='name', boost=1.2)

没有对查询集进行排序。

搜索查询:'字体'

结果:

-> 很棒的字体

-> 其他一些值

-> Font Awesome

预期结果:

-> Font Awesome

-> xxxx(其他值)

-> xxxx(其他值)

为什么“Font Awesome”没有排在列表的首位?

如果我使用 NgramField 而不是 CharField,则该值会上升到第二位。但它仍然不正确,因为以查询开头的单词的值应该排在第一位。

最佳答案

您不能使用 CharFieldsqs.autocomplete() .来自 documentation .

You have two choices: NgramField and EdgeNgramField. Though very similar, the choice of field is somewhat important.

  • If you’re working with standard text, EdgeNgramField tokenizes on whitespace. This prevents incorrect matches when part of two different words are mashed together as one n-gram. This is what most users should use.
  • If you’re working with Asian languages or want to be able to autocomplete across word boundaries, NgramField should be what you use.


要么你需要改变你的name EdgeNgramField 的索引字段或(如果您在其他地方使用 name)创建一个用于自动完成搜索的单独字段,例如:
autocomplete_name = indexes.EdgeNgramField(model_attr='name')

然后查询:
sqs = sqs.autocomplete(autocomplete_name=query)

这应该会给你预期的结果。

关于Django Haystack 自动完成功能无法按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38976687/

相关文章:

mysql - 为什么从 Django pre_save 信号触发的 celery 任务永远看不到对象

elasticsearch - 如何只返回嵌套字段中匹配的对象,而不返回整个对象?

elasticsearch - 优先考虑某些字段的ES搜索结果

jquery - ASP.NET MVC jQuery 自动完成 HtmlHelper

autocomplete - 加载建议时 XPages Type Ahead 失败

python - 如何将类添加到 Django 国家/地区小部件

Django:验证多个字段

elasticsearch - 类型错误 : __init__() missing 1 required positional argument: 'content'

javascript - 由 Google 提供支持的 Logo 未显示在地址自动完成字段中

python - 在 Python 中将元组传递给 __init__