Skip to main content
Version: 1.x

Full-Text Search

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.⁠

Define tables and fields

To implement Full-Text Search it is important that your data is first defined using SurrealDB DEFINE TABLE and DEFINE FIELD.

Analyzers

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);

Define a Full-Text Index

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;

The MATCHES Operator

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';

Search Functions

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.