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

点点圈 提交于 2021-01-20 16:16:17

问题


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 this code:

        search = 'Car'
        query_set = Article.objects.annotate(
            search=SearchVector(
                'content',
                'name'
                )
            ).filter(
            search__unaccent=search
        )

When we tried adding unaccent word after search the query doesn't work. How can we get django full text search to work with postgress unaccent?


回答1:


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


来源:https://stackoverflow.com/questions/37818492/how-use-unaccent-with-full-text-search-in-django-1-10

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!