如何将不区分重音的搜索添加到 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/