Hybrid search
=============

Hybrid search combines semantic (vector/embedding) search on knowledge
base embeddings with one or more additional filter mechanisms:

- **Relational predicate filtering** — Filter results by any column in
  the source table, for example ``WHERE category = 'vegetarian'`` or
  ``WHERE published_date > '2024-01-01'`` .

- **Full-text search** — Rank results by keyword relevance using native
  PostgreSQL FTS, ensuring that specific words or phrases appear in the
  results.

These two approaches can be used independently or together alongside
semantic search.

AI pipelines handle knowledge base construction, including embedding
generation, as normal. Hybrid search is applied at query time by
constructing custom SQL that combines the vector table with your source
table. It is not a separately installed feature and no additional setup
is required beyond the prerequisites below.

Prerequisites
-------------

pgvector iterative index scans are required for all hybrid query
patterns. Native FTS is only needed if your queries use the keyword leg.

pgvector iterative index scans
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Requires pgvector 0.8.0 or later. Enable iterative index scans before
running hybrid queries — they allow the vector index to keep producing
candidates until your ``LIMIT`` is satisfied, even after filtering. See
:ref:`Writing hybrid search queries <Writing hybrid search queries>`  for the ``SET`` commands.

Native PostgreSQL FTS (no extension required)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Standard PostgreSQL full-text search (FTS) (``tsvector`` / ``ts_rank`` )
is available in every Postgres installation without additional
extensions. See :ref:`Setting up native PostgreSQL FTS <Setting up native PostgreSQL FTS>`  for setup steps.

Volume source limitation
------------------------

Hybrid search with relational predicate filtering or full-text search is
not supported for volume-sourced knowledge bases. Volume sources have no
backing Postgres table to join against, so there’s no source table to
filter or index.

Volume sourced knowledge bases continue to support standard semantic
search via ``aidb.retrieve_text()`` and ``aidb.retrieve_key()`` .

.. _hybrid-search-1:

Hybrid search
=============

Next steps
----------

- :ref:`Setting up native PostgreSQL FTS <Setting up native PostgreSQL FTS>`  — Add a ``tsvector`` column, GIN index, and trigger to
  your source table.

- :ref:`Writing hybrid search queries <Writing hybrid search queries>`  — SQL patterns for every combination: relational filter,
  Reciprocal Rank Fusion (RRF), weighted linear fusion. Includes a
  worked example with sample data and expected output.

- :ref:`Handling embedding service outages <Handling embedding service outages>`  — A three-tier fallback chain to keep queries returning
  results gracefully.

- :ref:`Pipelines API reference <Pipelines API reference>`  — Full reference for ``aidb.kb_query_encode()`` ,
  ``aidb.rerank_text()`` , and the ``aidb.knowledge_bases`` view.

.. toctree::
  :maxdepth: 3

  knowledge-bases--hybrid-search--set-up-native-fts
  knowledge-bases--hybrid-search--query-patterns
  knowledge-bases--hybrid-search--fallback-and-degradation
  knowledge-bases--hybrid-search--reference
