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 Knowledge2from kern.vectordb.pgvector import PgVector, SearchType34knowledge = Knowledge(5 vector_db=PgVector(6 table_name="docs",7 db_url=db_url,8 search_type=SearchType.keyword,9 ),10)How It Works
- Text parsing: Your query is broken into searchable terms
- Index lookup: The system finds documents containing those terms
- 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
| Scenario | Why Keyword Search Works |
|---|---|
| Searching for specific terms | Exact match on product names, codes, IDs |
| Error codes and identifiers | Precise matching without semantic interpretation |
| Technical terminology | Users know the exact terms to search |
| Structured data queries | Matching 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, SearchType23vector_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 CohereReranker23vector_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 Knowledge2from kern.vectordb.pgvector import PgVector, SearchType34db_url = "postgresql+psycopg://ai:ai@localhost:5532/ai"56knowledge = Knowledge(7 vector_db=PgVector(8 table_name="recipes",9 db_url=db_url,10 search_type=SearchType.keyword,11 ),12)1314# Load content15knowledge.insert(16 url="https://kern-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf",17)1819# Search by exact terms20results = knowledge.search("chicken coconut soup", max_results=5)21for doc in results:22 print(doc.content[:200])