我在Postgres中安装了unaccent扩展,简单的过滤器在我的Django应用程序中工作得很好,例如:
q = 'hello'
queryset.filter(name__unaccent__startswith=q)我现在正在尝试使用搜索索引来注释查询集结果:
queryset.annotate(search_index=StrIndex(Lower('name'), Value(q)))对于无重音的文本,这本身工作得很好,但我正在尝试找出一种将UNACCENT应用于name变量的方法。本质上:
SELECT
-- This is what I want!
STRPOS(LOWER(UNACCENT(core_ingredient.name)::text), 'hello') AS search_index_unaccented,
STRPOS(LOWER(core_ingredient.name), 'hello') AS search_index_basic
FROM
-- ...我试过了:
# This has no effect, gives same query / result as above
queryset.annotate(search_index=StrIndex(Lower('name__unaccent'), Value(q)))我见过这样的答案:How use unaccent with full text search in django 1.10?,但我觉得这不是必需的。
发布于 2020-01-06 03:47:38
经过额外的挖掘,我能够做到这一点:
from django.contrib.postgres.lookups import Unaccent
queryset.annotate(search_index=StrIndex(Unaccent(Lower('name')), Value(q)))在Django中似乎没有很好的文档记录,但在SQL中可以正常工作:
STRPOS(UNACCENT(LOWER("core_ingredient"."name")), 'hello') AS "search_index"https://stackoverflow.com/questions/59603507
复制相似问题