Highlights

Contents

Part II
02

Tokenization and analysis

We turn a document into the pieces the index can compare. An inconsistent transformation makes relevant documents disappear before ranking begins. We will trace tokenization, normalization, positions, and fields with the same tiny corpus.

CHAPTER ATLAS / FOLLOW THE READING PATHCHAPTER ATLAS / FOLLOW THE READING PATH1Search basics: why LIKEfails2Tokenization and analysis3The inverted index4Ranking: TF-IDF and BM255The indexing pipeline6Shards and replicas7Autocomplete and fuzzysearch8Operating a searchcluster9Case study: search end toend

#Section 5Tokenization preserves a decision

A report title contains Red bird scores. We need to decide whether Red, red, and punctuation around the word refer to the same indexed term. Tokenization splits input text into candidate word-like pieces, often retaining offsets and positions. The result is not a universal linguistic truth. It is a deliberate representation for a family of queries.

Our illustrative tokenizer splits on spaces. The first title becomes Red, bird, scores at positions 1, 2, and 3. A subsequent lowercase transformation gives red, bird, scores. Real tokenizers handle punctuation, scripts, compound words, and languages without spaces. We use the tiny rule because every state fits on the page, not because it is a production recommendation.

BEFORE AND AFTER ANALYSIScharactersRedbirdscoressplitsplittokenslowercaseRedposition 1redposition 1birdposition 2birdposition 2scoresposition 3scoresposition 3Red and red become one comparable term
Figure 15.5Illustrative whitespace tokenizer and lowercase transformation. Positions remain 1, 2, and 3.

An analyzer is the ordered pipeline that transforms text into indexed or queried tokens. The order matters. A transformation performed before tokenization can change boundaries, while a token filter changes already selected tokens. Keep analyzer versions with the index so the service can explain which transformation produced a missing result.

#Section 6Normalize without destroying meaning

The words résumé and resume illustrate the tension. Accent folding can make a user without an accent key find the same documents, but it may collapse distinct words in some languages. Normalization maps surface forms into a chosen comparison form. Lowercasing, Unicode normalization, and accent folding solve different representation problems.

An implementation applies the transform deterministically at indexing time and, where appropriate, at query time. It may store both an original field and a normalized field. The original remains available for display and exact identity. The normalized field supports forgiving lookup. Treat these as separate purposes rather than overwriting the only copy.

Stemming reduces related word forms with rules, while lemmatization maps a word to a dictionary form using more linguistic knowledge. A stem need not be a readable word. These techniques can improve recall, but collapsing unrelated forms damages precision. A product search over model identifiers may need exact tokens even when a report body benefits from word normalization.

TWO FIELDS, TWO JOBSsource titleRed Birdkeep bothtitle, originalRed Birddisplay and exact valuetitle, analyzedredbirdterm comparisonquery RED becomes red, matchesscreen still shows Red Bird
Figure 15.6Illustrative dual representation. The display value is preserved while a separate field supplies searchable terms.

The cost is more indexed data and more schema decisions. Test representative language examples, including names and identifiers, before adopting a transformation. A query that starts returning more matches has not necessarily become more useful.

#Section 7Positions make phrases possible

Both red bird and bird red contain the same distinct terms. A term-only representation cannot tell them apart. A position records where a token occurs in a document’s analyzed sequence. A phrase query looks for terms whose positions satisfy an order-and-distance rule.

Document 1 stores red at position 1 and bird at position 2. The exact phrase succeeds because the second position is the first plus 1. Document 4 stores bird at positions 1 and 2 but has no red, so it fails before position checking. A document saying red fast bird would satisfy both terms but fail an exact adjacent phrase.

WORD MEMBERSHIP IS NOT WORD ORDERdoc 1red bird scores1red2bird3scoresdoc 4bird bird match1bird2bird3matchwindow: red at p, bird at p + 1no red, so positions are never checkedphrase rulered at pbird at p + 1doc 1: p = 1, matchdoc 4: no red, skipsame terms, order kept by positions
Figure 15.7Computed token positions in the illustrative corpus. The final row is the adjacency condition.

A phrase check usually runs only after term membership has narrowed candidates. It then compares the stored positions inside each remaining document. This ordering avoids reading positional details for documents already known to lack a required term. If a token filter creates multiple terms at one position, phrase interpretation also needs that graph relationship rather than a flat list of offsets.

Phrase slop permits a bounded departure from the strict phrase position pattern, with exact semantics depending on the engine. Do not invent an implementation-independent count rule for transpositions. Explain the product requirement first, then consult the engine’s phrase semantics. Position storage costs bytes per occurrence, so an index serving only term membership can omit data that a phrase index needs.

#Section 8Fields stop unrelated terms combining

A report has a title, a body, a sport, and an owner. A query for red bird could accidentally combine red from the title with bird from the owner’s name if every field becomes one undifferentiated bag. A field is a named document component with its own representation and query rules.

The engine can index the title as analyzed text, sport as an exact category, creation time as an ordered value, and owner as an authorization identifier. A title match may receive more weight than a body match. An exact category filter should not stem or lowercase identifiers unless the schema explicitly defines that equality rule.

ONE DOCUMENT, SEPARATE REPRESENTATIONSreport 1titleRed bird scoressportfootballowneru-17text: analyzed and scoredredbirdscorestitle match can weigh morekeyword: exact, filter onlyfootballno lowercase, no stemmingidentifier: authorizationu-17filters by viewer, never scoredeach field keeps its own rules
Figure 15.8Illustrative mapping. Each field has a query purpose and an independent representation.

A mapping declares field types and analysis behavior. Changing a field’s analysis after documents have been indexed does not rewrite their existing terms. Plan a new index and reindex workflow for semantic representation changes. Uncontrolled dynamic fields can also create a growing schema, so validate incoming document structures instead of allowing arbitrary keys to become permanent index fields.