Part 10 of 10
Multidimensional and full-text indexes
B-trees and LSM-trees handle range queries on one attribute, such as names that start with "M". A concatenated index joins several fields into one key in a fixed order, like a contact list sorted by last name and then first name. It can find people by last name, or by last name and first name, but it is no help for first name alone.
Multidimensional indexes filter on several columns at once. The classic case is a map: find cafes inside a box, which means a latitude range AND a longitude range. A concatenated (latitude, longitude) index can narrow only one of them. It returns a whole band of latitudes at every longitude, or the reverse, not just the box. Options:
- Turn each 2D point into one number with a space-filling curve, then use a normal B-tree.
- More often, use a spatial index, like an R-tree or a Bkd-tree, which keeps points that are close together in the same subtree.
- Use a regular grid of squares, triangles, or hexagons.
This is not only for maps. A shop could search products by (red, green, blue) color values, or a weather site by (date, rainfall). A 2D index narrows both at once, while a 1D index scans one range and filters the other row by row.
Full-text search
Full-text search finds documents by words anywhere in their text. You can think of it as a multidimensional query where each word is its own dimension: a document scores 1 if it contains the word and 0 if not. A search for "cheap flights" wants documents with 1 in both, out of a huge number of dimensions.
- An inverted index maps each term to a postings list: the IDs of the documents that contain it.
- If document IDs are numbered in order, a postings list can be a sparse bitmap.
- A bitwise AND of two lists finds documents with both words, even when they are run-length encoded, just like in column stores.
- N-grams index every substring of length n instead of whole words.
- The trigrams of "train" are "tra", "rai", and "ain".
- A trigram index can find any substring of 3 or more letters, and even support regular expressions, but the index is large.
- Typos are handled by also matching words that are only a few edits away, where one edit adds, removes, or changes a single letter.
Vector embeddings
Semantic search matches meaning and intent, going past matching words, synonyms, or typos. For example, a search for "my parcel never arrived" should find a help page titled "Tracking a missing delivery". It matters for AI apps such as retrieval-augmented generation (RAG), where search results are fed to an LLM to shape its answer.
- An embedding model, often built on an LLM, turns a document into a vector embedding.
- A vector embedding is a list of decimal numbers that marks a point in a space with many dimensions.
- Documents with similar meaning end up close together.
- In a toy 3D space, pages about baking bread and about sourdough starters would sit near each other, while a page about tax law would sit far away.
- Real embeddings often have over 1,000 numbers, and no single number has a meaning a person could read.
- Cosine similarity measures closeness by the angle between two vectors.
- Euclidean distance measures the straight line between them.
Watch the word "vector". In vectorized processing it means a batch of values handled together. In embeddings it means a list of numbers that gives a location.
To search, embed the user's query, plus any context such as their location. Then a vector index finds the stored documents whose vectors are nearest to the query's vector. R-trees break down with this many dimensions, so special vector indexes are used:
| Index | How it works | Result |
|---|---|---|
| Flat | Store vectors as they are and compare the query with every one | Exact but slow |
| IVF (inverted file) | Group vectors into clusters around centre points and search only some clusters; the number of clusters checked is called probes | Approximate, since a close match may sit in an unchecked cluster; more probes means more accurate but slower |
| HNSW | Several layers of graphs, where nodes are vectors and edges link near neighbors; start in the sparse top layer, get close, drop down a layer, and repeat until the bottom | Approximate and fast |
Key takeaways
- OLTP engines serve many small reads and writes by key, and their indexes are sorted maps that also support range queries.
- An index trades slower writes and more disk for faster reads, so pick indexes from your real queries.
- LSM-trees buffer writes in a memtable, flush sorted immutable files, merge them in the background, and use Bloom filters to skip files.
- B-trees keep fixed-size pages in a shallow balanced tree, overwrite them in place, and rely on a write-ahead log for crash safety.
- LSM-trees usually win on writes and disk space, and B-trees usually win on reads and range queries, but test with your own workload.
- Secondary, clustered, and covering indexes differ in what they store next to the key, and map-style queries need a multidimensional index.
- Analytics engines store data by column, compress it with bitmaps and run-length encoding, and save CPU with compiled or vectorized queries.
- Inverted indexes power keyword search, and vector indexes like IVF and HNSW power search by meaning.