Skip to main content
Enterprise-only
This feature is currently in beta. The SQL syntax and JSON query format may change in future releases as we continue to refine and improve the FTS SQL interface. We recommend testing thoroughly and being prepared to update your queries as newer versions of LanceDB become available.
LanceDB provides support for full-text search via SQL queries using the fts() User-Defined Table Function (UDTF). This allows you to incorporate keyword-based search (based on BM25) in your SQL queries for powerful text retrieval. The SQL fts() table function expects exactly two string literals: the table name and the JSON FTS query. Build the JSON query in your application, pass it as a SQL string literal, and keep filtering, grouping, or joining in the surrounding SQL.

Table Setup

First, set up your FlightSQL client connection. See SQL Queries documentation for detailed client setup instructions. For the examples below, we assume you have a run_query() helper function that executes SQL and returns results.

Creating the Table

Create a table with text data:

Inserting Data

Insert sample documents:

Creating FTS Index

Create a full-text search index on the text column:

Phrase queries require position information

To use phrase queries (exact phrase matching), create the index with with_position = true:
Without position information, phrase queries will not work. See the Phrase Queries section below for details. Use the fts() UDTF in SQL queries with JSON-formatted search queries:

Understanding result ordering and relevance scores

FTS queries compute a BM25 relevance score for each matching document and by default return the top 5 matching results in arbitrary order:
For exact ordering by relevance, select the special _score column and order by it:
Key points:
  • Without ORDER BY _score DESC, you get the top matching results but in arbitrary order
  • The _score column is optional - include it only when you need to see or order by relevance scores
  • _score uses the BM25 ranking algorithm to measure relevance

Advanced Query Types

Fuzzy search allows you to find matches even when the search terms contain typos:

Phrase Queries

Search for exact phrases in documents:
For phrase queries to work, the FTS index must be created with with_position=true:

Phrase Queries with Slop

Allow some flexibility in phrase matching with the slop parameter:

Boolean Queries

Combine multiple queries using boolean logic:

AND Queries

OR Queries

Boost Queries

Control relevance by boosting or demoting certain terms:

Multi-Match Queries

Search across multiple columns simultaneously:

Multi-Match with Field Boosting

Combining FTS with SQL

FTS queries can be combined with standard SQL features like WHERE clauses, GROUP BY, and JOINs:

Query Parameters Reference

For detailed information about query parameters and options for MatchQuery, PhraseQuery, BoostQuery, and MultiMatchQuery, see the Full-Text Search documentation.