问题描述
我正在寻找一种方法来支持具有良好性能的不区分大小写 + 不区分重音的搜索.到目前为止,我们使用 MSSql 服务器对此没有任何问题,在 Oracle 上我们不得不使用 OracleText
,现在我们在 PostgreSQL 上需要它.
I'm looking for a way to support with good performances case insensitive + accent insensitive search. Till now we had no issue on this using MSSql server, on Oracle we had to use OracleText
, and now we need it on PostgreSQL.
我发现这篇关于重音不敏感的帖子:
PostgreSQL 是否支持accent insensitive"?排序规则?
I've found this post about accent insensitive:
Does PostgreSQL support "accent insensitive" collations?
但我们需要将它与不区分大小写的情况结合起来.我们还需要使用索引,否则性能可能会受到影响.有关大型数据库最佳方法的真实经验吗?
But we need to combine it with case insensitve. We also need to use indexes, otherwise performances could be impacted.Any real experience about the best approach for large databases?
推荐答案
如果您需要结合不区分大小写",有多种选择,具体取决于您的具体要求.
If you need to "combine with case insensitive", there are a number of options, depending on your exact requirements.
也许最简单,让表达式索引不区分大小写.
Maybe simplest, make the expression index case insensitive.
建立在参考答案中列出的函数 f_unaccent()
上:
Building on the function f_unaccent()
laid out in the referenced answer:
CREATE INDEX users_lower_unaccent_name_idx ON users(lower(f_unaccent(name)));
那么:
SELECT *
FROM users
WHERE lower(f_unaccent(name)) = lower(f_unaccent('João'));
或者您可以将 lower()
构建到函数 f_unaccent()
中,以导出类似 f_lower_unaccent()代码>.
Or you could build the lower()
into the function f_unaccent()
, to derive something like f_lower_unaccent()
.
或者(特别是如果您无论如何都需要进行模糊模式匹配)您可以使用附加模块提供的三元索引pg_trgm 建立在上述功能的基础上,它还支持 ILIKE
.详情:
Or (especially if you need to do fuzzy pattern matching anyways) you can use a trigram index provided by the additional module pg_trgm building on above function, which also supports ILIKE
. Details:
我在参考答案中添加了注释.
或者你可以使用附加模块citext:
Or you could use the additional module citext:
这篇关于PostgreSQL 重音 + 不区分大小写的搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!