PGroonga: Multilingual Full Text Search
PGroonga
is a PostgreSQL extension adding a full text search indexing method based on Groonga. While native PostgreSQL supports full text indexing, it is limited to alphabet and digit based languages. PGroonga
offers a wider range of character support making it viable for a superset of languages supported by PostgreSQL including Japanese, Chinese, etc.
Enable the extension#
- Go to the Database page in the Dashboard.
- Click on Extensions in the sidebar.
- Search for "pgroonga" and enable the extension.
Creating a full text search index#
Given a table with a text
column:
We can index the column for full text search with a pgroonga
index:
To test the full text index, we'll add some data.
The PostgreSQL query planner is smart enough to know that, for extremely small tables, it's faster to scan the whole table rather than loading an index. To force the index to be used, we can disable sequential scans:
Now if we run an explain plan on a query filtering on memos.content
:
The pgroonga index is used to retrive the result set:
Full Text Search#
The &@~
operator performs full text search. It returns any matching results. Unlike LIKE
operator, pgroonga can search any text that contains the keyword case insensitive.
Take the following example:
And the result:
Match all search words#
To find all memos where content contains BOTH of the words postgres
and pgroonga
, we can just use space to separate each words:
And the result:
Match any search words#
To find all memos where content contain ANY of the words postgres
or pgroonga
, use the upper case OR
:
And the result:
Search that matches words with negation#
To find all memos where content contain the word postgres
but not pgroonga
, use -
symbol:
And the result:
Resources#
- Official PGroonga documentation