DEV Community

Cover image for Understanding MongoDB Atlas Search Scoring for Better Search Results
Shannon Lal
Shannon Lal

Posted on

1

Understanding MongoDB Atlas Search Scoring for Better Search Results

Recently, while implementing hybrid search functionality in our application, I encountered a challenge in improving search result relevancy. While vector search and text search provided good results, we needed finer control over how results were ranked. MongoDB Atlas Search provides powerful features - boost and bury - that allow developers to fine-tune search rankings.
In this post, I'll give a quick overview of how you can implement score optimization in Mongo to help improve your search results.

If you're working with hybrid search in MongoDB, you might be interested in my previous posts about implementing semantic search (https://dev.to/shannonlal/building-blocks-for-hybrid-search-combining-keyword-and-semantic-search-236k) and hybrid search with filtering (https://dev.to/shannonlal/navigating-hybrid-search-with-mongodb-ugly-approach-1p9g).

Let's explore how we can leverage MongoDB's scoring features to create more relevant search results for your users.

Understanding Score Components
MongoDB Atlas Search uses the BM25 algorithm for scoring, which considers:

  • Term frequency in the document
  • Inverse document frequency (term rarity across all documents)
  • Field length normalization

Implementing Score Modification
Here's an example of how to implement boost and examine score details:

db.collection.aggregate([
  {
    $search: {
      index: "my_index",
      compound: {
        should: [{
          text: {
            query: "ocean",
            path: "description",
            score: { boost: { value: 3 } }
          }
        }],
      },
      scoreDetails: true
    }
  },
  {
    $project: {
      description: 1,
      score: { $meta: "searchScore" },
      scoreDetails: { $meta: "searchScoreDetails" }
    }
  }
])
Enter fullscreen mode Exit fullscreen mode

Understanding Score Details
The scoreDetails provide a breakdown of how the final score was calculated:

{
  value: 14.02,
  description: "sum of:",
  details: [
    {
      value: 8.92,
      description: "weight(description:ocean) [BM25Similarity]",
      details: [
        {
          value: 3,
          description: "boost"
        }
        // Additional scoring factors
      ]
    }
  ]
}
Enter fullscreen mode Exit fullscreen mode

Optimizing Search Rankings

{
  compound: {
    should: [
      {
        text: {
          query: "search_term",
          path: "title",
          score: { boost: { value: 3 } }
        }
      },
      {
        text: {
          query: "search_term",
          path: "description",
          score: { boost: { value: 1 } }
        }
      }
    ]
  }
}
Enter fullscreen mode Exit fullscreen mode

Understanding and implementing proper score modification in MongoDB Atlas Search can significantly improve search relevance for your users. As you implement these techniques, remember to:

  • Start with score analysis using scoreDetails
  • Test with real-world queries
  • Monitor user interaction with search results
  • Iterate based on user feedback

With these tools and techniques, you can create more precise and relevant search experiences in your MongoDB-based applications. Remember that scoring optimization is an iterative process - start simple, measure impact, and refine based on your specific use case.

API Trace View

How I Cut 22.3 Seconds Off an API Call with Sentry

Struggling with slow API calls? Dan Mindru walks through how he used Sentry's new Trace View feature to shave off 22.3 seconds from an API call.

Get a practical walkthrough of how to identify bottlenecks, split tasks into multiple parallel tasks, identify slow AI model calls, and more.

Read more →

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more