Sling Academy
Home/PostgreSQL/PostgreSQL Full-Text Search: Handling Diacritics and Accents

PostgreSQL Full-Text Search: Handling Diacritics and Accents

Last updated: December 20, 2024

PostgreSQL is a powerful open-source relational database system. One of its features is built-in full-text search, which can handle complex queries efficiently. However, when working with multi-lingual content, one common challenge is managing diacritics and accents in search queries. This article will guide you on how to configure PostgreSQL to manage these characters effectively, enhancing your full-text search capability.

Understanding Diacritics and Accents

Diacritics are marks added to letters that often alter their pronunciation or significance. In languages like French, Spanish, and German, these markings are prevalent. In terms of search functionality, we might want the search term to match regardless of whether diacritics are present. This entails configuring PostgreSQL to treat similar letters equally even if they have different diacritics.

Using PostgreSQL to Handle Diacritics

PostgreSQL’s full-text search functionality allows you to standardize the input so that searches can be conducted more broadly or specifically, depending on your requirements. Here’s how you can configure your database to handle these accents.

Unaccent Extension

Firstly, PostgreSQL offers an extension called unaccent. This extension provides a simple way to remove diacritical marks from text, which can be especially useful for creating compatible search queries.

To enable this extension, you need to run the following command:

CREATE EXTENSION unaccent;

Once the extension is enabled, you can use the unaccent function to strip accents from your text fields. Consider the following example setup:


-- Create a table with some accented text data
CREATE TABLE articles (id SERIAL PRIMARY KEY, content TEXT);

-- Insert data into the table
INSERT INTO articles (content) VALUES 
    ('El Niño is a complex phenomenon'),
    ('Café con leche'),
    ('Résumé writing tips'),
    ('Über cool features');

Now, if you try to search directly for 'El Nino', you might not get a match. To handle this, use the unaccent function:

SELECT * FROM articles WHERE unaccent(content) ILIKE unaccent('%El Nino%');

The unaccent function will strip out the accents, enabling you to perform searches that return matching results even if the input contains no accents.

To integrate accent-handling with full-text search, you'll combine the unaccent function with PostgreSQL’s full-text search capabilities.


CREATE INDEX content_tsvector_idx ON articles USING gin (
  to_tsvector('english', unaccent(content))
);

-- Query with full-text search
SELECT * FROM articles WHERE to_tsvector('english', unaccent(content)) @@ to_tsquery('nino');

This approach ensures that the index used by the full-text search does not contain diacritic marks, making searches straightforward across different inputs.

Configuring Custom Dictionaries

In some cases, you might want more fine-tuned control, such as treating specific diacritical marks in specific contexts while preserving others. This is done through custom dictionary configuration thereby adapting to the diverse linguistic needs.

Custom dictionaries can be created by configuring text search dictionaries and incorporating the unaccent function within them.


CREATE TEXT SEARCH CONFIGURATION mycfg (COPY = pg_catalog.english);
ALTER TEXT SEARCH CONFIGURATION mycfg
  ALTER MAPPING FOR hword, hword_part, word
  WITH simple, unaccent;

-- Query using custom text search configuration
SELECT * FROM articles WHERE to_tsvector('mycfg', content) @@ to_tsquery('nino');

By creating custom configurations, PostgreSQL can better align to distinct accents, enabling users to search content more naturally.

Conclusion

Handling diacritics and accents is critical for effective full-text search functionality in multilingual environments using PostgreSQL. Extensions like unaccent make PostgreSQL a robust tool for managing diverse languages and improving search accuracy. By leveraging these database capabilities, developers can ensure that their applications provide a more user-friendly search experience, irrespective of language complexities.

Next Article: Combining Full-Text Search with Partial Matches in PostgreSQL

Previous Article: Best Practices for Optimizing PostgreSQL Full-Text Search Performance

Series: PostgreSQL Tutorials: From Basic to Advanced

PostgreSQL

You May Also Like

  • PostgreSQL with TimescaleDB: Querying Time-Series Data with SQL
  • PostgreSQL Full-Text Search with Boolean Operators
  • Filtering Stop Words in PostgreSQL Full-Text Search
  • PostgreSQL command-line cheat sheet
  • How to Perform Efficient Rolling Aggregations with TimescaleDB
  • PostgreSQL with TimescaleDB: Migrating from Traditional Relational Models
  • Best Practices for Maintaining PostgreSQL and TimescaleDB Databases
  • PostgreSQL with TimescaleDB: Building a High-Performance Analytics Engine
  • Integrating PostgreSQL and TimescaleDB with Machine Learning Models
  • PostgreSQL with TimescaleDB: Implementing Temporal Data Analysis
  • Combining PostgreSQL, TimescaleDB, and Airflow for Data Workflows
  • PostgreSQL with TimescaleDB: Visualizing Real-Time Data with Superset
  • Using PostgreSQL with TimescaleDB for Energy Consumption Analysis
  • PostgreSQL with TimescaleDB: How to Query Massive Datasets Efficiently
  • Best Practices for Writing Time-Series Queries in PostgreSQL with TimescaleDB
  • PostgreSQL with TimescaleDB: Implementing Batch Data Processing
  • Using PostgreSQL with TimescaleDB for Network Traffic Analysis
  • PostgreSQL with TimescaleDB: Troubleshooting Common Performance Issues
  • Building an IoT Data Pipeline with PostgreSQL and TimescaleDB