Search words inside text, with ranking, using an inverted index (Elasticsearch).
A full-text search engine finds documents that contain the words you type, and puts the best matches first. It does this with an inverted index.
A normal database index helps you find a row by an exact value. But what if the user types "quick dog" and you want every article that mentions both words, anywhere in the text, even as "dogs" or "Quickly"? A SQL LIKE '%dog%' would have to read every row. That is slow and has no idea which result is better.
A search engine flips the problem. When a document arrives, it analyzes the text: split into words, lowercase, remove tiny words like "the" (stop words), and reduce words to a root (stemming, so "dogs" becomes "dog"). Then it writes each word into the inverted index, with the documents where it appears. A query is analyzed the same way, and the engine looks up each word's list and combines them. Finally it scores each document.
| Step | What it does | Example |
|---|---|---|
| Tokenize | Split the text into words. | "Quick dogs!" → Quick, dogs |
| Lowercase | Ignore capital letters. | Quick → quick |
| Stop words | Drop very common, low-value words. | the, and, is (often removed, or kept in modern setups) |
| Stem / lemmatize | Reduce to a root form. | dogs → dog, running → run |
| Synonyms | Treat some words as equal. | tv = television |
| N-grams | Cut words into pieces for partial matches and autocomplete. | sea, ear, arc, rch |
| Idea | Meaning |
|---|---|
| Term frequency (TF) | How often the word appears in this document. More is better. |
| Inverse document frequency (IDF) | How rare the word is across all documents. A rare word counts for more than a common one. |
| Length normalisation | A hit in a short title beats the same hit in a very long text. |
| BM25 | The modern default in Lucene, Elasticsearch and Solr. A tuned relative of TF-IDF that stops very frequent words from scoring without limit. |
| Boosts | You can give extra weight to fields (title above body) or to fresh and popular items. |
A teaching model: the score is (term frequency divided by the square root of document length) times log(1 + N / documents with the word), added over the query words. Real engines such as Elasticsearch default to BM25, a close and better-tuned relative of TF-IDF, and use proper stemmers and language rules.
PUT /products/_doc/1
{ "title": "Quick brown fox toy", "price": 12, "brand": "Acme" }
GET /products/_search
{
"query": {
"bool": {
"must": { "match": { "title": "quick fox" } },
"filter": { "range": { "price": { "lte": 20 } } }
}
}
}| Term | Meaning in easy words |
|---|---|
| Inverted index | A map from each word to the documents (and positions) that contain it |
| Document | One searchable record, usually JSON |
| Token / term | A word (or piece of a word) after analysis |
| Analyzer | The pipeline that turns text into tokens: tokenizer plus filters |
| Posting list | The list of documents for one term |
| Stop words | Very common words that are often dropped |
| Stemming | Cutting a word down to its root so forms match |
| TF-IDF | A score: frequent in this document, rare in all documents is good |
| BM25 | The default ranking function in modern engines, a refined TF-IDF |
| Shard / replica | A slice of the index / a copy of that slice |
| Segment | An immutable chunk of index files. Lucene merges small segments into big ones |
| Relevance | How well a result matches what the user wants |
| Precision / recall | Share of results that are right / share of right items that were found |
| Choice | We gain | We lose |
|---|---|---|
| More analysis (stemming, synonyms, n-grams) | Finds more matches | Bigger index, more noise, lower precision |
| More shards | Parallel search and writes | Overhead per shard, harder to change later |
| More replicas | Availability and read capacity | Disk cost, slower indexing |
| Fast refresh | Newer data is searchable sooner | More work, smaller segments |
| Rich scoring | Better relevance | Slower queries, harder to explain |