Django Haystack:搜索有或没有重音的词 [英] Django Haystack : search for a term with and without accents

查看:116
本文介绍了Django Haystack:搜索有或没有重音的词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用django haystack在我的django项目上实施一个搜索系统。问题是,我的模型中的某些字段有一些法国口音,我想找到含有和没有重音的查询内容的条目。



我认为最好的想法是创建一个具有重音符的字段的SearchIndex,并且没有重音的相同字段。



有什么想法或提示?



以下是一些代码



想像下面的模型:

  Cars(models.Model):
name = models.CharField()

和以下干草堆索引:

  Cars(indexes.SearchIndex):
name = indexes.CharField(model_attr ='name')
cleaning_name = indexes.CharField(model_attr ='name')

def prepare_cleaned_name(self,object):
return strip_accents(object.name)

现在,在我的索引模板中,我把这两个字段: p>

  {{object.cleaned_name}} 
{{object.name}}

所以,这是一些伪代码,我不知道它是否有效,但如果你有任何想法,让我知道! p>

解决方案

我找到一种方法来从我的模型中的同一个字段中的两个值进行索引。


$ b $首先,在你的模型中写一个返回t的方法他的字段值:

  class Car(models.Model):
name = model.CharField()

def ascii_name(self):
return strip_accents(self.name)

所以在您用于生成索引的模板中,您可以执行以下操作:

  {{object.name} } 
{{object.ascii_name}}

然后,你只需要重建索引!


I'm implementing a search system onto my django project, using django haystack. The problem is that some fields in my models have some french accents, and I would like to find the entries which contents the query with and without accents.

I think the best Idea is to create a SearchIndex with both the fields with the accents, and the same field without the accents.

Any idea or hint on this ?

Here is some code

Imagine the following models :

Cars(models.Model):
    name = models.CharField()

and the following Haystack Index:

Cars(indexes.SearchIndex):
    name = indexes.CharField(model_attr='name')
    cleaned_name = indexes.CharField(model_attr='name')

    def prepare_cleaned_name(self, object):
        return strip_accents(object.name)

now, in my index template, I put the both fields :

{{ object.cleaned_name }}
{{ object.name }}

So, thats some pseudo code, I don't know if it works, but if you have any idea on this, let me know !

解决方案

I find a way to index both value from the same field in my Model.

First, write a method in your model which returns the ascii value of the fields:

class Car(models.Model):
    name = model.CharField()

    def ascii_name(self):
        return strip_accents(self.name)

So that in your template used to generate the index, you could do this:

{{ object.name }}
{{ object.ascii_name }}

Then, you just have to rebuild your indexes !

这篇关于Django Haystack:搜索有或没有重音的词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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