Vietnamese Full-Text Search on PostgreSQL

Tuan Do QuocTuan Do Quoc
1 min read

Install extensions

CREATE EXTENSION IF NOT EXISTS unaccent;
CREATE EXTENSION vector SCHEMA "public" VERSION 0.7.2;

Use a custom text search configuration

CREATE TEXT SEARCH CONFIGURATION vietnamese (COPY = simple);
ALTER TEXT SEARCH CONFIGURATION vietnamese
ALTER MAPPING FOR asciiword, word
WITH unaccent, simple;

Example query

SELECT staffCode, userName, staffName, phoneNumber, email
FROM public.users_embedding_table
WHERE to_tsvector('vietnamese', 
                  unaccent(COALESCE(staffCode, '') || ' ' || 
                           COALESCE(userName, '') || ' ' || 
                           COALESCE(staffName, '') || ' ' || 
                           COALESCE(phoneNumber, '') || ' ' || 
                           COALESCE(email, '')
                  )
                 ) @@ plainto_tsquery('vietnamese', unaccent('Hòa'));
0
Subscribe to my newsletter

Read articles from Tuan Do Quoc directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Tuan Do Quoc
Tuan Do Quoc

I'm a software engineer with a strong enthusiasm for security. I possess more than 2 years of hands-on experience with the .NET ecosystem, complemented by an additional 6 months specializing in Spring Boot and ReactJS. Experienced in deploying small and medium-sized systems to the cloud as well as on-premises.