published: · updated:
PGroonga GuideWhat Is PGroonga?
Build a powerful search system using PostgreSQL alone. This series introduces PGroonga, which brings multilingual full-text search into PostgreSQL without operating an external search engine such as Elasticsearch.
Written by: gymynnym
seriesPGroonga Guide
What Is PGroonga?
Introduction
This series explores how to build a powerful search system using PostgreSQL alone.
The goal is to solve the problem simply within PostgreSQL, without an external search engine such as Elasticsearch.
ElasticSearch vs PGroonga
Elasticsearch is a powerful search engine for quickly searching large datasets, with features such as horizontal scaling and unstructured document search. However, it requires a separate system to operate and maintain, and synchronizing data with a relational database can be complex. Configuration and tuning also have a steep learning curve for beginners.
PGroonga is a PostgreSQL extension that integrates Groonga as an index engine to provide powerful multilingual full-text search within PostgreSQL. There is no separate search engine to operate and no data synchronization problem. Configuration is relatively straightforward, too. :)
Groonga is an inverted-index full-text search engine. Newly registered documents immediately appear in search results, and updates do not require read locks, making it suitable for real-time applications. As a column-oriented DBMS, it also excels at aggregation queries, complementing row-oriented systems such as MySQL and PostgreSQL. See Characteristics of Groonga for details.
Of course, Elasticsearch is preferable in some cases:
- You need horizontal scaling.
- You need complex search features, such as pipelines or machine learning integration.
- You need powerful distributed log/metrics analysis (ELK: Elasticsearch, Logstash, Kibana).
- Your requirements explicitly mandate Elasticsearch. :(
If your search system has a simple structure, PGroonga can be a pleasant choice for developers.
How PGroonga Works
Full-text search through a PGroonga index proceeds as follows:
- Normalize: normalize the input string using techniques such as:
- Unify uppercase and lowercase.
- Handle whitespace and symbols.
- Unicode normalization.
- Remove unnecessary characters.
- Tokenize: split the normalized string into tokens.
- Choose a tokenizer suited to the language; for example,
Mecabfor Japanese. - Example:
"postgresql 검색"->["po", "os", "st", "tg", ... , "검색"].
- Choose a tokenizer suited to the language; for example,
- Index lookup and comparison: search PGroonga’s data using the generated tokens.
- Return results: return documents (records) that match the search conditions.