PostgreSQL口音+区分大小写搜索 [英] PostgreSQL accent + case insensitive search

查看:180
本文介绍了PostgreSQL口音+区分大小写搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种支持的方式,具有良好的性能,不区分大小写+重音不敏感搜索。到目前为止,我们没有使用MSSql服务器的问题,在Oracle上,我们必须使用 OracleText ,现在我们需要它在PostgreSQL上。



我发现这篇关于重音不敏感的文章:

PostgreSQL是否支持重音不敏感整理?



但是我们需要将它与case insensitve相结合。我们还需要使用索引,否则表演可能会受到影响。
有关大型数据库最佳方法的真实体验?

解决方案

如果您需要与大小写不兼容根据您的具体要求,有很多选项。



也许最简单,使表达式索引不区分大小写。



建立在参考答案中的功能 f_unaccent()的基础上:





  CREATE INDEX users_lower_unaccent_name_idx ON用户( lower( f_unaccent(name));  

然后:

  SELECT * 
FROM users
WHERE lower(f_unaccent(name))= lower(f_unaccent('João'));

可以构建 lower() 进入函数 f_unaccent(),以得到类似于 f_lower_unaccent()

(特别是如果您需要执行模糊模式匹配)可以使用附加模块提供的三元组索引 pg_trgm 构建在上述函数上,这也支持 ILIKE 。详细信息:





我在参考答案



,您可以使用附加模块 citext




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.

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.

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)));

Then:

SELECT *
FROM   users
WHERE  lower(f_unaccent(name)) = lower(f_unaccent('João'));

Or you could build the lower() into the function f_unaccent(), to derive something like f_lower_unaccent().

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:

I added a note to the referenced answer.

Or you could use the additional module citext:

这篇关于PostgreSQL口音+区分大小写搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