多语言django网站的搜索功能 [英] search functionality on multi-language django site

查看:100
本文介绍了多语言django网站的搜索功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在建立一个多语言Django网站,我使用的是 django-transmeta 我的模型数据翻译。现在我想知道是否有一个适用于多语言模型的Django搜索应用程序。我已经玩过干草堆,它可以适用于单一语言网站,但我无法让它处理transmeta的元类...



有人有任何经验吗?任何指针都将不胜感激!



欢呼,



martin

解决方案

这不仅仅是一个完整的解决方案的起点,但我希望它有所帮助,而其他用户
可以改进这个想法并达成更好的解决方案。 p>

使用干草堆索引多语言网站(使用django-transmeta或django多语言),您面临两个问题:


  1. 如何索引所有
    语言的内容

  2. 如何根据
    搜索查询
    正确的索引选择语言

1)索引所有语言的内容



在每个SearchIndex模型中为每种语言创建一个单独的字段,使用通用前缀
和语言代码:

 code> text_en = indexes.CharField(model_attr ='body_en',document = True)
text_pt = indexes.CharField(model_attr ='body_pt')
pre>

如果要索引几个字段,您可以显然使用一个模板。只有一个索引可以具有document = True。



如果您需要预渲染 http://haystacksearch.org/docs/searchindex_api.html 字段为
更快的显示,您应该为每种语言创建一个(即,rendered_en,rendered_pt)



2)查询正确的索引



默认的haystack auto_query方法被编程为接收请求
上的q查询参数,并在所有索引模型中搜索content索引字段 - 标记为document = True的字段。
只有其中一个索引可以具有document = True,我相信每个django模型只能有一个SearchIndex



最简单的解决方案使用通用搜索表单,是创建一个多语言搜索集
,基于过滤器,而不是基于内容,但是在text_(文本是上面使用的Searchindex模型
上使用的前缀)

$ b从django.conf导入设置
$ b

 来自django.utils.translation的
import get_language
from haystack.query import SearchQuerySet,DEFAULT_OPERATOR

class MlSearchQuerySet(SearchQuerySet):
def filter(self,** kwargs):
根据某些属性和默认运算符缩小搜索。
如果kwargs中的'content':
kwd = kwargs.pop('content')
kwdkey =text_%s%str(get_language())
kwargs [kwdkey] = kwd
if getattr(settings,'HAYSTACK_DEFAULT_OPERATOR',DEFAULT_OPERATOR)=='OR :
return self.filter_or(** kwargs)
else:
return self.filter_and(** kwargs)

,并将搜索网址指向使用此查询集的视图:

 来自haystack.forms import ModelSearchForm 
from haystack.views import SearchView

urlpatterns + = patterns('haystack.views',
url(r'^ search / $',SearchView (
searchqueryset = MlSearchQuerySet(),
form_class = ModelSearchForm
),name ='haystack_search_ml'),

现在您的搜索应该知道所选语言。


I'm building a multi-language Django site, and I'm using django-transmeta for my model data translations. Now I'm wondering if there is a Django search app that works with multi-language models. I've played with Haystack and it works fine for single-language sites, but I can't get it to work with transmeta's metaclasses...

Does anybody have any experience with this? Any pointers would be appreciated!

cheers,

martin

解决方案

This more of a starting point than a full solution, but I hope it help and that other users can improve this idea and reach a better solution.

Using Haystack to index a multilingual site (using django-transmeta or django-multilingual) you face two problems:

  1. how to index the content for all the languages
  2. how to search the query the correct index depending on the selected languages

1) Index the content for all the languages

Create a separate fields for each language in every SearchIndex model, using a common prefix and the language code:

text_en = indexes.CharField(model_attr='body_en', document=True)
text_pt = indexes.CharField(model_attr='body_pt')

If you want to index several fields you can obviously use a template. Only one of the indexes can have document=True.

If you need pre-rendered http://haystacksearch.org/docs/searchindex_api.html field for faster display, you should create one for each language (ie, rendered_en, rendered_pt)

2) Querying the correct index

The default haystack auto_query method is programmed to receive a "q" query parameter on the request and search the "content" index field - the one marked as document=True - in all the Index models. Only one of the indexes can have document=True and I believe we can only have a SearchIndex for each django Model.

The simplest solution, using the common search form, is to create a Multilingual SearchQuerySet that filters based, not on content, but on text_ (text being the prefix used on the Searchindex model above)

from django.conf import settings
from django.utils.translation import get_language
from haystack.query import SearchQuerySet, DEFAULT_OPERATOR

class MlSearchQuerySet(SearchQuerySet):
    def filter(self, **kwargs):
        """Narrows the search based on certain attributes and the default operator."""
        if 'content' in kwargs:
            kwd = kwargs.pop('content')
            kwdkey = "text_%s" % str(get_language())
            kwargs[kwdkey] = kwd
        if getattr(settings, 'HAYSTACK_DEFAULT_OPERATOR', DEFAULT_OPERATOR) == 'OR':
           return self.filter_or(**kwargs)
        else:
            return self.filter_and(**kwargs)

and point your search URL to a view that uses this query set:

from haystack.forms import ModelSearchForm
from haystack.views import SearchView

urlpatterns += patterns('haystack.views',
    url(r'^search/$', SearchView(
        searchqueryset=MlSearchQuerySet(),
        form_class=ModelSearchForm
    ), name='haystack_search_ml'),
)

Now your search should be aware of the selected language.

这篇关于多语言django网站的搜索功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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