CL-HTTP Chapter 16: Lambda-IR Information Retrieval


Reference Manual | Prev: Chapter 15. HTML Parser | Next: Chapter 17. SMTP Mail


Contents

  1. 16.1 Overview
  2. 16.2 Document Universe
  3. 16.3 Tokenization
  4. 16.4 Indexing
  5. 16.5 Search
  6. 16.6 LambdaVista

16.1 Overview

Lambda-IR (also known as the Information Retrieval Substrate) is a full-text indexing and retrieval system for Common Lisp, developed by Andrew J. Blumberg at the MIT AI Laboratory. It provides a bit-vector inverted index with constraint-based search, supporting boolean queries (AND, OR, ANDC2) over tokenized document collections.

Lambda-IR lives in the :LAMBDA-IR package (nicknamed :LR and :INFORMATION-RETRIEVAL-SUBSTRATE) and uses ANSI-COMMON-LISP and HTTP. It is designed to index web site content served by CL-HTTP, enabling full-text search over dynamically generated pages and static documents.

The architecture consists of four layers:

  1. Document Universe — A collection of documents to be indexed, with metadata and document contexts.
  2. Tokenization — Configurable tokenizers that split document text into tokens, with lexical feature extraction and stemming.
  3. Indexing — Bit-vector inverted indices that map tokens to the documents containing them, with incremental update support.
  4. Search — Constraint-based search that combines token lookups with boolean operations (AND, OR, ANDC2) to produce ranked result sets.

16.2 Document Universe

A document universe is the top-level container for a collection of indexed documents. It holds the tokenizer configuration, the token clusters (inverted indices), and the document metadata.

Creating a Document Universe

;; Create a new document universe
(setq universe (lambda-ir:make-document-universe))

;; Set it as the default
(lambda-ir:set-default-document-universe universe)

;; Retrieve the current default
(lambda-ir:default-document-universe)

Adding Documents

