> ## Documentation Index
> Fetch the complete documentation index at: https://docs.agno.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Keyword Search

> Rank knowledge documents with a vector database's lexical search implementation.

Keyword search uses the selected vector database's lexical search implementation.

```python theme={null}
from agno.knowledge.knowledge import Knowledge
from agno.vectordb.pgvector import PgVector, SearchType

db_url = "postgresql+psycopg://ai:ai@localhost:5532/ai"

knowledge = Knowledge(
    vector_db=PgVector(
        table_name="docs",
        db_url=db_url,
        search_type=SearchType.keyword,
    ),
)
```

## How It Works

With PgVector, keyword search:

1. Converts document content to a PostgreSQL `tsvector`.
2. Converts the query with `websearch_to_tsquery` by default.
3. Orders rows by PostgreSQL's `ts_rank_cd` score.

Set `prefix_match=True` to build a prefix query from the query tokens. Other vector databases implement keyword search differently.

## When to Use Keyword Search

| Query Pattern                     | Why Test Keyword Search                                        |
| --------------------------------- | -------------------------------------------------------------- |
| Product names and technical terms | Lexical ranking retains the query terms                        |
| Error codes and identifiers       | Embedding similarity may not preserve the token                |
| Quoted terms or operators         | PgVector's default parser accepts PostgreSQL web-search syntax |

Test vector search for conceptual queries. Test hybrid search when both lexical and vector signals affect relevance.

## Configuration

### Basic Setup

```python theme={null}
from agno.vectordb.pgvector import PgVector, SearchType

vector_db = PgVector(
    table_name="docs",
    db_url="postgresql+psycopg://ai:ai@localhost:5532/ai",
    search_type=SearchType.keyword,
    prefix_match=False,
)
```

<Note>
  PgVector applies a configured `reranker` to vector and hybrid search only. Keyword search results are ranked by PostgreSQL's full-text relevance score.
</Note>

## Example

Install the dependencies and start a PostgreSQL instance with pgvector enabled:

```bash theme={null}
uv pip install -U agno openai pgvector psycopg pypdf sqlalchemy
```

```python keyword_search.py theme={null}
from agno.knowledge.knowledge import Knowledge
from agno.vectordb.pgvector import PgVector, SearchType

db_url = "postgresql+psycopg://ai:ai@localhost:5532/ai"

knowledge = Knowledge(
    vector_db=PgVector(
        table_name="recipes",
        db_url=db_url,
        search_type=SearchType.keyword,
    ),
)

knowledge.insert(
    url="https://agno-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf",
)

results = knowledge.search("chicken coconut soup", max_results=5)
for doc in results:
    print(doc.content[:200])
```

## Next Steps

<CardGroup cols={2}>
  <Card title="Hybrid Search" icon="magnifying-glass" href="/knowledge/concepts/search-and-retrieval/hybrid-search">
    Combine keyword search with vector similarity
  </Card>

  <Card title="Vector Search" icon="brain" href="/knowledge/concepts/search-and-retrieval/vector-search">
    Search by semantic meaning
  </Card>
</CardGroup>
