Posts

Showing posts with the label tag20

prioritize search relevance with scoring strategies in fusejs

Understanding the Importance of Relevance in Search Results

In a knowledge base, not all content is equally relevant to a user's query. By customizing the scoring logic of your client-side search with Fuse.js, you can promote more helpful content to appear first—improving user satisfaction and efficiency, especially in multilingual setups hosted with Jekyll on GitHub Pages.

How Fuse.js Calculates Search Scores

Fuse.js returns search results with scores indicating how closely each item matches the query. Lower scores mean higher relevance. Each key (like title, description, or tags) can be weighted differently to influence results.

const options = {
  keys: [
    { name: "title", weight: 0.5 },
    { name: "description", weight: 0.3 },
    { name: "tags", weight: 0.2 }
  ],
  threshold: 0.4,
  includeScore: true,
  ignoreLocation: true
};

This config prioritizes matches in the title over description and tags. You can tune these weights depending on how your users interact with your site.

Step 1: Identify Key Fields for Boosting

  • title — often the most direct match for a query.
  • description — offers context and summary.
  • tags — useful for semantic categories and topics.
  • lang — for multilingual priority sorting (e.g., showing preferred language first).

Step 2: Boost Specific Content Programmatically

Let’s say you want to promote articles marked as featured: true in the front matter:

const indexedData = originalData.map(item => {
  const boost = item.featured ? -0.2 : 0;
  return {
    ...item,
    customScore: boost
  };
});

Then in your results sorting logic, apply this adjustment manually after Fuse.js returns matches:

results.sort((a, b) => {
  const scoreA = a.score + (a.item.customScore || 0);
  const scoreB = b.score + (b.item.customScore || 0);
  return scoreA - scoreB;
});

Step 3: Support Language Preference in Ranking

For multilingual knowledge bases, sort by user-preferred language first:

const userLang = navigator.language.slice(0, 2);

results.sort((a, b) => {
  const langBoostA = a.item.lang === userLang ? -0.1 : 0;
  const langBoostB = b.item.lang === userLang ? -0.1 : 0;
  return (a.score + langBoostA) - (b.score + langBoostB);
});

This ensures that even when scores are tied, users see results in their own language first.

Step 4: Leverage Exact Match Boosts

You can use exact matches to push relevant pages to the top:

results.forEach(result => {
  if (result.item.title.toLowerCase() === query.toLowerCase()) {
    result.score -= 0.3;
  }
});

This simple condition checks if the query matches the title exactly and reduces the score significantly to increase its rank.

Fine-Tuning Threshold and Location Sensitivity

The threshold setting controls how "fuzzy" the match is. Lower values mean stricter matches. If your queries tend to be precise (like documentation search), keep the threshold low (e.g., 0.3). Set ignoreLocation: true to allow matches anywhere in the string.

Visual Feedback for Relevance

Optional but helpful: display score values or ranking badges for debugging and UX tuning:

<div>
  <a href="{{ result.item.url }}">{{ result.item.title }}</a>
  <span class="score">Relevance: {{ result.score.toFixed(2) }}</span>
</div>

Case Example: Highlighting Internal Guides

Suppose you maintain internal vs external resources. You can label internal pages and apply a boost to help internal support staff find internal documentation first:

const indexedData = originalData.map(item => {
  const internalBoost = item.internal ? -0.15 : 0;
  return {
    ...item,
    customScore: internalBoost
  };
});

Conclusion

By customizing Fuse.js scoring with field weights and post-processing boosts, you can guide your search interface to serve the most relevant results. This fine-tuning is especially valuable in multilingual Jekyll knowledge bases where user needs vary widely.

In the next article, we’ll explore how to integrate search analytics and behavior tracking into your GitHub Pages-hosted Jekyll site to continuously improve your knowledge base performance.