python - 如何在 Django 中进行不区分重音的 TrigramSimilarity 搜索?

标签 python django postgresql django-postgresql

如何将不区分重音的搜索添加到 django docs 中的以下代码片段中:

>>> from django.contrib.postgres.search import TrigramSimilarity
>>> Author.objects.create(name='Katy Stevens')
>>> Author.objects.create(name='Stephen Keats')
>>> test = 'Katie Stephens'
>>> Author.objects.annotate(
...     similarity=TrigramSimilarity('name', test),
... ).filter(similarity__gt=0.3).order_by('-similarity')
[<Author: Katy Stevens>, <Author: Stephen Keats>]

这如何匹配 test = 'Kâtié Stéphèns'

最佳答案

存在 unaccent查找:

The unaccent lookup allows you to perform accent-insensitive lookups using a dedicated PostgreSQL extension.

此外,如果您查看 aggregation django 文档的一部分,您可以阅读以下内容:

When specifying the field to be aggregated in an aggregate function, Django will allow you to use the same double underscore notation that is used when referring to related fields in filters. Django will then handle any table joins that are required to retrieve and aggregate the related value.


从上面得出:

您可以使用 trigram_similar查找,结合unaccent,然后对结果进行注释:

Author.objects.filter(
    name__unaccent__trigram_similar=test
).annotate(
    similarity=TrigramSimilarity('name__unaccent', test),
).filter(similarity__gt=0.3).order_by('-similarity')

如果你想让它尽可能接近原始样本(并省略一个可能较慢的过滤,然后再省略另一个):

Author.objects.annotate(
    similarity=TrigramSimilarity('name__unaccent', test),
).filter(similarity__gt=0.3).order_by('-similarity')

那些只适用于 Django 版本 >= 1.10


编辑:

虽然上面应该可以工作,@Private 报告发生了这个错误:

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

这可能是一个错误,或者 unaccent 不打算以这种方式工作。下面的代码没有错误:

Author.objects.filter(
    name__unaccent__trigram_similar=test
).annotate(
    similarity=TrigramSimilarity('name', test),
).filter(similarity__gt=0.3).order_by('-similarity')

关于python - 如何在 Django 中进行不区分重音的 TrigramSimilarity 搜索?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43844295/

相关文章:

python - 如何让 pyCharm 停止隐藏(展开)我的 Python 导入?

python - 连接到 dask.distributed 集群时出现 Pickle 错误

python - 为什么我的链接在点击后无法导航到我想要的页面?

python - Python/Django “if statement”语法不正确?

python - 为什么无法在 Mac 上添加 Azure DB 扩展?

arrays - 如何循环遍历 JSONb 字段中包含的数组?

python - 在 GAE 中组合文本搜索和查询过滤器

python - emacs `python-shell-send-defun` 跳过缓冲区中的第一行

postgresql - JdbcTemplate 和 inet 数据类型

sql - 使用 Postgres 一次在 3 个表中插入数据