mroonga - An open-source storage engine for fast fulltext search with MySQL.

3.3. Storage mode

Here we explain how to use storage mode of mroonga

3.3.2. How to get search score

Note

In version 1.0.0 or before, mroonga used a special column named _score to get search score. From version 1.0.0, it follows MySQL's standard way to get search score.

We often want to display more relevant results first in full text search. We use search score in such case.

We can get search score by MySQL's standard way [1], i.e. we use MATCH...AGAINST in one of columns in SELECT or ORDER BY.

Let's try.

mysql> INSERT INTO diaries (content) VALUES ("It's fine today. It'll be fine tomorrow as well.");
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO diaries (content) VALUES ("It's fine today. But it'll rain tomorrow.");
Query OK, 1 row affected (0.00 sec)

mysql> SELECT *, MATCH (content) AGAINST ("fine") FROM diaries WHERE MATCH (content) AGAINST ("fine") ORDER BY MATCH (content) AGAINST ("fine") DESC;
+----+--------------------------------------------------------------+------------------------------------+
| id | content                                                      | MATCH (content) AGAINST ("fine") |
+----+--------------------------------------------------------------+------------------------------------+
|  3 | It's fine today. It'll be fine tomorrow as well. |                                  2 |
|  1 | It'll be fine tomorrow.                      |                                  1 |
|  4 | It's fine today. But it'll rain tomorrow.    |                                  1 |
+----+--------------------------------------------------------------+------------------------------------+
3 rows in set (0.00 sec)

The result having the search word fine more, i.e. id = 3 message having the higher search score, is displayed first. And you also get search score by using MATCH AGAINST in SELECT phrase.

You can use AS to change the attribute name.

mysql> SELECT *, MATCH (content) AGAINST ("fine") AS score FROM diaries WHERE MATCH (content) AGAINST ("fine") ORDER BY MATCH (content) AGAINST ("fine") DESC;
+----+--------------------------------------------------------------+-------+
| id | content                                                      | score |
+----+--------------------------------------------------------------+-------+
|  3 | It's fine today. It'll be fine tomorrow as well. |     2 |
|  1 | It'll be fine tomorrow.                      |     1 |
|  4 | It's fine today. But it'll rain tomorrow.    |     1 |
+----+--------------------------------------------------------------+-------+
3 rows in set (0.00 sec)

3.3.5. How to get the record ID

Groonga assigns a unique number to identify the record when a record is added in the table.

To make the development of applications easier, you can get this record ID by SQL in mroonga

To get the record ID, you need to create a column named _id when you create a table.

mysql> CREATE TABLE memos (
    ->   _id INT,
     >   content VARCHAR(255),
    ->   UNIQUE KEY (_id) USING HASH
    -> ) ENGINE = mroonga;
Query OK, 0 rows affected (0.04 sec)

Tye typo of _id column should be integer one (TINYINT, SMALLINT, MEDIUMINT, INT or BIGINT).

You can create an index for _id column, but it should be HASH type.

Let's add records in the table by INSERT. Since _id column is implemented as a virtual column and its value is assigned by groonga, you cannot specify the value when updating. So you need to exclude it from setting columns, or you need to use null as its value.

mysql> INSERT INTO memos VALUES (null, "Saury for today's dinner.");
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO memos VALUES (null, "Update mroonga tomorrow.");
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO memos VALUES (null, "Buy some dumpling on the way home.");
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO memos VALUES (null, "Thank God It's meat day.");
Query OK, 1 row affected (0.00 sec)

To get the record ID, you invoke SELECT with _id column.

mysql> SELECT * FROM memos;
+------+------------------------------------------+
| _id  | content                                  |
+------+------------------------------------------+
|    1 | Saury for today's dinner.                    |
|    2 | Update mroonga tomorrow. |
|    3 | Buy some dumpling on the way home.                 |
|    4 | Thank God It's meat day.                 |
+------+------------------------------------------+
4 rows in set (0.00 sec)

By using last_insert_grn_id function, you can also get the record ID that is assigned by the last INSERT.

mysql> INSERT INTO memos VALUES (null, "Just one bottle of milk in the fridge.");
Query OK, 1 row affected (0.00 sec)

