How use `unaccent` with full text search in django 1.10?

后端 未结 1 692
不思量自难忘°
不思量自难忘° 2021-02-03 14:07

We are working on a project and we are using Django 1.10a1, we\'re using django full text search with PostgreSQL but we need to use unaccent.

So, I have thi

相关标签:
1条回答
  • 2021-02-03 15:04

    Install the unaccent extension into your database:

    mydb=# CREATE EXTENSION unaccent;
    

    Create a new search configuration, based on an other one:

    mydb=# CREATE TEXT SEARCH CONFIGURATION french_unaccent( COPY = french );
    

    Insert the unaccent dictionary into your new search configuration:

    mydb=# ALTER TEXT SEARCH CONFIGURATION french_unaccent
        ALTER MAPPING FOR hword, hword_part, word
        WITH unaccent, french_stem;
    

    Use this configuration in your Django query :

    search = 'Car'
    query_set = Article.objects.annotate(
                    search=SearchVector('content','name', config='french_unaccent')
                ).filter(search=SearchQuery(search, config='french_unaccent')))
    
    0 讨论(0)
提交回复
热议问题