;; Create a document object with metadata
(setq doc (lambda-ir:make-document
            "my-document"
            '((:pathname #p"/path/to/file.txt")
              (:url "http://example.com/file.txt"))
            '(:text)))

;; Add the document to a context and index it
(lambda-ir:add-document context doc
                        :type-list '(:text)
                        :code-list (list #'read-text-file))

The :type-list specifies the content types of the document. The :code-list provides functions to extract text from the document for each type. After adding, tokenize and apply lexical features:

(lambda-ir:tokenize-and-apply-lexical-features context doc)

Document Contexts

A document context binds a document universe to specific tokenizers and lexical features for indexing:

(setq context
      (lambda-ir:make-document-context
        :in-document-universe (lambda-ir:default-document-universe)
        :in-tokenizer-names '("tok")
        :in-lexical-features (list lexical-features)))
(lambda-ir:make-document-context &key :in-document-universe :in-tokenizer-names :in-lexical-features)
Create a document context binding a universe to tokenizers and lexical features.
(lambda-ir:initialize-document-context context)
Reinitialize an existing context.
(lambda-ir:document-context doc)
Retrieve the context associated with a document.

16.3 Tokenization

Tokenizers split document text into tokens and store them in token clusters (the inverted index structure). Each tokenizer is configured with a tokenization function, capacity parameters, and content type filters.

Creating Tokenizers

;; Create a bit-vector-storing tokenizer
(setq tokenizer
      (lambda-ir:make-bit-vector-storing-tokenizer
        "tok"                    ; name
        #'lambda-ir:stock-tokenizer  ; tokenization function
        9600                     ; initial token capacity
        9600                     ; initial document capacity
        5                        ; cluster size
        '(:text)))               ; content types to index

;; Add the tokenizer to the universe
(lambda-ir:add-tokenizer universe tokenizer)

The stock-tokenizer function is the built-in tokenizer that splits text on configurable delimiter characters. It processes text sequentially, interning each token in the token cluster.

Delimiter Configuration

Delimiters are characters that separate tokens. The default delimiters include space, punctuation, and control characters. Use lambda-ir:delimiter-p to test individual characters and customize tokenization by providing alternative delimiter predicates to the tokenizer function.

Lexical Features

Lexical features are post-tokenization filters and transformations applied to tokens. They enable stemming, stop-word removal, and token statistics collection:

;; Define a filter that removes short tokens
(defun filter-words (token)
  (> (length (lambda-ir:token-datum token)) 2))

;; Create a computation type and computation
(make-base-computation-type "filter-type" #'filter-words '("token"))
(setq filter (make-base-computation "filter" "filter-type" nil))

;; Build a lexical feature set
(setq features
      (lambda-ir:make-lexical-feature-set
        "feature-set"
        filter
        (list (lambda-ir:make-token-feature))
        nil
        #'lambda-ir:update-token-stats))

Stemming

Lambda-IR supports pluggable stemming functions that reduce tokens to their root forms before indexing. Register stemming functions with lambda-ir:add-stemming-function and reference them in the lexical feature pipeline.

16.4 Indexing

The index is a bit-vector inverted index: each token maps to a bit vector where bit n is set if document n contains that token. This representation enables extremely fast boolean operations via hardware bit-vector instructions.

Index Operations

(lambda-ir:get-token-index token cluster)
Retrieve the bit vector for a token from a cluster.
(lambda-ir:token-intern string cluster)
Intern a token string in a cluster, returning the token object.
(lambda-ir:get-token-stats token)
Retrieve frequency statistics for a token.
(lambda-ir:update-token-stats token)
Update the statistics for a token after indexing.

Bit Vector Operations

(lambda-ir:make-bit-vector size)
Create a new bit vector of the given size.
(lambda-ir:bit-vector-cardinality bv)
Count the number of set bits (number of matching documents).
(lambda-ir:map-bit-vector bv function)
Apply function to each set bit index.
(lambda-ir:map-bit-vector-accumulating bv function initial)
Map over set bits, accumulating results.

Incremental Updates

New documents can be added to an existing index without rebuilding. The add-document and tokenize-and-apply-lexical-features functions extend the bit vectors as needed. The soft-add-tokenizer function adds a tokenizer to an existing universe without disturbing existing indices.

16.5 Search

Search in Lambda-IR is constraint-based: a search constraint specifies how to process the query string, which boolean operations to apply, and how to format the results.

Search Constraints

(setq search-constraint
      (lambda-ir:make-search-constraint
        :in-search-arguments '(search-string)
        :in-arg-function #'altavista-style-query-processing
        :in-limiting-args nil
        :in-limiting-function #'display-names))
:in-search-arguments
The names of the query parameters.
:in-arg-function
A function that processes the query string into a boolean constraint structure.
:in-limiting-function
A function that post-processes the result bit vector into a user-facing result.

Performing Searches

(lambda-ir:perform-search-constraint
  context search-constraint "common lisp server")

perform-search-constraint tokenizes the query, constructs boolean bit-vector operations, and returns the result. The constraint's argument function translates the query into a boolean expression tree.

Boolean Query Operators

Query expressions use the following operators, which map directly to bit-vector operations:

(:and term1 term2 ...)
Documents containing all terms. Maps to bit-and.
(:or term1 term2 ...)
Documents containing any term. Maps to bit-ior.
(:andc2 include exclude)
Documents matching include but not exclude. Maps to bit-andc2. Used for exclusion queries (e.g., "lisp -java").

AltaVista-Style Queries

The LambdaVista example implements AltaVista-style query processing where terms prefixed with - are exclusions:

;; "lisp server -java" becomes:
;; (:andc2 (:and "lisp" "server") (:or "java"))

;; "lisp server" (no exclusions) becomes:
;; (:and "lisp" "server")

16.6 LambdaVista

LambdaVista is the example web search application built on Lambda-IR. It provides a complete indexing and search interface for a CL-HTTP web site, with a web-based query form and results page.

Setting Up LambdaVista

;; 1. Create a document universe with a tokenizer
(setq universe (lambda-ir:set-up-universe))
(lambda-ir:set-default-document-universe universe)

;; 2. Set up lexical features (token filtering, stemming)
(lambda-ir:set-up-lexical-features-to-use)

;; 3. Create a document context
(setq *document-context*
      (lambda-ir:make-document-context
        :in-document-universe universe
        :in-tokenizer-names '("tok")
        :in-lexical-features
        (list lambda-ir::lexical-features-to-use)))

;; 4. Index documents
(lambda-ir:add-url-to-document-universe
  #p"/path/to/document.html"
  "http://example.com/document.html")

The set-up-universe Function

set-up-universe creates a document universe pre-configured with a stock tokenizer named "tok" with capacity for 9600 tokens, 9600 documents, cluster size 5, and content type :text:

(defun set-up-universe ()
  (let ((universe (make-document-universe))
        (tokenizer (make-bit-vector-storing-tokenizer
                     "tok" #'stock-tokenizer
                     9600 9600 5 '(:text))))
    (add-tokenizer universe tokenizer)
    universe))

Searching

;; Perform a search
(lambda-ir:perform-search-constraint
  *document-context*
  lambda-ir::archive-search-constraint
  "web server")

The archive-search-constraint uses AltaVista-style query processing and returns document names from the result bit vector. The limiting function display-names maps bit indices back to document names via map-bit-vector-accumulating.

Integration with CL-HTTP

In a typical deployment, LambdaVista is configured as a computed form URL that accepts a search query, performs the search, and renders results as an HTML page. The W4 web walker can be used to automatically index a site by walking it and feeding each page to Lambda-IR. This creates a self-indexing web server where new content is automatically searchable.