如何使用干草堆进行局部场地比赛? [英] How do I do a partial field match using Haystack?

查看:98
本文介绍了如何使用干草堆进行局部场地比赛?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个简单的搜索工具,我的django供电的网站,所以我去干草堆和索尔。我已经正确设置所有内容,并且可以在输入确切短语时找到正确的搜索结果,但是在输入部分短语时我无法获得任何结果。



例如:John返回John Doe,但Joh不返回任何内容。



型号:

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

搜索索引:



$ _ code 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(),而且我已经Google Googled了,但我不太确定它的实现。另外,我希望有一个干草堆特定的方法,以防万一我退出搜索后端。

解决方案

您可以通过使索引的文本字段为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')
/ pre>

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.

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

Model:

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

Search Index:

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)

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.

解决方案

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')

这篇关于如何使用干草堆进行局部场地比赛?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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