如何使用 Haystack 进行部分字段匹配? [英] How do I do a partial field match using Haystack?

查看:45
本文介绍了如何使用 Haystack 进行部分字段匹配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 django 驱动的网站需要一个简单的搜索工具,所以我选择了 Haystack 和 Solr.我已正确设置所有内容,在输入精确词组时可以找到正确的搜索结果,但在输入部分词组时却找不到任何结果.

I needed a simple search tool for my django-powered web site, so I went with Haystack and Solr. I have set everything up correctly and can find the correct search results when I type in the exact phrase, but I can't get any results when typing in a partial phrase.

例如:John"返回John Doe"但Joh"不返回任何内容.

For example: "John" returns "John Doe" but "Joh" doesn't return anything.

型号:

class Person(models.Model):
    first_name = models.CharField(max_length=50)
    last_name = models.CharField(max_length=50)

搜索索引:

class PersonIndex(SearchIndex):
    text = CharField(document=True, use_template=True)
    first_name = CharField(model_attr = 'first_name')
    last_name = CharField(model_attr = 'last_name')

site.register(Person, PersonIndex)

我猜我缺少一些启用部分字段匹配的设置.我在一些论坛上看到有人在谈论 EdgeNGramFilterFactory(),我也用谷歌搜索过它,但我不太确定它的实现.另外,我希望有一种特定于大海捞针的方法,以防我切换搜索后端.

I'm guessing there's some setting I'm missing that enables partial field matching. I've seen people talking about EdgeNGramFilterFactory() in some forums, and I've Googled it, but I'm not quite sure of its implementation. Plus, I was hoping there was a haystack-specific way of doing it in case I ever switch out the search backend.

推荐答案

您可以通过将索引的文本字段设为 EdgeNgramField 来实现该行为:

You can achieve that behavior by making your index's text field an EdgeNgramField:

class PersonIndex(SearchIndex):
    text = EdgeNgramField(document=True, use_template=True)
    first_name = CharField(model_attr = 'first_name')
    last_name = CharField(model_attr = 'last_name')

这篇关于如何使用 Haystack 进行部分字段匹配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