The search process involves several steps executed via a MongoDB aggregation pipeline:
-
Filtering (
$match):- Applies filters based on user input (price range, RAM, storage, brand, rating, stock status, name, reviews, text search terms, popularity).
- Includes a specific recommendation logic: if only a maximum price is set, it narrows the search to a ₹5000 range below that maximum.
-
Weighted Scoring (
$addFields):- Calculates a
scorefor each phone to determine its relevance beyond simple filtering. - Different features contribute differently to the score (weights):
bought: 4xreviews: 3xratingFloat: 2xram: 1xstorage: 1x
- Missing values (
null) for these fields are treated as 0 during score calculation ($ifNull).
- Calculates a
-
Initial Sorting (
$sort):- Phones are sorted primarily by the calculated
scorein descending order (highest score first). price(ascending, lowest first) is used as a secondary sort key to break ties.
- Phones are sorted primarily by the calculated
-
Brand Diversity Enforcement:
- Group by Brand (
$group): Phones are grouped by theirbrand. - Limit per Brand (
$project&$slice): Each brand group is limited to its top 4 phones (based on the initial sort). - Recombine (
$unwind&$replaceRoot): The limited groups are merged back into a single list, ensuring no brand has more than 4 entries.
- Group by Brand (
-
Final Sorting (
$sort):- The diversified list is sorted again by
score(descending) andprice(ascending).
- The diversified list is sorted again by
-
Result Limiting (
$limit):- The final list is limited to the top 8 phones.
This multi-stage process aims to return the most relevant phones based on user criteria and popularity/ratings, while also ensuring a variety of brands are presented in the results.