PostgreSQL full text search and 'unaccented'

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hans@dykema.nl
    Junior Member
    • May 2020
    • 16

    #1

    PostgreSQL full text search and 'unaccented'

    When enabling full text search, it could be convenient to let PostgreSQL search without accents.
    This can be enabled using the right text search engines.
    Here's some instruction:

    When one gets this:

    Code:
    crm_divertimento=# create extension unaccent;
    ERROR: extension "unaccent" is not available
    One needs to install the progresql contrib package or build postgresql with 'make world'.

    Now, to enable full text search without accents, the following can be done:


    Code:
    crm_divertimento=# create extension unaccent;
    CREATE EXTENSION
    ​
    crm_divertimento=# show default_text_search_config;
    default_text_search_config
    ----------------------------
    pg_catalog.dutch
    (1 row)
    ​
    crm_divertimento=# create text search configuration dutch_unaccented(COPY = dutch);
    CREATE TEXT SEARCH CONFIGURATION
    crm_divertimento=# alter text search configuration dutch_unaccented alter mapping for hword, hword_part, word with unaccent, dutch_stem;
    ALTER TEXT SEARCH CONFIGURATION
    ​


    Next in postgresql.conf enable the new text search configuration:

    Code:
    (...)
    # default configuration for text search
    default_text_search_config = 'pg_catalog.dutch_unaccented'
    (...)
    And restart the postgresql service.

    Now

    Code:
     to_tsvector('dutch_unaccented', 'Dvořák, Antonín - Carnival Overture, op.92, B.169 (Karneval)')
    will result in

    Code:
    'antonin':2 'b.169':6 'carnival':3 'dvorak':1 'karneval':7 'op.92':5 'overtur':4
    instead of

    Code:
    'antonin':2 'b.169':6 'carnival':3 'dvořak':1 'karneval':7 'op.92':5 'overtur':4
    See the accent on the 'r'.


Working...