在Django中使用SearchVector和SearchQuery来实现unaccent [英] Using unaccent with SearchVector and SearchQuery in Django
问题描述
我已经在Django中安装了UnaccentExtension,但是我在使用它进行搜索时遇到了问题:
$ b
vector = SearchVector(' title__unaccent','abstract__unaccent')
query = SearchQuery(word)| SearchQuery(word2)
files = Doc.objects.annotate(rank = SearchRank(vector,query))。order_by(' - rank')
这是错误:
无法将关键字'unaccent'解析为字段。加入'标题'不允许。
使用最简单的搜索工作正常:
Doc.objects.filter(title__unaccent = word)
<所以,我做错了什么?
你不能在'SearchVector'中使用'unaccent',但你有在PostgreSQL中定义一个新的非空闲配置。
- 如果您错过了安装非扩展名。
-
在PostgrSQL中创建您的不存在的字典或使用空迁移与此SQL:
创建文本搜索配置french_unaccent(COPY = french);
更改文本搜索配置RATION french_unaccent
修改hword,hword_part,word
与unaccent,french_stem;
-
在Django查询中使用此配置:
/ b>
SearchVector('title','abstract',config ='french_unaccent')
SearchQuery ='french_unaccent')
您可以在官方PostgreSQL文档在各种 articles
I have installed UnaccentExtension in Django but I'm having problems using it with this search:
vector = SearchVector('title__unaccent', 'abstract__unaccent')
query = SearchQuery(word) | SearchQuery(word2)
files = Doc.objects.annotate(rank=SearchRank(vector, query)).order_by('-rank')
This is the error:
Cannot resolve keyword 'unaccent' into field. Join on 'title' not permitted.
Whit a simplest search it works fine:
Doc.objects.filter(title__unaccent=word)
So, what am I doing wrong?
You can't use 'unaccent' in 'SearchVector' but you have to define a new "unaccented" config in PostgreSQL.
- If you missed, installs the unaccent extension.
Create your unaccented dictionary in PostgrSQL or using an empty migrations with this 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;
Use this configuration in your Django query :
SearchVector('title','abstract', config='french_unaccent')
SearchQuery(word, config='french_unaccent')
You can find more info about this type of configuration in the official PostgreSQL documentation on in various articles
这篇关于在Django中使用SearchVector和SearchQuery来实现unaccent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!