liveBook · Manning

Lexical search

As explained in Designing AI Systems

Keyword search retrieves content by matching literal terms or tokens in a query against indexed content.

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
        )    #C

Inspect the VectorStore interface for the raw-query method, shared filters, consistent result type, and explicit unsupported-backend behavior. section 5.5.1

How Designing AI Systems covers it

How deep it goes
Defines strengths, limitations, interface behavior, and hybrid orchestration, but stops short of backend-specific matching implementation.
Examples
Uses ERR-4012 and HIPAA 164.512(a) to demonstrate exact identifier and citation retrieval.
What it assumes
Assumes indexed chunks and a retrieval service, while contrasting literal matching with embeddings and semantic search.
What you can do after
Build keyword retrieval through a VectorStore interface and combine it with vector results through hybrid search.
How it connects
Connects lexical search to semantic search, vector similarity search, hybrid search, full-text search, and Reciprocal Rank Fusion.

Also explained in

Compare how 14 books explain lexical search →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Designing AI Systems. The section links point to the parts of the book it draws on.