liveBook · Manning

Keyword search index

As explained in Natural Language Processing in Action, Second Edition

A search index is a data structure that organizes terms and their associated documents to support efficient retrieval.

A keyword search index organizes terms by their presence or absence in documents, making later retrieval efficient. A bag-of-words vector treats a document like a chord: its words appear together rather than replaying sequentially. section 2.5.2

A practical index-building workflow:

  • Build a vocabulary from the unique tokens in the document collection. section 2.5.2
  • Ignore uninformative stop words when they are unlikely to help keyword searches. section 2.5.2
  • Encode each document as a fixed-length binary vector and use it to retrieve matching documents. section 2.5.2

Scaling the representation

Limiting a vocabulary to 10,000 important words compresses an imaginary 3,500-sentence book to about 10 kilobytes, while a 3,000-book corpus takes about 30 megabytes. The same representation supports fast document lookup and underlies tools such as Elasticsearch, Solr, and PostgreSQL. section 2.5.2

>>> bow = sorted(set(re.findall(pattern, text)))
>>> bow[:9]
 [',', '.', 'Liesel', 'Trust', 'and', 'arrived', 'clouds', 'hands', 'her']
>>> bow[9:19]
['hold', 'in', 'like', 'me', 'on', 'out', 'rain', 'she', 'the', 'their']
>>> bow[19:27]
['them', 'they', 'though', 'way', 'were', 'when', 'words', 'would']

Inspect the extracted vocabulary and see the unique tokens that define the vector dimensions. section 2.5.2

How Natural Language Processing in Action, Second Edition covers it

How deep it goes
Explains vocabulary construction, stop-word removal, binary vectors, compression, and constant-time similarity, but not index internals.
Examples
Uses extracted vocabulary listings, an imaginary book corpus, and Elasticsearch, Solr, and PostgreSQL.
What it assumes
Assumes tokenized documents and a vocabulary while teaching document-level word-presence encoding.
What you can do after
Apply binary bag-of-words vectors to filter documents for information retrieval and full-text search.
How it connects
Connects bag-of-words vectors, tokenization, stop words, information retrieval, and full-text search.

Also explained in

Compare how 3 books explain keyword search index →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Natural Language Processing in Action, Second Edition. The section links point to the parts of the book it draws on.