Django 全文搜索:TrigramDistance 不工作
Django full text search: TrigramDistance not working
我正在使用 Django 2.0 和 postgresql 9.6。
我已经在 postgresql
中安装了 pg_trgm 扩展
我正在尝试了解三字母组合的工作原理:我正在尝试以下操作。我希望使用 triagram
匹配更接近 "sridam" 的单词
from django.contrib.postgres.search import TrigramDistance
from django.contrib.postgres.search import SearchQuery, SearchRank, SearchVector
vector = SearchVector('title',config='english_unaccent', weight='A') + SearchVector('description',config='english_unaccent', weight='B')
query = SearchQuery('sridam',config='english_unaccent')
Article.objects.annotate(distance=TrigramDistance(vector, query)).filter(distance__lte=0.7).order_by('distance')
ProgrammingError: operator does not exist: tsvector <-> tsquery
LINE 1: ...SCE("articles_article"."description", '')), 'B')) <-> plaint...
^
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
您无法计算 SearchVector
和 SearchQuery
之间的三连词距离,因为 "A trigram is a group of three consecutive characters.".
如Django documentationTrigramDistance
"accepts a field name or expression, and a string or expression. Returns the Trigram distance between the two arguments.".
在使用 postgres 扩展之前,您必须将扩展迁移应用到您的模型。
解决方案 1
制作空迁移文件:
python3 manage.py makemigrations --empty yourapp
然后转到新的迁移文件并添加以下内容
from django.db import migrations
from django.contrib.postgres.operations import TrigramExtension
class Migration(migrations.Migration):
operations = [
TrigramExtension()
]
最后 运行 python3 manage.py migrate
将迁移应用到您的数据库,您就可以使用扩展了。
解决方案 2
您可以转到 psql
控制台并直接应用迁移:
CREATE EXTENSION pg_trgm;
我正在使用 Django 2.0 和 postgresql 9.6。
我已经在 postgresql
中安装了 pg_trgm 扩展我正在尝试了解三字母组合的工作原理:我正在尝试以下操作。我希望使用 triagram
匹配更接近 "sridam" 的单词from django.contrib.postgres.search import TrigramDistance
from django.contrib.postgres.search import SearchQuery, SearchRank, SearchVector
vector = SearchVector('title',config='english_unaccent', weight='A') + SearchVector('description',config='english_unaccent', weight='B')
query = SearchQuery('sridam',config='english_unaccent')
Article.objects.annotate(distance=TrigramDistance(vector, query)).filter(distance__lte=0.7).order_by('distance')
ProgrammingError: operator does not exist: tsvector <-> tsquery
LINE 1: ...SCE("articles_article"."description", '')), 'B')) <-> plaint...
^
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
您无法计算 SearchVector
和 SearchQuery
之间的三连词距离,因为 "A trigram is a group of three consecutive characters.".
如Django documentationTrigramDistance
"accepts a field name or expression, and a string or expression. Returns the Trigram distance between the two arguments.".
在使用 postgres 扩展之前,您必须将扩展迁移应用到您的模型。
解决方案 1
制作空迁移文件:
python3 manage.py makemigrations --empty yourapp
然后转到新的迁移文件并添加以下内容
from django.db import migrations
from django.contrib.postgres.operations import TrigramExtension
class Migration(migrations.Migration):
operations = [
TrigramExtension()
]
最后 运行 python3 manage.py migrate
将迁移应用到您的数据库,您就可以使用扩展了。
解决方案 2
您可以转到 psql
控制台并直接应用迁移:
CREATE EXTENSION pg_trgm;