Skip to main content

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.
A help center search box gets a few words, such as 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:
The extension is not bundled with the server. Build it as the vsql_textsearch page describes, and then install it:
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:
Do not run CREATE INDEX on a table that already has rows. It stops the server with an InnoDB assertion. To index rows you already have, see Index text you already have.
The 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 by bm25_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 with bm25_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:
The filter uses operators like PostgreSQL’s tsquery:
  • email matches a row that contains email.
  • reset password matches a row that contains both words, because a space means AND.
  • email | code matches a row that contains either word.
  • !code matches a row that does not contain code.
  • "reset link" matches a row that contains the phrase.
  • auth* matches a row that contains a word that starts with auth.
To exclude the articles about authenticator codes:
To require every word of the search, repeat the words in the filter:

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 a TEXT column, create a new table with a DOC column, index it while it is empty, and copy the rows in with INSERT ... SELECT:
A value longer than the 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:
To index computed text, store it in a TEXT column first, and then copy that column.

Query shapes to avoid

The score needs the statistics of the index scan, so bm25_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:
A 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:
A filter with a syntax error, such as an unclosed quote in 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