End-to-End Generative Search
π Before You Continue: Read 8.1 first for semantic IDs and the Encoder-Decoder approach β this section transfers the same generative philosophy to the cross-modal matching of "text query β product results," but under sharper business constraints.
OneRec in 8.1 takes item IDs from a closed vocabulary as both input and output. E-commerce search is fundamentally different: users express intent with explicit text queries, and the system must return precise matches from a massive product catalog under hard relevance constraints. This "text query β product results" setting mixes an open vocabulary (arbitrary queries) with a closed vocabulary (a finite product catalog), plus multi-level tasks spanning query understanding, semantic matching, and personalized ranking.
Traditional e-commerce search is likewise an MCA: query understanding (correction/rewriting/intent) β retrieval (inverted index + vectors) β pre-ranking β fine-ranking. It suffers from three problems: query and product retrieval are decoupled, cold-start long-tail products, and keyword-stuffed title noise. OneSug and OneSearch propose end-to-end generative solutions for the front half (query completion) and back half (product retrieval) of the search pipeline respectively. They share a unified architectural philosophy but make different trade-offs in input/output spaces and ID design.
After reading this chapter, you will be able to:
- Explain how OneSug reformulates query completion as conditional text generation and uses the PRE module to augment short prefixes
- Describe how the RWR strategy injects business value into ranking via six-level interaction feedback
- Explain how OneSearch's KHQE balances semantic hierarchy against product uniqueness with "3 RQ layers + 2 OPQ layers"
- Recount Mu-Seq's three-perspective user modeling and PARS's preference-aware rewards
- Complete 5 tiered practice problems consolidating prefix augmentation, semantic ID encoding, and constrained decoding
8.2.0 Three Unique Challenges of E-commerce Search
Compared with video recommendation, e-commerce product retrieval faces more complex constraints:
- Strong relevance is the first priority. Recommendation can suggest items in a different category that match your style history; search cannot compromise β if a user searches "red dress," returning a "blue dress" is a severe relevance violation even if she often buys blue. The system must satisfy relevance first, then optimize personalization.
- Product information is full of noise and redundancy. Merchants stuff titles with keywords ("2024 New Korean-Style Slimming Long-Sleeve Dress Women Students Petite Sweet Temperament Skirt Versatile"), and traditional text encoders get drowned in the redundancy, unable to identify core attributes.
- Balancing semantic hierarchy and product uniqueness. The system must understand the category hierarchy (Clothing β Women's β Dresses β Korean-style dresses) for coarse-grained matching while preserving each product's distinctive attributes (style/brand/price) β otherwise all "Korean-style dresses" get mapped to the same representation.
π‘ Key Insight: The end-to-end difficulty of search is essentially a tightrope walk between "generation" and "hard constraints" β generation offers high freedom, but relevance is a bottom line that cannot be crossed. This is exactly why OneSearch amplifies the relevance weight 10Γ in its reward system.
8.2.1 OneSug: Generative Query Completion
Query completion is the first gate of search: a user types the prefix "red dr", and the system must generate complete query candidates in real time ("red dress", "red hoodie"). The traditional MCA uses a prefix tree (Trie) to coarsely retrieve from candidates down to , then pre-ranks to and fine-ranks 16 for display. It suffers from two problems: upstream performance bottlenecks cap downstream ceilings, and stage objectives conflict with each other.
OneSug reformulates query completion as an end-to-end conditional text generation task:
bypassing the traditional multi-stage pipeline. Its core challenges: semantic ambiguity of short prefixes ("appl" may mean fruit or phone), balancing personalization against popularity, fine-grained modeling of multi-level feedback, and a 100ms real-time constraint.
Encoder: Prefix Augmentation and Multi-Source Features
Prefix-query semantic alignment. For the raw text prefix , a pre-trained Text Encoder (BGE) extracts . But generic NLP models are biased in the e-commerce semantic space, so OneSug domain-aligns BGE with fine-tuning: mine high-quality prefix-query and query-query co-occurrence pairs from logs, and use contrastive learning to pull collaboratively related queries together:
After alignment, BGE's semantic relevance on the query retrieval task rose from 0.67 to 0.81.
Prefix representation enhancement (the PRE module). A short prefix yields an insufficient representation, so PRE retrieves from historical logs a set of high-quality queries co-occurring with it, and fuses the mean embedding with a weighted blend:
Ablation shows that at , MRR improves 2.3% over no augmentation, but introduces noise and degrades performance. For efficient retrieval, OneSug uses RQ-VAE to encode queries as hierarchical discrete codes (4 layers, codebook size 512 each); at inference it matches hierarchically from coarse to fine, reducing complexity from vector retrieval's ( being the full candidate count) to per-layer codebook lookup's β independent of the candidate scale , growing only linearly with the number of layers and the codebook size .
User features. OneSug integrates short-term historical queries (the most recent ; more introduces noise and drops MRR by 1.2%) and static profile . Note that OneSug does not include product interaction features β query completion happens at the input stage, before any product exposure. The encoder input is constructed as:
Decoder and the RWR Ranking Strategy
The decoder is a standard Causal Transformer that autoregressively generates subwords, trained by minimizing the NTP loss. Inference uses Beam Search (beam width ) with length normalization to avoid favoring short queries:
A generation model trained with NTP alone cannot distinguish candidates' business value. RWR (Reward-Weighted Ranking) converts six-level interaction feedback into fine-grained preference signals:
| Level | Feedback Type | Business Meaning | Base Weight |
|---|---|---|---|
| Level 1 | Order | Purchase completed through this query | 2.0 |
| Level 2 | Item Click | Clicked a product returned by this query | 1.5 |
| Level 3 | Click | Clicked this query | 1.0 |
| Level 4 | Show | Displayed but not clicked | 0.5 |
| Level 5 | Not Show | In the retrieval pool but not displayed | 0.2 |
| Level 6 | Rand | Random negative sample | 0.0 |
For each <prefix, query> pair, the reward is (where is the query's normalized frequency at the corresponding level), so high-frequency interaction queries receive higher rewards. From the 6 levels, 9 preference-pair types are constructed, with preference gap . Finally, reward weighting and a margin are introduced into the DPO loss:
Analysis: OneSug turns query completion from an MCA into end-to-end generation. PRE resolves short-prefix ambiguity, and the reward system built from six feedback levels precisely models preference gaps. The unified framework not only simplifies the architecture but also enables global optimization and avoids upstream bottlenecks. The cost is the extra inference overhead of Beam Search and RWR alignment, which must stay within 100ms.
8.2.2 OneSearch: Generative Product Retrieval
After a user hits enter on "red dress," the system must find the most relevant results among hundreds of millions of products within a second. OneSearch unifies "query β retrieval β pre-ranking β fine-ranking" into end-to-end sequence generation:
That is, it directly takes the query text and user behavior features as input and outputs an ordered product list. It designs four core modules: KHQE (Keyword-augmented Hierarchical Quantized Encoding), Mu-Seq (multi-perspective behavior sequence injection), a unified Encoder-Decoder generative architecture, and PARS (preference-aware reward system).
KHQE: Keyword-Augmented Hierarchical Quantized Encoding
The core question: how do you represent hundreds of millions of products in a generative framework? Atomic IDs have two fatal flaws: a vocabulary of makes Softmax infeasible; and atomic IDs are random numbers carrying no semantics.
OneSearch uses hierarchical semantic IDs: each product maps to a multi-layer discrete code sequence . For example, a Korean-style dress might encode as , with a vocabulary of about 6000 unique tokens β far smaller than hundreds of millions. The first 3 layers preserve semantic hierarchy; the last 2 preserve product uniqueness.
Product representation learning. Text, structured attributes, and statistical features pass through a distilled BGE to get initial embeddings , then multiple alignment tasks jointly capture semantics and collaboration: query-query / item-item contrastive, query-item contrastive, hierarchical feedback alignment (different margins assigned to exposure/click/order), and hard-sample relevance correction (an LLM scores boundary samples).
Core keyword augmentation. Marketing words in titles ("hot seller", "free shipping") dilute core attributes. OneSearch uses NER to build an 18-class attribute vocabulary and matches core words in titles quickly with an Aho-Corasick automaton ( multi-pattern matching), enhancing with 50%-50% weighting:
RQ-Kmeans semantic hierarchy encoding. Each layer extracts semantics and passes the residual to the next: L1 (codebook 4096) captures the coarsest categories (clothing/electronics/food), L2 (1024) captures subcategories (women's/men's), L3 (512) captures fine granularity (dresses/T-shirts). A key optimization: balanced K-means is applied only at L3 β forcing balance at earlier layers collapses the hierarchy and destroys semantic discrimination.
OPQ product-uniqueness encoding. After 3 RQ layers, the residual still holds unique attributes (style/brand/price). With only the first 3 layers, two "Korean-style dresses" (one Zara at 299 yuan, one unbranded at 99 yuan) would be treated as identical. So OPQ (Optimized Product Quantization) is introduced, splitting the residual into sub-vectors each quantized by K-means (codebook 256):
Why not use OPQ for all layers? Experiments showed it destroys hierarchical semantics and sharply degrades performance β losing the "coarse-to-fine" progressive generation pattern.
Mu-Seq: Multi-Perspective Behavior Sequence Injection
Behavior-sequence-driven user ID. Instead of a random hash ID, the User ID is constructed from behavior sequences: short-term clicks and long-term clicks are each weighted-summed (weights β more recent clicks weigh more, but not aggressively), rounded up, and concatenated (total length 10). Benefits: users with similar interests get similar IDs; cold-start users can use the platform's "query β top clicks" as a default sequence.
Explicit short-term sequence injection. Recent historical queries and clicked products are placed explicitly in the input: queries as raw text (short, tokenized directly), products as semantic IDs (titles are long; semantic IDs are more compact); with length limits (queries , clicks ).
Sliding-window data augmentation. A full sequence traditionally yields 1 sample; OneSearch uses a maximum window to generate several, letting the model learn interest evolution and naturally handle cold start.
Q-Former long-term sequence compression. Active users may have thousands to tens of thousands of long-term behaviors. These are aggregated by behavior type (click/order/RSU) into vectors, then learnable query vectors extract a fixed-length representation via cross-attention β no significant compute increase no matter how long the history.
Unified Encoder-Decoder Generative Architecture
OneSearch chooses BART (Encoder-Decoder, with a bidirectional encoder and autoregressive decoder, plus good pre-trained weights and industrial acceleration optimizations). The encoder takes a heterogeneous sequence (discrete tokens + continuous vectors) and outputs .
The decoder generates the target product's 5-layer semantic ID token by token, taking as an example:
Step 0: input [BOS] β predict L1 = 3856
Step 1: input [BOS, 3856] β predict L2 = 724
Step 2: input [BOS, 3856, 724] β predict L3 = 385
Step 3: input [BOS, ..., 385] β predict OPQ1 = 142
Step 4: input [BOS, ..., 142] β predict OPQ2 = 201
Each step passes through Causal Self-Attention and Cross-Attention, with a Softmax predicting the next token:
The training objective maximizes the log-likelihood of the ground-truth SID, . Inference uses Beam Search, either constrained (forcing each layer's token to come from the valid SID pool, guaranteeing a real product) or unconstrained.
PARS: Preference-Aware Reward System
A model trained with NTP alone only learns "which products co-occur with which queries," not "which ones users prefer." PARS comprises multi-stage supervised fine-tuning and an adaptive reward system.
Multi-stage SFT. Stage one: semantic content alignment (textβSID, textβcategory). Stage two: co-occurrence synchronization (queryβitem collaboration at both text and SID levels). Stage three: user personalization modeling (introducing the full user context).
Adaptive reward signals. User interactions fall into 6 levels (search order 2.0 / same-category recommendation order 1.5 / click 1.0 / exposure without click 0.5 / same-category not shown 0.2 / random 0.0). To avoid bias from low exposure of new products, CTR and CVR are computed with logarithmic smoothing, and the reward is the harmonic mean:
Reward model (three-tower SIM). The CTR tower / CVR tower / CTCVR tower predict separately, and the composite score is β the offline relevance score has its weight amplified 10Γ, ensuring relevance is satisfied before personalization is optimized.
Hybrid ranking framework. Built on the reward model, it performs List-wise DPO: sample 512 candidates, train on samples whose ranking changes, and combine DPO with the SFT objective so the model learns preference ordering while preserving generation ability. After launch, real interactions (Levels 1β3 positive, Levels 4β6 negative) feed near-real-time online learning.
Analysis: OneSearch elegantly balances semantic hierarchy and product uniqueness with KHQE's "3+2" semantic IDs; Mu-Seq's three-perspective modeling addresses both relevance and personalization; PARS embeds relevance as a hard constraint (Γ10) into the reward. The whole pipeline collapses from an MCA's many stages into a single generative model β at the cost of training-data engineering (alignment, sliding windows, multi-stage SFT) and latency control for Beam Search at inference.
β οΈ Common Mistakes in 8.2
| # | Mistake | Example | Why It's Wrong | Fix |
|---|---|---|---|---|
| 1 | Optimizing search like recommendation | "Suggest blue dresses for a red-dress query too" | Search's strong relevance is non-negotiable | Satisfy relevance first, then personalize (reward Γ10) |
| 2 | Ignoring prefix semantic ambiguity | OneSug directly encodes a 1-character prefix | Short prefixes carry no clear intent signal | Use the PRE module to retrieve co-occurring queries for augmentation |
| 3 | Using OPQ for all KHQE layers | "OPQ for all 5 layers is finer" | Destroys the coarseβfine semantic hierarchy | First 3 RQ layers keep hierarchy; last 2 OPQ layers keep uniqueness |
| 4 | Forcing balanced K-means at L1/L2 | "Balanced at every layer is more even" | Balance at early layers collapses hierarchical clustering | Apply the balance constraint only at L3 |
| 5 | Confusing SIDs with atomic IDs | "Just use item_123 as the vocabulary" | A vocabulary of hundreds of millions makes Softmax explode | Hierarchical semantic IDs compress to about 6000 tokens |
Chapter Summary
π Key Takeaways
| Concept | Key Points | Why It Matters |
|---|---|---|
| OneSug | Conditional text generation + PRE prefix augmentation + RWR six-level feedback | Query completion goes from MCA to end-to-end generation |
| KHQE | 3 RQ-Kmeans layers (semantics) + 2 OPQ layers (uniqueness) | Compresses hundreds of millions of products into controllable semantic IDs |
| Mu-Seq | Behavior-sequence UserID + explicit short-term + Q-Former long-term compression | Personalization under relevance-first constraints |
| PARS | Multi-stage SFT + adaptive rewards + relevance Γ10 | Protect relevance first, then optimize preferences |
| Beam Search | Constrained vs. unconstrained; SID pool filters illegal outputs | Generates real products and controls latency |
β FAQ
Q1: Why doesn't OneSug include product interaction features?
A: Query completion happens at the input stage, when there is no product exposure behavior yet. Product features would have no data support and would pollute the prefix representation with irrelevant signals. It uses only the prefix, historical queries, and static profile.
Q2: Why do KHQE's first 3 layers use RQ-Kmeans and the last 2 use OPQ, rather than all RQ?
A: The first 3 layers express the progressive category hierarchy "clothing β women's β dresses," a natural fit for RQ's residual passing; the last 2 layers encode unique attributes in the residual, better served by OPQ's independent sub-vector quantization. All OPQ would lose hierarchical semantics.
Q3: Does PARS's 10Γ relevance weight hurt personalization?
A: It actually protects personalization β it first guarantees "no irrelevant products returned," then optimizes personalization with CTR/CVR within the relevant set. This avoids the "relevance drift" common in recommender systems.
π Connections to Later Chapters
- 8.1 (end-to-end generative recommendation) provides the semantic ID and Enc-Dec foundations for this section; OneSug/OneSearch extend them cross-modally.
- 8.3 (end-to-end generative advertising) further stacks auction mechanisms and economic constraints onto generation.
- 2.3 (two-tower) covers vector retrieval, which OneSearch replaces with the "generative retrieval" of semantic IDs + Beam Search.
- 6.x (generative foundations) covers RQ-VAE quantization, which appears here in two forms: RQ-Kmeans (OneSearch) / RQ-VAE (OneSug).
Practice Problems
Work through all problems in order β they get progressively harder. Each has a complete solution you can reveal after trying it yourself.
Problem 8.2.1 β PRE Augmentation Computation π’ Easy
A prefix embedding is , and the mean embedding of related queries is . With PRE weight , compute the augmented . What trend appears if ?
π‘ Solution (click to reveal)
Approach: Weighted average.
If : β the prefix's own signal is heavily diluted, over-relying on co-occurring queries. This is exactly why introduces noise and degrades performance.
Key points:
- is the empirically optimal balance point.
- Too large a makes the prefix "become someone else," losing the user's actual input signal.
Problem 8.2.2 β KHQE Encoding Space π’ Easy
A product's KHQE SID is , with codebook sizes 4096 / 1024 / 512 / 256 / 256 per layer. Questions: (a) About how many unique vocabulary tokens are there in total? (b) How does this encoding embody both "semantic hierarchy" and "product uniqueness"?
π‘ Solution (click to reveal)
Approach: The number of unique vocabulary tokens is the sum of the per-layer codebook sizes (codes in different layers are numbered independently).
- (a) unique tokens.
- (b) The first 3 RQ-Kmeans layers: L1=2341 (clothing), L2=567 (women's - dresses), L3=89 (dress - Korean style) embody the coarse-to-fine category hierarchy; the last 2 OPQ layers encode unique attributes in the residual (style/brand/price), so two identical "Korean-style dresses" can still be distinguished.
Key points:
- The total vocabulary is far smaller than hundreds of millions of atomic IDs, making Softmax feasible.
- "Hierarchy + uniqueness" is the core tension that KHQE's design balances.
Problem 8.2.3 β Constructing a Behavior-Sequence User ID π‘ Medium
A user's short-term clicked products have semantic IDs (earliest to latest), with weights . Compute the normalized weights (to 3 decimal places), and explain why rather than linear is used.
π‘ Solution (click to reveal)
Approach: Compute then normalize.
Sum
More recent behaviors get higher weights (0.218 < 0.329 < 0.453). Why instead of linear : linear decay (e.g., ) would let the most recent behavior dominate explosively and drive early behaviors to nearly zero; is "gentle growth" β reflecting recency while retaining earlier behaviors' contribution, avoiding overly aggressive forgetting of long-term interests.
Key points:
- Weights reflect recency without being aggressive.
- This is "soft decay" balancing short-term intent against long-term preference.
Problem 8.2.4 β Illegal-Output Filtering in Constrained Beam Search π΄ Hard
OneSearch decodes a 5-layer SID, and constrained search requires each layer's token to come from the valid SID pool (the set of real products). Suppose layer 1 has 4096 candidate tokens in total, of which the valid SID pool covers only 2000; the beam width is . Compare "constrained search" versus "unconstrained search" on (a) output legality and (b) per-step candidate count, and explain why constrained search reduces latency.
π‘ Solution (click to reveal)
Approach: Analyze the search space and post-processing.
- (a) Constrained search: each layer decodes only within the valid pool (2000 at layer 1, narrowing layer by layer), so generated sequences necessarily map to real products β no "hallucinated SIDs." Unconstrained search: allows arbitrary token combinations, may generate illegal SIDs mapping to no real product, and requires post-filtering.
- (b) Per-step candidates: constrained search has at most 2000 candidates at step 1 (smaller in later layers as the tree narrows); unconstrained search has a fixed 4096 per step. The constrained search space is , far smaller than .
- Latency: constrained search prunes illegal branches early in decoding, instead of generating large numbers of invalid candidates and filtering afterward; combined with a Trie prefix tree (see GPR in 8.3), the search space shrinks from to the number of valid products, significantly cutting per-step computation.
Key points:
- Constrained search = turning "legality" into a hard mask at decoding time.
- This is the key engineering trick for deploying generative retrieval.
π Challenge: Arguing the Case for End-to-End Search
An e-commerce search MCA often returns blue dresses for the query "red dress" (relevance drift). Write roughly 160 words explaining, when introducing a OneSearch-style end-to-end generative architecture: (1) which stage should be replaced first; (2) which two designs in KHQE and PARS directly mitigate this problem; (3) what new risks to watch for?
π‘ Hint
(1) Prioritize replacing "query understanding + retrieval + fine-ranking" with unified Enc-Dec generation, eliminating intent loss between stages. (2) KHQE's hierarchical semantic IDs distinguish "red dress" from "blue dress" as early as L3; PARS amplifies the offline relevance score weight 10Γ, forcing relevance to be satisfied first. (3) New risks: Beam Search latency, complex training-data engineering (multi-stage SFT, sliding windows), and difficulty localizing bad cases due to the opacity of generative retrieval.