Recently, I've been continuing my exploration of MongoDB Atlas Search with a goal of understanding how to improve hybrid search in Mongo. My previous blog was on how boost and bury features of Mongo Search Sore could improve results, I wanted to explore two additional scoring techniques: constant and function scoring.
These features offer powerful ways to fine-tune search result rankings.
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 score optimization with boost and bury (https://dev.to/shannonlal/understanding-mongodb-atlas-search-scoring-for-better-search-results).
Let's explore how we can leverage these MongoDB scoring features to create even more relevant search results for your users.
Understanding Score Modifiers
MongoDB Atlas Search provides two powerful scoring modifications:
Constant Scoring: Replaces the base score with a fixed value
Function Scoring: Enables mathematical operations on scores for complex scoring logic
Implementing Score Modification
In the aggregation below, we're combining two different scoring approaches to achieve better search relevance. The function scoring in the first clause multiplies the base relevance score by 4 when matching "search_term" in descriptions, which helps maintain relative relevance while giving these matches more weight. Meanwhile, the constant scoring in the second clause assigns a fixed score of 1 for category matches, ensuring a consistent contribution to the final score regardless of how well it matches. This combination allows us to prioritize description matches while maintaining predictable scoring for category filtering, giving us precise control over how different search criteria influence the final results.
db.collection.aggregate([
{
$search: {
index: "my_index",
compound: {
must: [
{
text: {
query: "search_term",
path: "description",
score: {
function: {
multiply: [
{ constant: 4 },
{ score: "relevance" }
]
}
}
}
},
{
text: {
path: "category",
query: "specific_category",
score: { constant: { value: 1 } }
}
}
]
},
scoreDetails: true
}
},
{
$project: {
description: 1,
category: 1,
score: { $meta: "searchScore" },
scoreDetails: { $meta: "searchScoreDetails" }
}
}
])
Understanding Score Details
The scoreDetails show how constant and function scoring affect the final score:
{
value: 16.08,
description: "sum of:",
details: [
{
value: 15.08,
description: "function(multiply([constant(4), relevance]))",
details: [
{
value: 4,
description: "constant"
},
{
value: 3.77,
description: "relevance score"
}
]
},
{
value: 1,
description: "constant score for category match"
}
]
}
Optimizing Search Rankings
When implementing these scoring techniques, remember to:
- Use constant scoring when you need consistent scores for specific criteria
- Apply function scoring when combining multiple scoring factors
- Start with simple modifications and iterate
- Always test with real-world queries
- Monitor how users interact with the search results
With these additional scoring techniques, you can create even 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.
Top comments (0)