mysql> SELECT last_insert_grn_id();
+----------------------+
| last_insert_grn_id() |
+----------------------+
|                    5 |
+----------------------+
1 row in set (0.00 sec)

last_insert_grn_id function is included in mroonga as a User-Defined Function (UDF), but if you have not yet register it in MySQL by CREATE FUNCTION, you need to invoke the following SQL for defining a function.

mysql> CREATE FUNCTION last_insert_grn_id RETURNS INTEGER SONAME 'ha_mroonga.so';

As you can see in the example above, you can get the record ID by _id column or last_insert_grn_id function. It will be useful to use this value in the ensuing SQL queries like UPDATE.

mysql> UPDATE memos SET content = "So much milk in the fridge." WHERE _id = last_insert_grn_id();
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

3.3.6. How to get snippet (Keyword in context)

Mroonga provides functionality to get keyword in context. It is implemented as 'mroonga_snippet' UDF.

See mroonga_snippet about details.

3.3.7. How to run groonga command

In storage mode, mroonga stores all your data into groonga database. You can access groonga database by SQL with mroonga. SQL is very powerful but it is not good for some operations such as faceted search.

Faceted search is popular recently. Many online shopping sites such as amazon.com and ebay.com support faceted search. Faceted search refines the current search by available search parameters before users refine their search. And faceted search shows refined searches. Users just select a refined search. Users benefit from faceted search:

  • Users don't need to think about how to refine their search. Users just select a showed refined search.
  • Users don't get boared "not match" page. Faceted search showes only refined searches that has one or more matched items.

Faceted search needs multiple GROUP BY operations against searched result set. To do faceted search by SQL, multiple SELECT requests are needed. It is not effective.

Groonga can do faceted search by only one groonga command. It is effective. Groonga has the select command that can search records with faceted search. Faceted search is called as drilldown in groonga. See groonga's document about groonga's select command.

Mroonga provides mroonga_command() function. You can run groonga command in SQL by the function. But you should use only select command. Other commands that change schema or data may break consistency.

Here is the schema definition for execution examples:

CREATE TABLE diaries (
  id INT PRIMARY KEY AUTO_INCREMENT,
  content VARCHAR(255),
  date DATE,
  year YEAR,
  `year_month` VARCHAR(9),
  tag VARCHAR(32),
  FULLTEXT INDEX (content)
) ENGINE = mroonga DEFAULT CHARSET utf8;

Here is the sample data for execution examples:

INSERT INTO diaries (content, date, year, `year_month`, tag)
       VALUES ('Groonga is an open-source fulltext search engine and column store.',
               '2013-04-08',
               '2013',
               '2013-04',
               'groonga');
INSERT INTO diaries (content, date, year, `year_month`, tag)
       VALUES ('Mroonga is an open-source storage engine for fast fulltext search with MySQL.',
               '2013-04-09',
               '2013',
               '2013-04',
               'MySQL');
INSERT INTO diaries (content, date, year, `year_month`, tag)
       VALUES ('Tritonn is a patched version of MySQL that supports better fulltext search function with Senna.',
               '2013-03-29',
               '2013',
               '2013-03',
               'MySQL');

Each record has groonga or MySQL as tag. Each record also has year and year_month. You can use tag, year and year_month as faceted search keys.

Groonga calls faceted search as drilldown. So parameter key in groonga is --drilldown. Groonga returns search result as JSON. So mroonga_command() also returns search result as JSON. It is not SQL friendly. You need to parse search result JSON by yourself.

Here is the example of faceted search by all available faceted search keys (result JSON is pretty printted):

SELECT mroonga_command("select diaries --output_columns _id --limit 0 --drilldown tag,year,year_month") AS faceted_result;
+-----------------------------+
| faceted_result              |
+-----------------------------+
| [[[3],                      |
|   [["_id","UInt32"]]],      |
|  [[2],                      |
|   [["_key","ShortText"],    |
|    ["_nsubrecs","Int32"]],  |
|   ["groonga",1],            |
|   ["MySQL",2]],             |
|  [[1],                      |
|   [["_key","Time"],         |
|    ["_nsubrecs","Int32"]],  |
|   [1356998400.0,3]],        |
|  [[2],                      |
|   [["_key","ShortText"],    |
|    ["_nsubrecs","Int32"]],  |
|   ["2013-04",2],            |
|   ["2013-03",1]]]           |
+-----------------------------+
1 row in set (0.00 sec)

