Skip to content

[Feature]: Replace naive regex-based question search with MongoDB full-text index for relevance-ranked results #78

Description

@prince-pokharna

enhancement, feature, backend, performance, GSSoC 2026

Summary

StackIt's current search implementation (GET /api/questions with a keyword param) almost certainly uses $regex against question titles. This performs a full collection scan on every search — O(n) complexity — with no relevance ranking and no support for partial word matching or tag-based boosting. For a Q&A platform aspiring to be a professional Stack Overflow alternative, relevance-ranked full-text search is a baseline expectation.

Problem

  • $regex performs a full collection scan (no index used), degrading linearly as question count grows.
  • No relevance ranking — results are returned in insertion order, not by semantic match quality.
  • No stemming, stop-word filtering, or phrase-match support.
  • Search does not cover answer bodies or tags — only question titles.
  • No search analytics or autocomplete suggestions.

Impact

  • As the question count grows beyond a few thousand, search becomes unusably slow.
  • Poor relevance ordering means users cannot find the answers they need, undermining the platform's core value.

Proposed Solution

1. Add MongoDB text indexes on the questions collection:

// server/models/Question.js — add index
QuestionSchema.index(
  { title: 'text', body: 'text', tags: 'text' },
  {
    weights: { title: 10, tags: 5, body: 1 },
    name: 'question_full_text_index'
  }
);

2. Update the search controller to use $text with score-based sorting:

// server/routes/questions.js
if (keyword) {
  const results = await Question.find(
    { $text: { $search: keyword } },
    { score: { $meta: 'textScore' } }
  )
  .sort({ score: { $meta: 'textScore' } })
  .populate('author', 'username reputation')
  .lean();

  return res.json(results);
}

3. Add a migration script that builds the text index on the existing collection without downtime using createIndex with background: true.

4. Add tag-based autocomplete endpoint (GET /api/search/suggestions?q=<prefix>) for the search bar.

I am participating in GSSoC 2026. Could you please assign this issue to me?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions