published: · updated:
PGroonga GuideBasic PGroonga Usage
Run PostgreSQL with the official PGroonga Docker image, enable the extension, create indexes, and use full-text queries and array indexes for searches across multiple columns.
Written by: gymynnym
seriesPGroonga Guide
Basic PGroonga Usage
Installing PGroonga
PGroonga is a PostgreSQL extension, so it must be installed in an environment that already has PostgreSQL.
The installation process varies by operating system and PostgreSQL version and can be complex. Instead of covering it here, we will use the official Docker image.
Here is a docker-compose.yml that runs a PostgreSQL image containing PGroonga.
services:
my-pgroonga:
container_name: my-pgroonga
image: groonga/pgroonga:4.0.4-alpine-18
shm_size: 128mb
env_file: .env.postgres
Put PostgreSQL settings, including the username and password, in .env.postgres.
$ docker compose up -d
$ docker exec -it my-pgroonga psql -U <username>
Start PostgreSQL and connect with the commands above. You can also connect using another method.
CREATE EXTENSION pgroonga;
Enable the PGroonga extension with the query above.
Basic PGroonga Usage
1. Create an Example Table
CREATE TABLE articles (
id SERIAL PRIMARY KEY,
title TEXT,
content TEXT
);
The articles table contains id (primary key), title, and content columns.
2. Create PGroonga Indexes
PGroonga integrates Groonga into its index engine. To use Groonga for search, create PGroonga indexes.
CREATE INDEX articles_title_pgroonga_idx
ON public.articles USING pgroonga (title)
WITH (tokenizer='TokenBigram');
CREATE INDEX articles_content_pgroonga_idx
ON public.articles USING pgroonga (content)
WITH (tokenizer='TokenBigram');
articles_title_pgroonga_idxcreates an index on the single columntitle.articles_content_pgroonga_idxcreates an index on the single columncontent.
We use TokenBigram, the most suitable tokenizer for Korean among the default tokenizers.
You can also use
Mecab-ko. We will cover it in detail in a later chapter.
SELECT jsonb_pretty(pgroonga_command('tokenize TokenBigram "이것은 첫 번째 기사입니다."')::jsonb);
The command above lets you inspect the tokenization result.
3. Insert Example Data
INSERT INTO articles (title, content) VALUES
('첫 번째 기사', '이것은 첫 번째 기사입니다.'),
('두 번째 기사', '이것은 두 번째 기사입니다.'),
('세 번째 기사', '이것은 세 번째 기사입니다.');
4. PGroonga FTS : &@~
SELECT * FROM articles
WHERE title &@~ '첫 번째' OR content &@~ '첫 번째';
-- id | title | content
-- ----+--------------+----------------------------
-- 1 | 첫 번째 기사 | 이것은 첫 번째 기사입니다.
The &@~ operator performs PGroonga full-text search.
Specify the target column on the left and the search keyword on the right. Keywords can also use Groonga’s query syntax.
Frequently used Groonga query operators include:
KEYWORD1 AND KEYWORD2: AND operator.KEYWORD1 OR KEYWORD2: OR operator.KEYWORD1 -KEYWORD2: keyword exclusion operator.
5. Searching Multiple Columns
At this point, you may have noticed a potential problem.
What happens if the
articlestable gains more searchable columns?
You are right: writing an OR condition for every column is cumbersome and requires multiple indexes, which also hurts performance.
Fortunately, PGroonga supports indexes on text[]. You can group the searchable columns into an array and manage them with one index.
CREATE INDEX articles_pgroonga_idx
ON public.articles USING pgroonga ((ARRAY [title, content]))
WITH (tokenizer='TokenBigram');
SELECT * FROM articles
WHERE ARRAY[title, content] &@~ '첫 번째';
The queries above group title and content into an array to create a single PGroonga index, then search that array with PGroonga.
Now you can search with one index and one query, even as the number of searchable columns grows!