Skip to main content
These worked examples build on the concepts from the Full-Text Search guide. They walk through creating sample tables, building FTS indices, and running fuzzy, phrase, boosted, boolean, and substring queries.

Fuzzy Search and Boosting Example

Generate Data

First, let’s create a table with sample text data for testing fuzzy search:

Create Table

Construct FTS Index

Create a full-text search index on the first text column:
Then, create an index on the second text column:
Now we can perform basic, fuzzy, and prefix match searches:

Fuzzy Search with Typos

Prefix based Match

Prefix-based match allows you to search for documents containing words that start with a specific prefix.

Phrase Match

Phrase matching enables you to search for exact sequences of words. Unlike regular text search which matches individual terms independently, phrase matching requires words to appear in the specified order with no intervening terms.
Phrase queries are supported but only for a single column; providing multiple columns with a quoted phrase raises an error.
Phrase matching is particularly useful for:
  • Searching for specific multi-word expressions
  • Matching exact titles or quotes
  • Finding precise word combinations in a specific order

Flexible Phrase Match

To provide more flexible phrase matching, LanceDB supports the slop parameter. This allows you to match phrases where the terms appear close to each other, even if they are not directly adjacent or in the exact order, as long as they are within the specified slop value. For example, the phrase query “puppy merrily” would not return any results by default. However, if you set slop=1, it will match phrases like “puppy jumps merrily”, “puppy runs merrily”, and similar variations where one word appears between “puppy” and “merrily”.

Search with Boosting

Boosting allows you to control the relative importance of different search terms or fields in your queries. This feature is particularly useful when you need to:
  • Prioritize matches in certain columns
  • Promote specific terms while demoting others
  • Fine-tune relevance scoring for better search results

Best practices

  • Use fuzzy search when handling user input that may contain typos or variations
  • Apply field boosting to prioritize matches in more important columns
  • Combine fuzzy search with boosting for robust and precise search results
Recommendations for optimal FTS performance:
  • Create full-text search indices on text columns that will be frequently searched
  • For hybrid search combining text and vectors, see our hybrid search guide
  • For performance benchmarks, check our benchmark results
  • For complex queries, use SQL to combine FTS with other filter conditions

Boolean Queries

LanceDB supports boolean logic in full-text search, allowing you to combine multiple queries using and and or operators. This is useful when you want to match documents that satisfy multiple conditions (intersection) or at least one of several conditions (union).

Combining Two Match Queries

In Python, you can combine two MatchQuery objects using either the and function or the & operator (e.g., MatchQuery("puppy", "text") and MatchQuery("merrily", "text")); both methods are supported and yield the same result. Similarly, you can use either the or function or the | operator to perform an or query. In TypeScript, boolean queries are constructed using the BooleanQuery class with a list of [Occur, subquery] pairs. For example, to perform an AND query:
SQL
This approach allows you to specify complex boolean logic by combining multiple subqueries with different Occur values (such as Must, Should, or MustNot).
Which queries are allowed?A boolean query must include at least one SHOULD or MUST clause. Queries that contain only a MUST_NOT clause are not allowed.
How to use booleans?
  • Use and/&(Python), Occur.Must(Typescript) for intersection (documents must match all queries).
  • Use or/|(Python), Occur.Should(Typescript) for union (documents must match at least one query).

Substring Search Example

LanceDB supports searching for substrings in text columns using n-gram tokenization. This is useful for finding partial matches within text content.

Setting Up the Table

First, create a table with sample text data and configure n-gram tokenization:
With the default n-gram settings (minimum length of 3), you can search for substrings of length 3 or more:

Handling Short Substrings

By default, the minimum n-gram length is 3, so shorter substrings like “la” won’t match:

Customizing N-gram Parameters

You can customize the n-gram behavior by adjusting the minimum length and using prefix-only matching:

Testing Custom N-gram Settings

With the new settings, you can now search for shorter substrings and use prefix-only matching: