VillageSQL is a drop-in replacement for MySQL with extensions.
All examples on this page work on VillageSQL. Install Now →
This guide needs a VillageSQL server built from the
main branch, as
Build from source describes. The 0.0.8 release does not
have the index scan interface that vsql_textsearch uses.reset password, and has
to put the most useful article first. LIKE '%password%' reads every row and
does not rank anything. The vsql_textsearch extension adds a bm25 index that
ranks rows with BM25, the ranking function that Lucene uses, and returns the
best matches first. This guide builds a small table
of help articles, searches it, and narrows the results with filters.
Before you start
vsql_textsearch uses preview capabilities, so turn them on first. This takes
effect at once:
Create a table you can search
The extension indexes text in its own column type,DOC(n), which holds up to
n bytes of UTF-8 text, with n from 1 to 16303. A VARCHAR or TEXT column
does not take a bm25 index. Choose n near your longest document, because
every value takes a full record of n + 2 bytes.
Create the table, and create the index while the table is still empty:
english analyzer lowercases the text, drops common English words such as
the and to, and reduces each word to its stem, so resetting and reset
match.
Load the articles
Search and rank
Order bybm25_distance and give a LIMIT. The index serves the query and
returns the best matches first:
bm25_distance is the BM25 score with its sign flipped, so a lower value is a
better match and ascending order puts the best match first. The search text
matches a row that has any of its words. Article 3 mentions password once
and does not mention reset, so it ranks last. When no row has any of the
words, the query returns an empty result.
EXPLAIN shows that the bm25 index serves the query:
Require, exclude, and match phrases
To narrow the results, pass a filter withbm25_query(search_text, filter).
The scan returns only the rows that match the filter, still ranked by the
search text, so LIMIT counts matching rows. Put the query in a user variable
first, because bm25_distance accepts it only from a user variable or a
prepared-statement parameter:
tsquery:
emailmatches a row that containsemail.reset passwordmatches a row that contains both words, because a space means AND.email | codematches a row that contains either word.!codematches a row that does not containcode."reset link"matches a row that contains the phrase.auth*matches a row that contains a word that starts withauth.
Keep the index current
INSERT, UPDATE, and DELETE keep the index current, so a search sees a
changed article without a rebuild:
Index text you already have
When your articles already live in a table with aTEXT column, create a new
table with a DOC column, index it while it is empty, and copy the rows in
with INSERT ... SELECT:
DOC column fails the insert with
ERROR 3219 (HY000): DOC: text longer than the column's length, so size the
column for your longest document.
The value you copy must be a column, a literal, or a user variable. An
expression such as CONCAT(title, ' ', body) in the SELECT fails:
TEXT column first, and then copy that
column.
Query shapes to avoid
The score needs the statistics of the index scan, sobm25_distance works
only in a query that the bm25 index serves: ORDER BY bm25_distance(...)
in ascending order, with a LIMIT, and the same search text everywhere
bm25_distance appears in the query. A query without LIMIT fails:
WHERE clause works while the bm25 index still serves the query. A range
condition on another index, such as the primary key, makes the optimizer use
that index instead, and the query fails with the same error:
bm25_query('password', '"reset'), fails with
ERROR 3219 (HY000): InnoDB: Custom type operation failed. See server error log for details.
The server error log names the mistake.
Clean up
Drop the database when you are done:See also
- vsql_textsearch extension: the build steps, the index options, and the current limits
- Full-text search in MySQL: MySQL’s built-in
FULLTEXTindex andMATCH ... AGAINST - Trigram similarity in MySQL: matching misspelled words, which BM25 does not do
- How indexes work: the B-tree indexes that MySQL uses for everything else