Skip to content

published: · updated:

PGroonga Guide

Using a Korean Tokenizer with PGroonga

Build a PGroonga Docker image with Mecab-ko and its dictionary to search Korean text effectively, including text without spaces, and compare it with the Japanese Mecab tokenizer.

Written by: gymynnym

seriesPGroonga Guide

Using a Korean Tokenizer with PGroonga

See gymynnym/learn-pgroonga for this chapter’s examples.

What Is Mecab-ko?

Mecab-ko is an open-source Korean morphological analyzer. It is a fork of Mecab, the Japanese morphological analyzer, with features tailored to Korean.

$ echo "개발자커뮤니케이션보고서" | mecab
개발자  NNG,*,F,개발자,Compound,*,*,개발/NNG/행위+자/NNG/*
커뮤니케이션    NNG,행위,T,커뮤니케이션,*,*,*,*
보고서  NNG,*,F,보고서,Compound,*,*,보고/NNG/*+서/NNG/*
EOS

As shown above, Mecab-ko splits sentences into morphemes and provides each morpheme’s part of speech. Let’s use it with PGroonga to search Korean text effectively.

Using Mecab-ko with PGroonga

Groonga supports the TokenMecab tokenizer. Build Groonga with DGRN_WITH_MECAB=ON to enable a Mecab-based tokenizer.

Install the Mecab engine and dictionary separately. Japanese text generally requires Mecab and the IPA dictionary; Korean text requires Mecab-ko and Mecab-ko-dic.

The official PGroonga Docker image includes Mecab for Japanese, so we will build a new image.

Dockerfile: PGroonga + Mecab-ko

This example is a fork of okamyuji/pgroonga.

Here is a Dockerfile that combines PGroonga with Mecab-ko.

Dockerfile

FROM postgres:18-alpine AS builder

ENV PGROONGA_VERSION=4.0.4 \
    GROONGA_VERSION=15.2.0 \
    MECAB_KO_VERSION=0.996-ko-0.9.2 \
    MECAB_KO_DIC_VERSION=2.1.1-20180720

RUN apk add --no-cache \
    apache-arrow-dev \
    build-base \
    clang19-dev \
    cmake \
    gettext-dev \
    linux-headers \
    llvm19 \
    lz4-dev \
    msgpack-c-dev \
    postgresql-dev \
    rapidjson-dev \
    ruby \
    samurai \
    wget \
    xsimd-dev \
    xxhash-dev \
    zlib-dev \
    zstd-dev

COPY alpine/build.sh /
RUN chmod +x /build.sh && \
  /build.sh ${PGROONGA_VERSION} ${GROONGA_VERSION} ${MECAB_KO_VERSION} ${MECAB_KO_DIC_VERSION}

FROM postgres:18-alpine

ENV PGROONGA_VERSION=4.0.4 \
    GROONGA_VERSION=15.2.0 \
    LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH

RUN apk add --no-cache \
    libarrow \
    libgomp \
    libxxhash \
    msgpack-c \
    zlib \
    zstd

COPY --from=builder /usr/local/lib/libmecab* /usr/local/lib/
COPY --from=builder /usr/local/lib/libgroonga* /usr/local/lib/
COPY --from=builder /usr/local/lib/groonga /usr/local/lib/groonga
COPY --from=builder /usr/local/lib/postgresql/pgroonga* /usr/local/lib/postgresql/
COPY --from=builder /usr/local/lib/mecab /usr/local/lib/mecab
COPY --from=builder /usr/local/share/postgresql/extension/pgroonga* /usr/local/share/postgresql/extension/
COPY --from=builder /usr/local/bin/mecab* /usr/local/bin/
COPY --from=builder /usr/local/bin/groonga* /usr/local/bin/
COPY --from=builder /usr/local/bin/grn* /usr/local/bin/
COPY --from=builder /usr/local/etc/mecabrc /usr/local/etc/

builder stage

  • Use PostgreSQL 18 Alpine as the base image.
  • Install build dependencies.
  • Run build.sh.

runtime stage

  • Use PostgreSQL 18 Alpine as the base image.
  • Install runtime dependencies.
  • Copy binaries and libraries built in the builder stage (Mecab-ko, Groonga, PGroonga).

alpine/build.sh

#!/bin/sh
set -eux

PGROONGA_VERSION=$1
GROONGA_VERSION=$2
MECAB_KO_VERSION=$3
MECAB_KO_DIC_VERSION=$4

# Build and install MeCab-ko
cd /tmp
wget "https://bitbucket.org/eunjeon/mecab-ko/downloads/mecab-${MECAB_KO_VERSION}.tar.gz"
tar xf "mecab-${MECAB_KO_VERSION}.tar.gz"
cd "mecab-${MECAB_KO_VERSION}"
./configure --prefix=/usr/local --with-charset=utf8
make
make install
cd /tmp
rm -rf "mecab-${MECAB_KO_VERSION}" "mecab-${MECAB_KO_VERSION}.tar.gz"

# Build and install MeCab-ko dictionary
cd /tmp
wget "https://bitbucket.org/eunjeon/mecab-ko-dic/downloads/mecab-ko-dic-${MECAB_KO_DIC_VERSION}.tar.gz"
tar xf "mecab-ko-dic-${MECAB_KO_DIC_VERSION}.tar.gz"
cd "mecab-ko-dic-${MECAB_KO_DIC_VERSION}"
./configure --prefix=/usr/local --with-charset=utf8 --with-mecab-config=/usr/local/bin/mecab-config
make
make install
cd /tmp
rm -rf "mecab-ko-dic-${MECAB_KO_DIC_VERSION}" "mecab-ko-dic-${MECAB_KO_DIC_VERSION}.tar.gz"

# Build and install Groonga with MeCab support
cd /tmp
wget "https://packages.groonga.org/source/groonga/groonga-${GROONGA_VERSION}.tar.gz"
tar xf "groonga-${GROONGA_VERSION}.tar.gz"
cd "groonga-${GROONGA_VERSION}"
cmake \
  -S . \
  -B ../groonga.build \
  --preset=release-maximum \
  -DCMAKE_INSTALL_PREFIX=/usr/local \
  -DGRN_WITH_MRUBY=OFF \
  -DGRN_WITH_MECAB=ON
cmake --build ../groonga.build
cmake --install ../groonga.build
cd /tmp
rm -rf "groonga-${GROONGA_VERSION}" "groonga-${GROONGA_VERSION}.tar.gz" groonga.build

# Build and install PGroonga
cd /tmp
wget "https://packages.groonga.org/source/pgroonga/pgroonga-${PGROONGA_VERSION}.tar.gz"
tar xf "pgroonga-${PGROONGA_VERSION}.tar.gz"
cd "pgroonga-${PGROONGA_VERSION}"
make HAVE_MSGPACK=1 HAVE_XXHASH=1 \
  PG_CPPFLAGS="-I/usr/local/include/groonga -DPGRN_VERSION='\"${PGROONGA_VERSION}\"'" \
  SHLIB_LINK="-L/usr/local/lib -lgroonga"
make install
cd /tmp
rm -rf "pgroonga-${PGROONGA_VERSION}" "pgroonga-${PGROONGA_VERSION}.tar.gz"
  1. Build and install Mecab-ko.
  2. Build and install the Mecab-ko dictionary.
  3. Build and install Groonga with Mecab support enabled.
  4. Build and install PGroonga.
$ docker build -t pgroonga-mecab-ko .
services:
  ...
  pgroonga-mecab-ko:
    container_name: pgroonga-mecab-ko
    image: pgroonga-mecab-ko
    shm_size: 128mb
    env_file: .env.postgres
$ docker compose up -d

Build the Dockerfile and add a pgroonga-mecab-ko service to docker-compose.yml so we can compare it with the previous example.

Using Mecab-ko with PGroonga

Verify the Mecab-ko Installation

$ docker exec -it pgroonga-mecab-ko sh -c 'echo "개발자커뮤니케이션보고서" | mecab'
개발자  NNG,*,F,개발자,Compound,*,*,개발/NNG/행위+자/NNG/*
커뮤니케이션    NNG,행위,T,커뮤니케이션,*,*,*,*
보고서  NNG,*,F,보고서,Compound,*,*,보고/NNG/*+서/NNG/*
EOS

Check that Mecab-ko was installed correctly.

Create Example Data

$ docker exec -it pgroonga-mecab-ko psql -U root
CREATE EXTENSION pgroonga;

CREATE TABLE articles (
    id SERIAL PRIMARY KEY,
    title TEXT,
    content TEXT
);

CREATE INDEX articles_pgroonga_idx
ON public.articles USING pgroonga ((ARRAY [title, content]))
WITH (tokenizer='TokenMecab');

INSERT INTO articles (title, content) VALUES
('개발자커뮤니케이션보고서', '오늘 회의에서 API응답지연문제가 다시 논의되었습니다. 팀원들은 커피부족현상 때문에 집중력이 떨어졌다고 주장했습니다.'),
('서비스장애원인분석일지', '새벽에 발생한 DB커넥션폭주현상은 캐시미스연쇄작용 때문에 발생한 것으로 확인되었습니다. 담당자는 정신적피해보상요청서를 제출했습니다.'),
('개발자업무효율개선프로젝트', '개발환경자동화도구를 도입한 이후 빌드속도개선효과가 나타났습니다. 그러나 야근지수상승현상은 여전히 해결되지 않았습니다.');

The inserted data includes Korean text without word spacing.

SELECT id, title FROM articles
WHERE ARRAY[title, content] &@~ '보고서';
--  id |          title
-- ----+--------------------------
--   1 | 개발자커뮤니케이션보고서

The Mecab-ko tokenizer splits “개발자커뮤니케이션보고서” into “개발자” (developer), “커뮤니케이션” (communication), and “보고서” (report). A search for “보고서” can therefore find this record correctly.

Bonus: Comparison with the Mecab Tokenizer

$ docker exec -it my-pgroonga sh -c 'echo "개발자커뮤니케이션보고서" | mecab'
개발자커뮤니케이션보고서        記号,一般,*,*,*,*,*
EOS

Mecab for Japanese does not split the Korean text properly.

SELECT id, title FROM articles
WHERE ARRAY[title, content] &@~ '보고서';
--  id | title
-- ----+-------
-- (0 rows)

A search for “보고서” therefore cannot find the record that Mecab-ko found.