In SurrealDB, Full-Text Search supports text matching, proximity searches, result ranking, and keyword highlighting. It is also ACID-compliant, which ensures data integrity and reliability.
To implement Full-Text Search it is important that your data is first defined using SurrealDB DEFINE TABLE
and DEFINE FIELD
.
Once the data is in your tables, you can use customized analyzers to define rules for how your textual data should be searched. An analyzer includes tokenizers and filters which help break down text into manageable tokens and refine the search.
-- Combining tokenizers and filters into a custom analyzer DEFINE ANALYZER custom_analyzer TOKENIZERS blank FILTERS lowercase, snowball(english);
To make a text field searchable, you need to set up a full-text index on it by using the ‘search’ keyword.
Depending on the use case, each field or column can be associated with a different analyzers To enable text highlight on searches, use the HIGHLIGHTS
keyword when defining a full-text search index. This enables you use the search::highlight
function.
-- Defining two full-text indexes on the 'title' and 'content' field of the 'book' table DEFINE INDEX book_title ON book FIELDS title SEARCH ANALYZER custom_analyzer BM25; DEFINE INDEX book_content ON book FIELDS content SEARCH ANALYZER custom_analyzer BM25 HIGHLIGHTS;
To find documents that contain the given keywords based on the full-text indexes, the matches operator (@@) is used in queries.
-- Using the MATCHES (@@) operator in a query SELECT * FROM book WHERE content @@ 'tools';
If you want to do more with your search results, SurrealDB offers 3 search functions that accompany the matches operator.
search::highlight
: Highlights the matching keywords for the predicate reference number.search::offsets
: Returns the position of the matching keywords for the predicate reference number.search::score
: Helps with scoring and ranking the search results based on their relevance to the search terms.