Keyword Search

Find content using exact word and phrase matching.

Keyword search finds content by matching exact words and phrases. It uses your database's full-text search capabilities to find documents containing specific terms.

1from kern.knowledge.knowledge import Knowledge
2from kern.vectordb.pgvector import PgVector, SearchType
3
4knowledge = Knowledge(
5 vector_db=PgVector(
6 table_name="docs",
7 db_url=db_url,
8 search_type=SearchType.keyword,
9 ),
10)

How It Works

  1. Text parsing: Your query is broken into searchable terms
  2. Index lookup: The system finds documents containing those terms
  3. Ranking: Results are ordered by relevance (term frequency, document length, etc.)

When using PgVector, this leverages PostgreSQL's built-in full-text search. Other databases use their native text search capabilities.

When to Use Keyword Search

ScenarioWhy Keyword Search Works
Searching for specific termsExact match on product names, codes, IDs
Error codes and identifiersPrecise matching without semantic interpretation
Technical terminologyUsers know the exact terms to search
Structured data queriesMatching specific field values

Use vector search if users phrase things differently than your docs. Use hybrid search if you want both exact matching and semantic understanding.

Configuration

Basic Setup

1from kern.vectordb.pgvector import PgVector, SearchType
2
3vector_db = PgVector(
4 table_name="docs",
5 db_url=db_url,
6 search_type=SearchType.keyword,
7)

With Reranking

Add a reranker to improve result ordering:

1from kern.knowledge.reranker.cohere import CohereReranker
2
3vector_db = PgVector(
4 table_name="docs",
5 db_url=db_url,
6 search_type=SearchType.keyword,
7 reranker=CohereReranker(),
8)

Example

1from kern.knowledge.knowledge import Knowledge
2from kern.vectordb.pgvector import PgVector, SearchType
3
4db_url = "postgresql+psycopg://ai:ai@localhost:5532/ai"
5
6knowledge = Knowledge(
7 vector_db=PgVector(
8 table_name="recipes",
9 db_url=db_url,
10 search_type=SearchType.keyword,
11 ),
12)
13
14# Load content
15knowledge.insert(
16 url="https://kern-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf",
17)
18
19# Search by exact terms
20results = knowledge.search("chicken coconut soup", max_results=5)
21for doc in results:
22 print(doc.content[:200])

Next Steps