Skip to content
AI.info

Natural language processing

Lexical Search, Indexing, and Ranking

Design lexical retrieval with inverted indexes, term weighting, fields, query processing, filters, candidate recall, and ranking evaluation.

By the end you can

Analogy

An index as a book's back matter

A reader looking for every page that mentions a rare regulation code turns to the book's detailed index. The index maps terms to locations, while cross-references and headings help prioritize the useful pages.

A book index covers one fixed book, while modern search handles millions of changing documents, scoring, permissions, and user behavior. The central inversion holds in both: terms point to documents instead of every query scanning every document.

An inverted index turns lexical evidence into fast candidate access.

Visual

How lexical search answers a query

The stages can be measured and debugged independently.

FigureProcess · 5 steps
  1. 1. Ingest and analyze documents

    Normalize selected fields, tokenize, store positions, and apply permissions.

  2. 2. Build postings

    Map terms to documents, frequencies, positions, and field information.

  3. 3. Analyze the query

    Preserve exact identifiers while expanding or correcting ordinary language cautiously.

  4. 4. Retrieve and score candidates

    Combine term matching, rarity, field weights, length, filters, and recency.

  5. 5. Present and learn

    Show snippets and facets, log interactions, and collect relevance judgments.

Comparison

Several kinds of lexical matching

A useful search system combines them according to the query and corpus.

The first column's claim — that exact term matching is strong for names and codes — has been measured, and the size of the advantage is the part worth carrying. A Princeton group built a benchmark for it out of Wikidata: 24 relations turned into templated questions, up to 1,000 sampled facts per relation. They called it EntityQuestions and published it in 2021.

On Natural Questions the dense retriever DPR reaches 80.1 top-20 accuracy against BM25's 64.5. That is the ordering everyone expects. On EntityQuestions the ordering inverts. BM25 scores 71.2, against 49.7 for DPR trained on NQ and 56.7 for DPR trained on four datasets. Sciavolino and colleagues state it without hedging: “DPR trained on NQ significantly underperforms BM25 on almost all sets of questions.” On the single relation “Where was [E] born?” BM25 takes 75.2 against DPR(NQ)'s 25.4, a gap of 49.8 points absolute.

A second lab reproduced the reversal on the same benchmark. Meta AI reports EntityQuestions Acc@20 of 56.6 for DPR against 70.8 for BM25, micro-averaged. Its abstract puts the shortfall in one clause: dense retrievers “still lag behind sparse methods such as BM25 in their ability to reliably match salient phrases and rare entities in the query”. Two independent runs, the same direction. On queries whose whole content is a rare name, literal token matching is not a fallback.

FigureComparison · 4 columns

Exact term or phrase

Require literal token or ordered phrase matches.

  • Strong for names and codes
  • High precision
  • Sensitive to variants
  • Useful with quoted queries

Normalized lexical match

Use casing, morphology, spelling, or synonym rules.

  • Improves recall
  • Can over-expand
  • Language-specific
  • Needs query diagnostics

Fielded match

Weight or require title, body, tag, date, or metadata fields.

  • Uses document structure
  • Supports filters
  • Schema quality matters
  • Permissions may be field-specific

Proximity and passage match

Reward terms occurring near one another or inside a relevant section.

  • Preserves local evidence
  • Supports snippets
  • Position index cost
  • Window choice matters

Term rarity and document length shape lexical ranking

BM25-style ranking rewards query terms according to their document frequency, term occurrence, and a saturating treatment of repeated terms, while normalizing for document length. Those parameters are not a family of options a team picks from. In mainstream engines they are two fixed numbers, shipped as the default.

Apache Lucene's IndexSearcher carries one line under the comment `// the default Similarity`: `private static final Similarity defaultSimilarity = new BM25Similarity();`. The javadoc for that no-argument constructor reads “BM25 with these default values:” k1 = 1.2, b = 0.75, discountOverlaps = true. Elasticsearch is built on Lucene, and its index-settings reference lists “BM25 similarity (default)” with the same k1 = 1.2 and b = 0.75, and discount_overlaps true. Of the first constant, Elasticsearch's “Similarity settings” page says: “Controls non-linear term frequency normalization (saturation). The default value is 1.2.” Almost every lexical score a reader will ever be asked to explain was produced by those two untouched numbers.

