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.
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.
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 arun_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:Basic Full-Text Search
Use thefts() 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:
_score column and order by it:
Key points:
- Without
ORDER BY _score DESC, you get the top matching results but in arbitrary order - The
_scorecolumn is optional - include it only when you need to see or order by relevance scores _scoreuses the BM25 ranking algorithm to measure relevance
Advanced Query Types
Fuzzy Search
Fuzzy search allows you to find matches even when the search terms contain typos:Phrase Queries
Search for exact phrases in documents:with_position=true:
Phrase Queries with Slop
Allow some flexibility in phrase matching with theslop 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 forMatchQuery, PhraseQuery, BoostQuery, and MultiMatchQuery, see the Full-Text Search documentation.
Related Documentation
- Full-text search - Learn about FTS capabilities and query types
- SQL queries - General SQL query documentation
- Hybrid search - Combine FTS with vector search