See groonga's select command document for more details.

3.3.8. Logging

Mroonga outputs the logs by default.

Log files are located in MySQL's data directory with the filename groonga.log.

Here is the example of the log.

2010-10-07 17:32:39.209379|n|b1858f80|mroonga 1.10 started.
2010-10-07 17:32:44.934048|d|46953940|hash get not found (key=test)
2010-10-07 17:32:44.936113|d|46953940|hash put (key=test)

The default log level is NOTICE, i.e. we have important information only and we don't have debug information etc.).

You can get the log level by mroonga_log_level system variable, that is a global variable. You can also modify it dynamically by using SET phrase.

mysql> SHOW VARIABLES LIKE 'mroonga_log_level';
+-------------------+--------+
| Variable_name     | Value  |
+-------------------+--------+
| mroonga_log_level | NOTICE |
+-------------------+--------+
1 row in set (0.00 sec)

mysql> SET GLOBAL mroonga_log_level=DUMP;
Query OK, 0 rows affected (0.00 sec)

mysql> SHOW VARIABLES LIKE 'mroonga_log_level';
+-------------------+-------+
| Variable_name     | Value |
+-------------------+-------+
| mroonga_log_level | DUMP  |
+-------------------+-------+
1 row in set (0.00 sec)

Available log levels are the followings.

  • NONE
  • EMERG
  • ALERT
  • CRIT
  • ERROR
  • WARNING
  • NOTICE
  • INFO
  • DEBUG
  • DUMP

See mroonga_log_level about details.

You can reopen the log file by FLUSH LOGS. If you want to rotate the log file without stopping MySQL server, you can do in the following procedure.

  1. change the file name of groonga.log (by using OS's mv command etc.).
  2. invoke "FLUSH LOGS" in MySQL server (by mysql command or mysqladmin command).

3.3.9. Choosing appropriate columns

Groonga uses one file per column to store data, and mroonga accesses needed columns only when accessing a table to utilise this characteristic.

This optimisation is done automatically in mroonga internal, you don't need any specific configuration.

Imagine that we have a table with 20 columns like below.

CREATE TABLE t1 (
  c1 INT PRIMARY KEY AUTO_INCREMENT,
  c2 INT,
  c3 INT,
  ...
  c11 VARCHAR(20),
  c12 VARCHAR(20),
  ...
  c20 DATETIME
) ENGINE = mroonga DEFAULT CHARSET utf8;

When we run SELECT phrase like the following, mroonga reads data from columns that are referred by SELECT phrase and WHERE phrase only (and it does not access columns that not required internally).

SELECT c1, c2, c11 FROM t1 WHERE c2 = XX AND c12 = "XXX";

In this case above, only columns c1, c2, c11 and c12 are accessed, and we can process the SQL rapidly.

3.3.10. Optimisation for counting rows

In MySQL's storage engine interface, there is no difference between counting rows like COUNT(*) and normal data retrieving by SELECT. So access to data that is not included in SELECT result can happen even if you just want to count rows.

Tritonn (MySQL + Senna), that is mroonga's predecessor, introduced "2ind patch" to skip needless access to data and solved this performance issue.

Mroonga also has the optimisation for counting rows.

In the following SELECT, for example, needless read of columns are skipped and you can get the result of counting rows with the minimal cost.

SELECT COUNT(*) FROM t1 WHERE MATCH(c2) AGAINST("hoge");

You can check if this optimisation works or not by the status variable.

mysql> SHOW STATUS LIKE 'Mroonga_count_skip';
+--------------------+-------+
| Variable_name      | Value |
+--------------------+-------+
| Mroonga_count_skip | 1     |
+--------------------+-------+
1 row in set (0.00 sec)

Each time the optimisation for counting rows works, Mroonga_count_skip status variable value is increased.

Note : This optimisation is implemented by using the index. It only works in the case where we records can be specified only by the index.