Skip to main content
In certain cases, you may want to retrieve documents that are semantically similar to a given query, but also prioritize specific keywords. This is an example of hybrid search, a query method that combines multiple search techniques. For detailed examples, look at this Python Notebook or the TypeScript Example

1. Setup

Import the necessary libraries and dependencies for working with LanceDB, OpenAI embeddings, and reranking.

2. Connect to LanceDB

Establish a connection to your LanceDB instance, with different options for Enterprise setups or open source. OSS
Enterprise For LanceDB Enterprise, set the db:// URI, region and the host override to your private cloud endpoint:

3. Configure Embedding Model

Set up the any embedding model that will convert text into vector representations for semantic search.

4. Create Table & Schema

Define the data structure for your documents, including both the text content and its vector representation.

5. Add Data

Insert sample documents into your table, which will be used for both semantic and keyword search.

6. Build Full Text Index

Create a full-text search index on the text column to enable keyword-based search capabilities.

7. Set Reranker [Optional]

Initialize the reranker that will combine and rank results from both semantic and keyword search. By default, lancedb uses RRF reranker, but you can choose other rerankers like Cohere, CrossEncoder, or others lister in integrations section.
Perform a hybrid search query that combines semantic similarity with keyword matching, using the specified reranker to merge and rank the results.

9. Hybrid Search - Explicit Vector and Text Query pattern

You can also pass the vector and text query explicitly. This is useful if you’re not using the embedding API or if you’re using a separate embedder service.

Query controls

Hybrid queries inherit the same builder API as vector and FTS queries, so the same knobs for filtering, distance bounds, and row identity apply. These compose with .rerank(...) and the explicit .vector() / .text() form shown above.
Always set .limit(...) on production hybrid queries. Without an explicit cap, the query builder does not give you a useful top-k contract to tune, and it may materialize more rows than you intended before reranking.

Returning row IDs

Pass with_row_id(True) (Python) or withRowId() (TypeScript) to include the internal _rowid column in the results. This is useful for joining hybrid results back to a primary table, or for deduping across multiple queries:

Bounding vector distance

distance_range(lower, upper) (Python) and distanceRange(lower, upper) (TypeScript) constrain the vector half of the hybrid query to the half-open interval [lower, upper). This is helpful when you want to cap how far semantic candidates can drift from the query vector before reranking:
Either bound can be omitted to leave that side unbounded.

Prefilter vs. postfilter

When the query carries a metadata filter via where(...), you can choose whether the filter runs before or after the vector and FTS sub-queries. Prefiltering (the default) applies where to the candidate set before scoring, which is usually what you want — it shrinks the working set and benefits from any scalar indexes on the filter columns. Postfiltering runs the filter on the already-ranked top-k from each sub-query; this can be faster when the filter is non-selective or unindexed, but it may return fewer than limit rows because some of the top-k may be filtered out.
The choice gets baked into both sub-queries, so the vector and FTS halves see the filter applied the same way. Use explain_plan on a hybrid query to see whether the filter pushed into the scan or ran as a separate FilterExec step.

More on Reranking

You can perform hybrid search in LanceDB by combining the results of semantic and full-text search via a reranking algorithm of your choice. LanceDB comes with built-in rerankers and you can implement your own custom reranker as well. By default, LanceDB uses RRFReranker(), which uses reciprocal rank fusion score, to combine and rerank the results of semantic and full-text search. You can customize the hyperparameters as needed or write your own custom reranker. Here’s how you can use any of the available rerankers: