您好,我正在尝试在 GIN 索引列上使用 postgresql/FTS 在 Django 上进行搜索,但出现了一个奇怪的错误。此错误不会出现在 CharField 上,而只会出现在 SearchVectorField 上:
Postgresql 数据库安装了 pg_trgm 扩展(在 Django 迁移中)
这是对象:
class Language(models.Model):
code = models.CharField(primary_key=True, max_length=5, verbose_name=_("code"))
label = models.CharField(max_length=50, verbose_name=_("label"))
search = SearchVectorField(null=True)
objects = Language_Manager()
class Meta:
verbose_name = _("language")
verbose_name_plural = _("languages")
indexes = [
GinIndex(fields=["search"], name="search_index",),
]
def save(self):
fields = [x for x in dir(self) if x[:5] == "label"]
labels = [getattr(self, field) for field in fields if getattr(self, field)]
self.search = self.search + " ".join(labels)
super().save()
这是我的查询:
from core.models import Language as LanguageCode
from django.contrib.postgres.search import TrigramSimilarity
LanguageCode.objects.all().annotate(sim=TrigramSimilarity("search", "english"))
它返回一条错误消息:
LINE 1: ...e_language"."label_ko", "core_language"."search", SIMILARITY...
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
如果我使用标准CharField而不是 GIN 索引SearchVectorField,则不会发生错误:
from core.models import Language as LanguageCode
from django.contrib.postgres.search import TrigramSimilarity
LanguageCode.objects.all().annotate(sim=TrigramSimilarity("label", "english"))
<MultilingualQuerySet [<Language: Language object (fr)>, <Language: Language object (en)>, >><Language: Language object (it)>, ...
难道我做错了什么 ?
我也在使用 Django-model-translation 和这个对象的标签列:
from modeltranslation.translator import translator, TranslationOptions
from core.models import Language
class LanguageTranslationOptions(TranslationOptions):
fields = ("label",)
translator.register(Language, LanguageTranslationOptions)