A score has no universal probability meaning and is usually comparable only within a query and index configuration; product relevance can also require authority, freshness, permissions, and business rules.

A lexical score is ranking evidence under one query and index—not calibrated truth.

Case

BM25 stayed the robust baseline across eighteen retrieval datasets

Lexical ranking went through a hard test in 2021. BEIR assembled “a careful selection of 18 publicly available datasets from diverse text retrieval tasks and domains” and ran “10 state-of-the-art retrieval systems including lexical, sparse, dense, late-interaction and re-ranking architectures” across all of them. Thakur and colleagues built it, and published it at NeurIPS.

Their summary of the outcome runs to one clause: “Our results show BM25 is a robust baseline”. The dense and sparse retrieval models, though cheaper to run, “often underperform other approaches”. Term statistics and length normalisation, trained on nothing and configured with k1 = 1.2 and b = 0.75, still set the line the neural systems had to clear.

Example

Queries that deserve different processing

One automatic expansion policy can damage the most valuable searches. The mix of intents is not a matter of designer intuition either. It has been counted on real logs, twice, by different people.

AltaVista's queries were counted two ways. An interstitial survey produced 3,190 valid returns between 26 June and 3 November 2001. Separately, 400 queries were inspected by hand, drawn from a random sample of 1,000 daily log queries. Navigational came out at 24.5% in the survey against 20% in the log. Informational at an estimated 39% against 48%. Transactional at more than 22% (estimated 36%) against 30%. Andrei Broder, who ran both counts at IBM Research, states in his 2002 introduction what the counting was for: “In fact as we show later, informational queries constitute less than 50% of web searches.” The class everyone designs for was under half.

Jansen and colleagues applied the same three categories to a different engine's log. Dogpile.com, 6 May 2005: 1,523,793 queries from 534,507 users. More than 80% came out informational, and about 10% each navigational and transactional. Their automatic classifier agreed with 400 manually coded queries 74% of the time. One taxonomy, two corpora, proportions that differ by tens of points. That is the argument for measuring your own log before choosing one analysis policy for all of it.

  • Identifier lookup: “ZX-8841” should preserve punctuation and exact character order.
  • Known item: a user enters a product, case, person, or document name — the entity-centric case where BM25 held 71.2 top-20 accuracy against DPR's 49.7.
  • Exploratory topic: “how to appeal a transit penalty” benefits from terms, synonyms, and good passages — Broder's informational class, an estimated 39% to 48% of AltaVista queries and more than 80% of the Dogpile log.
  • Natural-language question: the query expresses a need that may not share document wording.
  • Filtered request: “refund policy in Italy after March 2025” combines text with structured constraints.
  • Navigational query: the user wants one official page rather than a broad topic list — 20% to 24.5% of AltaVista queries, about 10% at Dogpile.

Key idea

Search must enforce access before presentation

Filtering unauthorized results after ranking can leak titles, snippets, counts, timing, or the existence of confidential documents; permissions should be part of candidate eligibility and cache design.

This failure has a number. In Elasticsearch before 7.11.2 and 6.8.15, with Document or Field Level Security enabled, certain cross-cluster search queries did not preserve security permissions. Elastic disclosed the flaw as ESA-2021-08 on 23 March 2021, and it reached the NVD on 13 May as CVE-2021-22137. The description is the whole lesson in one sentence: “This could result in the search disclosing the existence of documents the attacker should not be able to view.”

NVD scores it 5.3, medium, with primary weakness CWE-281, Improper Preservation of Permissions; MITRE files the same record under CWE-200, Exposure of Sensitive Information to an Unauthorized Actor. The vendor advisory offered nothing to switch off in the meantime: “Anyone using Document or Field Level Security should upgrade to Elasticsearch version 7.11.2 or 6.8.15. There is no known workaround for this flaw.” The feature designed to hide documents leaked their existence. The only remedy was a new binary.

Index updates, deletions, tenant boundaries, and user roles require tests. Relevance cannot override authorization.

A document is not a candidate if the current requester is not allowed to know it exists.

Steps

Build a lexical search baseline worth trusting

