Lexical search retrieves chunks by matching query terms against literal text. It excels at exact identifiers, citations, partial matches, boolean queries, and phrases, but misses vocabulary changes and synonyms. section 5.5
A production retrieval interface accepts the raw query, index name, result limit, and metadata filters, then returns the same SearchResult objects used by vector retrieval. Backends implement the method only when they support keyword search; unsupported implementations raise NotImplementedError. section 5.5.1
Use lexical search when the query depends on exact wording:
- Retrieve an error-code match such as "ERR-4012" rather than semantically similar discussions of errors. section 5.5
- Find a precise compliance citation such as "HIPAA 164.512(a)". section 5.5
- Combine literal matching with vector similarity in hybrid search, then merge both ranked lists with Reciprocal Rank Fusion. section 5.5.4
Exact-match retrieval
A search for "ERR-4012" should return the chunk containing that code. A semantic-only search may instead rank chunks mentioning errors, problems, or other error codes; lexical search preserves the identifier signal. section 5.5
Listing 5.21 Adding keyword search to the VectorStore interface
class VectorStore(ABC):
# ... write operations from listing 5.16 ...
# ... search from listing 5.17 ...
def keyword_search(
self,
index_name: str,
query: str, #A
top_k: int = 5,
metadata_filters: Optional[Dict[str, str]] = None
) -> List[SearchResult]: #B
"""Find chunks matching the query by keyword."""
raise NotImplementedError( #C
f"{type(self).__name__}
➥does not support keyword search" #C
) #CInspect the VectorStore interface for the raw-query method, shared filters, consistent result type, and explicit unsupported-backend behavior. section 5.5.1













