wagtail search_backend multilanguage

198 Views Asked by At

The wagtail docs say that PostgreSQL search is a good alternative for relative smaller websites. The docs show how to set the search backend.

WAGTAILSEARCH_BACKENDS = {
    'default': {
        'BACKEND': 'wagtail.contrib.postgres_search.backend',
        'SEARCH_CONFIG': 'english',
    }
}

docs wagtail

However the website I'm creating has more then one language. How do I configure the other languages in the WAGTAILSEARCH_BACKEND?

Would 'SEARCH_CONFIG': ['english', 'dutch'] be possible? Docs don't say anything about it.

2

There are 2 best solutions below

0
On

I think I found howto. Would be nice if to see it confirmed. Wagtail has this from wagtail.search.backends import get_search_backend

If you look at the source code you can see that you can define a backend when calling this function

s = get_search_backend(backend=default)

So you can switch backend on the fly by first calling this function and switch to some other backend on basis of the language.

Wagtail is so cool!

0
On

Response to an old question, but maybe someone finds this useful as I just came across a need for this.

After your default (leave as fallback), create a backend config for each language using the language code.

WAGTAILSEARCH_BACKENDS = {
    'default': {
        'BACKEND': 'wagtail.search.backends.database',
        'SEARCH_CONFIG': 'english',
    },
    'en': {
        'BACKEND': 'wagtail.search.backends.database',
        'SEARCH_CONFIG': 'english',
    },
    'nl': {
        'BACKEND': 'wagtail.search.backends.database',
        'SEARCH_CONFIG': 'dutch',
    },
}

Now you can get your backend for current language from

from wagtail.search.backends import get_search_backend    
s = get_search_backend(backend=Locale.get_active().language_code)

These are the default languages in POSTGRES

$ psql -c "\dF"

Schema | Name | Description ------------+------------+--------------------------------------- pg_catalog | danish | configuration for danish language pg_catalog | dutch | configuration for dutch language pg_catalog | english | configuration for english language pg_catalog | finnish | configuration for finnish language pg_catalog | french | configuration for french language pg_catalog | german | configuration for german language pg_catalog | hungarian | configuration for hungarian language pg_catalog | italian | configuration for italian language pg_catalog | norwegian | configuration for norwegian language pg_catalog | portuguese | configuration for portuguese language pg_catalog | romanian | configuration for romanian language pg_catalog | russian | configuration for russian language pg_catalog | simple | simple configuration pg_catalog | spanish | configuration for spanish language pg_catalog | swedish | configuration for swedish language pg_catalog | turkish | configuration for turkish language