Isolate Vector Search
Scope searches to a single Knowledge instance when multiple instances share the same vector database.
When multiple Knowledge instances share the same vector database, searches return results from all instances by default. Set isolate_vector_search=True to scope each instance's searches to its own data.
1from kern.knowledge.knowledge import Knowledge2from kern.vectordb.pgvector import PgVector34vector_db = PgVector(5 table_name="shared_vectors",6 db_url="postgresql+psycopg://ai:ai@localhost:5532/ai",7)89# Only returns results from documents this instance inserted10knowledge = Knowledge(11 name="support-docs",12 vector_db=vector_db,13 isolate_vector_search=True,14)How It Works
When isolate_vector_search=True:
- Insert: Each document gets
linked_tometadata set to the Knowledge instance'sname. - Search: A
linked_tofilter is automatically injected, so only matching documents are returned.
When isolate_vector_search=False (default):
- Insert: No
linked_tometadata is added. - Search: No
linked_tofilter is applied. Searches return results from all documents in the vector database.
When to Use
| Scenario | isolate_vector_search |
|---|---|
| Single Knowledge instance | False (default) |
| Multiple instances, each with its own vector database | False (default) |
| Multiple instances sharing one vector database, need isolation | True |
Example: Shared Database, Isolated Searches
1from kern.agent import Agent2from kern.knowledge.knowledge import Knowledge3from kern.vectordb.pgvector import PgVector45vector_db = PgVector(6 table_name="shared_vectors",7 db_url="postgresql+psycopg://ai:ai@localhost:5532/ai",8)910# Two knowledge instances sharing the same vector database11hr_knowledge = Knowledge(12 name="hr-docs",13 vector_db=vector_db,14 isolate_vector_search=True,15)1617engineering_knowledge = Knowledge(18 name="engineering-docs",19 vector_db=vector_db,20 isolate_vector_search=True,21)2223# Insert into each instance24hr_knowledge.insert(path="hr-policies/")25engineering_knowledge.insert(path="engineering-docs/")2627# This agent only searches HR documents28hr_agent = Agent(knowledge=hr_knowledge, search_knowledge=True)2930# This agent only searches engineering documents31eng_agent = Agent(knowledge=engineering_knowledge, search_knowledge=True)Backwards Compatibility
isolate_vector_search defaults to False. Existing Knowledge instances behave exactly as before.
Existing data does not have linked_to metadata
Documents indexed before this flag existed do not have linked_to in their vector database metadata. When you enable isolate_vector_search=True, searches filter for linked_to=<name>. Documents without this metadata field will not match and will be invisible to the isolated search.
Enabling isolate_vector_search=True with vector databases that don't have existing linked_to metadata will cause those documents to disappear from search results. You must re-index or manually update the metadata to restore them.
Combining with Manual Filters
When isolate_vector_search=True, the linked_to filter is automatically merged with any filters you pass, regardless of filter format:
1# Dict-based filters: linked_to is merged automatically2results = hr_knowledge.search(3 query="vacation policy",4 filters={"department": "legal"},5)6# Searches for: linked_to="hr-docs" AND department="legal"1# List-based filters (FilterExpr): linked_to is also injected automatically2from kern.filters import EQ34results = hr_knowledge.search(5 query="vacation policy",6 filters=[EQ("department", "legal")],7)8# Searches for: linked_to="hr-docs" AND department="legal"Instance Uniqueness
Each Knowledge instance must have a unique combination of name, database, and table. Two instances with the same name pointing to the same contents database and table will raise a ValueError at startup.
1from kern.db.postgres import PostgresDb2from kern.knowledge.knowledge import Knowledge3from kern.vectordb.pgvector import PgVector45contents_db = PostgresDb(6 db_url="postgresql+psycopg://ai:ai@localhost:5532/ai",7 knowledge_table="knowledge_contents",8)910vector_db = PgVector(11 table_name="shared_vectors",12 db_url="postgresql+psycopg://ai:ai@localhost:5532/ai",13)1415# These two instances will conflict because they share the same16# name, contents_db, and table17knowledge_a = Knowledge(18 name="my-docs",19 contents_db=contents_db,20 vector_db=vector_db,21)2223knowledge_b = Knowledge(24 name="my-docs", # same name25 contents_db=contents_db, # same database and table26 vector_db=vector_db,27)28# ValueError: Duplicate knowledge instances detectedTo fix this, give each instance a unique name, or point them to different contents databases or tables.
Requirements
- The Knowledge instance must have a
nameset. Without a name, nolinked_tometadata is added and no filter is applied, even whenisolate_vector_search=True. - The vector database must support metadata filtering. See Filtering for supported databases.
Next Steps
| Task | Guide |
|---|---|
| Filter by other metadata | Filtering |
| Set up a vector database | Vector Databases |
| Track content metadata | Contents DB |