gpt4 book ai didi

django - 在 Django 中将 unaccent 与 SearchVector 和 SearchQuery 结合使用

转载 作者:行者123 更新时间:2023-11-29 11:29:13 33 4
gpt4 key购买 nike

我已经在 Django 中安装了 UnaccentExtension,但是我在搜索时遇到了问题:

vector = SearchVector('title__unaccent', 'abstract__unaccent')
query = SearchQuery(word) | SearchQuery(word2)
files = Doc.objects.annotate(rank=SearchRank(vector, query)).order_by('-rank')

这是错误:

Cannot resolve keyword 'unaccent' into field. Join on 'title' not permitted.

只需一个最简单的搜索就可以正常工作:

Doc.objects.filter(title__unaccent=word)

那么,我做错了什么?

最佳答案

您不能在“SearchVector”中使用“unaccent”,但您必须在 PostgreSQL 中定义一个新的“unaccented”配置。

  1. 如果您错过了,installs the unaccent extension .
  2. 在 PostgrSQL 中或使用 empty migrations 创建您的无重音字典使用此 SQL:

    CREATE TEXT SEARCH CONFIGURATION french_unaccent( COPY = french );
    ALTER TEXT SEARCH CONFIGURATION french_unaccent
    ALTER MAPPING FOR hword, hword_part, word
    WITH unaccent, french_stem;
  3. 在您的 Django 查询中使用此配置:

    SearchVector('title','abstract', config='french_unaccent')
    SearchQuery(word, config='french_unaccent')

您可以在 official PostgreSQL documentation 中找到有关此类配置的更多信息在各种articles

关于django - 在 Django 中将 unaccent 与 SearchVector 和 SearchQuery 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47230566/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com