A disciplined lexical system is both a product and a benchmark for neural retrieval.

FigureProcess · 5 steps
  1. 1. Define corpus and fields

    Specify authoritative sources, permissions, freshness, and document structure.

  2. 2. Create query–relevance judgments

    Sample real intents, exact lookups, rare terms, and no-answer cases.

  3. 3. Tune analysis and ranking

    Test tokenization, phrases, field boosts, spelling, synonyms, and length controls.

  4. 4. Inspect candidate failures

    Separate missing documents, query analysis, filters, stale data, and ranking errors.

  5. 5. Instrument the interface

    Measure reformulation, abandonment, click position, success, and misleading snippets.

Candidate recall comes before reranker sophistication

If no relevant document reaches the candidate set, a later reranker cannot rescue it; measure recall at several depths, then ranking quality such as reciprocal rank or discounted gain for queries with graded relevance.

The two numbers come apart far enough to see. Take the MS MARCO passage dev-small set of 6,980 queries and a plain BM25 first stage: Anserini, run as `-bm25 -bm25.k1 0.82 -bm25.b 0.68 -hits 1000`. A relevant passage lands somewhere inside the top 1,000 for 85.73% of queries. trec_eval prints `recall_1000 all 0.8573`, with map 0.1957. The same run's own top-10 ordering scores “MRR @10: 0.18741227770955546”.

One system, one index, one pass: recall_1000 is 0.8573 and MRR@10 is 0.1874. Baidu's RocketQA team lists the identical baseline from outside the group as “BM25 (anserini) (Yang et al., 2017) - 18.7 59.2 85.7” on MS MARCO Dev, and the leaderboard entry it matches, “BM25 (Lucene8, tuned)”, is dated 2019/06/26. Anserini's own documentation says which half it watches: “In many retrieval applications, average precision and recall@1000 are the two metrics we care about the most.” A reranker placed on that stage inherits 85.73% as its ceiling, and has most of the ordering left to win.

Aggregate metrics need slices for identifiers, languages, query length, freshness, permissions, and no-answer cases. Interaction logs are biased by current ranking and interface. So use controlled judgments and experiments too.

BEIR's authors had to pick a measure, and their reasoning is the part to copy. They rule out two families by name. “Decision support metrics such as Precision and Recall which are both rank unaware are not suitable”. “Binary rank-aware metrics such as MRR (Mean Reciprocal Rate) and MAP (Mean Average Precision) fail to evaluate tasks with graded relevance judgements”. They settle on normalised discounted cumulative gain. They “compute nDCG@10 for all datasets” with “the official TREC evaluation tool”. One metric, one stated reason, every dataset. That is what makes the table comparable at all.

recall_1000 0.8573 and MRR@10 0.1874 came out of one run: they name two different failures.

Explain one failed search from query to index

Choose a real query with an unsatisfactory result. Trace normalization, tokens, expansions, filters, candidate postings, term statistics, field scores, and snippet selection.

Propose the smallest change that would recover the relevant document without damaging exact lookups or permissions. Add the case to a regression set.

Take the query from a log rather than from a colleague. MS MARCO, released in 2016, is built that way: “all questions are sampled from real anonymized user queries”. The passages “are extracted from real web documents using the most advanced version of the Bing search engine”, and the answers are human generated. The first release was “100,000 queries with their corresponding answers”. Queries of that kind are abbreviated, misspelled and ambiguous in ways a team writing its own test set never reproduces.

The same collection also shows how thin judged evidence is at that scale. The TREC 2019 Deep Learning Track overview states it in its abstract: “The passage retrieval task has a corpus of 8.8 million passages with 503 thousand training queries, for which we generate a reusable test set of 43 queries”. A second task covered 3.2 million documents with 367 thousand training queries; 15 groups submitted 75 runs. Forty-three judged queries is the reusable test set for a collection of 8.8 million passages.

Per query the judgments are thinner still. RocketQA's authors open with it: “Taking the MSMARCO dataset (Nguyen et al., 2016) as an example, each question has only 1.1 annotated positive passages on average, while there are 8.8M passages in the whole collection.” Before recording that your system missed the relevant document, check whether any relevant document was ever marked.

A search fix should name the failed stage and its likely side effects.

Key takeaways