django 为什么TrigramSimilarity在postgresql上不工作

jjhzyzn0  于 2023-05-13  发布在  Go
关注(0)|答案(1)|浏览(131)

我想改变搜索排名与三角相似性,但我得到了这个错误
/search/ function similarity(character varying,unknown)does not exist行1:...ows_show”.“description”,'')),'B'))AS“search”,SIMILARITY...^提示:没有函数匹配给定的名称和参数类型。您可能需要添加显式类型强制转换。
我的代码:

def search_content(query: str) -> QuerySet | None:

if query:
    search_vector = SearchVector("name", weight="A") + SearchVector("description", weight="B")

    shows = (
        Show.objects.annotate(
            search=search_vector, similarity=TrigramSimilarity('name', query),
        )
        .filter(similarity__gt=0.4)
        .values("id", "name", "baner", "release_date", "search", "similarity")
    )
    
    movies = (
        Movie.objects.annotate(
            search=search_vector, similarity=TrigramSimilarity('name', query),
        )
        .filter(similarity__gt=0.4)
        .values("id", "name", "baner", "release_date", "search", "similarity")
    )
    
    episodes = (
        Episode.objects.annotate(
            search=search_vector, similarity=TrigramSimilarity('name', query),
        )
        .filter(similarity__gt=0.4)
        .values("id", "name", "baner", "release_date", "search", "similarity")
    )

    qs = shows.union(movies, episodes).order_by("-similarity")

    return qs

else:
    return None
lmvvr0a8

lmvvr0a81#

似乎你的数据库中没有包含该函数的扩展。问题a
select * from pg_extension;
如果没有出现一个名为pg_trgm的文件,则使用
create extension pg_trgm;
在官方文档中使用django与trigram相似

相关问题