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,则该值会上升到第二位。但它仍然不正确,因为以查询开头的单词的值应该排在第一位。
最佳答案
您不能使用 CharField
与 sqs.autocomplete()
.来自 documentation .
You have two choices:
NgramField
andEdgeNgramField
. 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/