Does PostgreSQL support “accent insensitive” collations?

前端 未结 3 1518
梦谈多话
梦谈多话 2020-11-22 10:17

In Microsoft SQL Server, it\'s possible to specify an \"accent insensitive\" collation (for a database, table or column), which means that it\'s possible for a query like

3条回答
  •  悲&欢浪女
    2020-11-22 11:04

    No, PostgreSQL does not support collations in that sense

    PostgreSQL does not support collations like that (accent insensitive or not) because no comparison can return equal unless things are binary-equal. This is because internally it would introduce a lot of complexities for things like a hash index. For this reason collations in their strictest sense only affect ordering and not equality.

    Workarounds

    Full-Text-Search Dictionary that Unaccents lexemes.

    For FTS, you can define your own dictionary using unaccent,

    CREATE EXTENSION unaccent;
    
    CREATE TEXT SEARCH CONFIGURATION mydict ( COPY = simple );
    ALTER TEXT SEARCH CONFIGURATION mydict
      ALTER MAPPING FOR hword, hword_part, word
      WITH unaccent, simple;
    

    Which you can then index with a functional index,

    -- Just some sample data...
    CREATE TABLE myTable ( myCol )
      AS VALUES ('fóó bar baz'),('qux quz');
    
    -- No index required, but feel free to create one
    CREATE INDEX ON myTable
      USING GIST (to_tsvector('mydict', myCol));
    

    You can now query it very simply

    SELECT *
    FROM myTable
    WHERE to_tsvector('mydict', myCol) @@ 'foo & bar'
    
        mycol    
    -------------
     fóó bar baz
    (1 row)
    

    See also

    • Creating a case-insensitive and accent/diacritics insensitive search on a field

    Unaccent by itself.

    The unaccent module can also be used by itself without FTS-integration, for that check out Erwin's answer

提交回复
热议问题