Skip to main content
vsql_textsearch adds BM25 full-text search to MySQL. You store text in a DOC(n) column, put a bm25 index on it, and ask for the best matches with ORDER BY bm25_distance(column, 'query') LIMIT n. The index ranks rows with BM25, the ranking function that Lucene uses, and stems English words, so a search for run dog finds a row that says running dogs.
This extension needs a VillageSQL server built from the main branch. The 0.0.8 release does not have the index scan interface that the extension uses. The extension also declares preview capabilities, so vsql_allow_preview_extensions must be ON. Start the server with --vsql_allow_preview_extensions=ON, or run SET PERSIST vsql_allow_preview_extensions = ON.

Install

vsql_textsearch is not bundled with the server, so build it from the repository first, against your server build tree:
cmake --install copies vsql_textsearch.veb into the server build’s veb_output_directory, which is the lib/veb directory that the server reads. Then install it:
Confirm it is there:
If vsql_allow_preview_extensions is OFF, the install fails:

What it adds

The index takes these options in a WITH (...) clause. An option that you leave out takes its default. bm25_indexes, bm25_status, bm25_spill, and bm25_merge are a temporary interface for testing. They name an index by an internal id (tablespace:root page), and they do not check privileges: an account with only USAGE can call them.

Example

Create the table and the index while the table is empty, then insert:
Ask for the best matches:
To require or exclude words, pass a filter with bm25_query, through a user variable:
The ranked search guide covers the filter syntax, loading rows you already have, and the query shapes that work.

Limits

CREATE INDEX on a table that already has rows stops the server with an InnoDB assertion (old_index != nullptr in the error log). Create the index on an empty table, then load it with INSERT or INSERT ... SELECT.
  • A search must be a query that the index serves: ORDER BY bm25_distance(...) in ascending order, with a LIMIT, and the same search text everywhere bm25_distance appears. Any other query that calls bm25_distance fails:
  • A WHERE clause works while the bm25 index still serves the query. A range condition on another index, such as WHERE id < 4 on the primary key, makes the optimizer use that index, and the query fails with the error above.
  • bm25_query(...) written directly inside bm25_distance(...) fails with an Incorrect DOC value error. Pass it through a user variable or a prepared-statement parameter.
  • Only a DOC column takes a bm25 index. On VARCHAR or TEXT, CREATE INDEX fails with ERROR 3219 (HY000): No default index profile found: column 'body' is not a custom type.
  • A value longer than n bytes fails with ERROR 3219 (HY000): DOC: text longer than the column's length.
  • A DOC value must come from a column, a literal, or a user variable. An expression such as CONCAT(...) fails with ERROR 3219 (HY000): Incorrect DOC value: cannot implicitly cast string expression. Use explicit conversion for column 'body' at row 1.
  • A bad index option or a filter with a syntax error fails with ERROR 3219 (HY000): InnoDB: Custom type operation failed. See server error log for details. The reason is only in the server error log.
  • The stored format can change between versions of the extension.

See also