我正在使用Django 2.0和postgres(PostgreSQL)9.6.1
我正在使用带有标题和body_text的以下模型:
class Entry(models.Model):
title = models.CharField(max_length=255)
description = models.TextField()
def __str__(self):
return self.title
以下是我的内容
headline: Dhṛtarāṣṭra inquired from Sañjaya,
body_text:
Prabhupāda: So Dhṛtarāṣṭra inquired from Sañjaya, kim akurvata: "After my sons and my brother's sons assembled together for fighting, what did they do?" This was the inquiry. So to encourage him... Because Sañjaya could understand the feelings of his master that he wanted the fight, no compromise, kṣatriya spirit, "Let my sons and my brother's sons fight..." That is kṣatriya spirit.
此外,我已按以下方式安装了非爆炸扩展程序:
# 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()
]
扩展已成功安装。现在我想搜索重音词Dhṛtarāṣṭra。所以我正在尝试以下方法:
from django.contrib.postgres.search import SearchQuery, SearchRank, SearchVector
vector = SearchVector('title__unaccent', weight='A') + SearchVector('description__unaccent', weight='B')
query = SearchQuery('Dhrtarastra')
Article.objects.annotate(rank=SearchRank(vector, query)).filter(rank__gte=0.3).order_by('rank')
: Cannot resolve keyword 'unaccent' into field. Join on 'title' not permitted.
我在这里读到:Using unaccent with SearchVector and SearchQuery in Django:那个
你不能在'SearchVector'中使用'unaccent',但你必须定义一个 PostgreSQL中新的“unaccented”配置。
在PostgrSQL中创建非重音字典或使用空字符 使用此SQL进行迁移:
创建文本搜索配置french_unaccent(COPY =法语);
ALTER TEXT SEARCH CONFIGURATION french_unaccent ALTER MAPPING FOR hword,hword_part,word with unaccent,french_stem;
此处使用COPY = french
。在我的情况下,我应该使用哪个字典。
答案 0 :(得分:0)
这些是PostgreSQL支持的词典:
您可以从PostgreSQL source code验证它。