Reference Manual | Prev: Chapter 15. HTML Parser | Next: Chapter 17. SMTP Mail
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:
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.
;; 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)
;; 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)
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)
(lambda-ir:initialize-document-context context)
(lambda-ir:document-context doc)
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.
;; 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.
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 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))
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.
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.
(lambda-ir:get-token-index token cluster)
(lambda-ir:token-intern string cluster)
(lambda-ir:get-token-stats token)
(lambda-ir:update-token-stats token)
(lambda-ir:make-bit-vector size)
(lambda-ir:bit-vector-cardinality bv)
(lambda-ir:map-bit-vector bv function)
(lambda-ir:map-bit-vector-accumulating bv function initial)
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.
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.
(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
:in-arg-function
:in-limiting-function
(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.
Query expressions use the following operators, which map directly to bit-vector operations:
(:and term1 term2 ...)
bit-and.
(:or term1 term2 ...)
bit-ior.
(:andc2 include exclude)
bit-andc2. Used for exclusion queries
(e.g., "lisp -java").
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")
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.
;; 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")
set-up-universe Functionset-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))
;; 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.
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.