Django 全文搜索:说 unaccent 不存在

Django full text search: says unaccent does not exist

我正在使用 Django 2.0 和 postgresql 9.6

我已经在我的应用程序(文章)中创建并清空了迁移文件,其中包含以下内容以添加 unaccent 和 trigram 扩展名

# Generated by Django 2.0 on 2018-02-06 22:34

from django.db import migrations
from django.contrib.postgres.operations import UnaccentExtension, TrigramExtension


class Migration(migrations.Migration):

    dependencies = [
        ('articles', '0012_auto_20180205_2234'),
    ]

    operations = [
        UnaccentExtension(),
        TrigramExtension()
    ]

然后我尝试运行以下查询:

from django.contrib.postgres.search import SearchQuery, SearchRank, SearchVector
vector = SearchVector('title',config='unaccent', weight='A') + SearchVector('description',config='unaccent', weight='B')
query = SearchQuery('india')
Article.objects.annotate(rank=SearchRank(vector, query)).filter(rank__gte=0.3).order_by('rank')

ProgrammingError: text search configuration "unaccent" does not exist
LINE 1: ...rticle"."qa_bool", ts_rank((setweight(to_tsvector('unaccent'...

我认为您的迁移文件没有在您的数据库中创建指定的 PostgreSQL 扩展。

可能您的数据库用户没有超级用户权限。

Django 文档在 Creating extension using migrations 部分对其进行了解释:

Creating the extension requires a database user with superuser privileges. If the Django database user doesn’t have superuser privileges, you’ll have to create the extension outside of Django migrations with a user that has the appropriate privileges. In that case, connect to your Django database and run the query CREATE EXTENSION IF NOT EXISTS hstore;

更新

我再次阅读了你的代码,我认为问题是 config='unaccent' 你不能使用 unaccent 作为配置,但你必须传递一种语言(es:'italian')或 'simple'