petesellsmihouses.com

Maximizing Search Results with Sorting Techniques in MongoDB

Written on

1. Sorting by Relevance

When conducting a full-text search, each document comes with associated metadata, which includes a relevance score known as textScore. By default, results are not sorted based on this score. To display this score, run the following command, searching for the term "world" while projecting the textScore in a new field called score:

db.movies.find({ $text: { $search: "world" } }, { _id: 0, title: 1, score: { $meta: "textScore" } })

The results will include titles and their corresponding scores:

{ "title": "Doctor Strange", "score": 0.5294 }

{ "title": "Avatar", "score": 0.5333 }

{ "title": "Vikings", "score": 0.5278 }

Notice that these results are not sorted by score. To sort them, use the following aggregation pipeline:

db.movies.aggregate([

{ $match: { $text: { $search: "world" } } },

{ $project: { title: 1, score: { $meta: "textScore" } } },

{ $sort: { score: -1 } }

])

This will return the results sorted by relevance, with the most relevant titles appearing first. To sort in ascending order, simply adjust the sort order to:

{ $sort: { score: 1 } }

2. Sorting Alphabetically

To sort results alphabetically, you can simply sort by the indexed fields. For an ascending order sort based on the title from the previous search, execute:

db.movies.find({ $text: { $search: "world" } }).sort({ title: 1 })

For a descending order sort, use:

db.movies.find({ $text: { $search: "world" } }).sort({ title: -1 })

3. Custom Score Influencing Results

Our movie collection includes a rating field. If you wish to use this field to influence results by combining relevance and rating, you can multiply the relevance score with the rating:

db.movies.aggregate([

{ $match: { $text: { $search: "world" } } },

{ $addFields: { customScore: { $multiply: ["$score", "$rating"] } } },

{ $sort: { customScore: -1 } }

])

This will allow results to be sorted based on a custom score that reflects both relevance and rating.

4. Prioritizing Sponsored Results

If certain results are sponsored and should be prioritized, you can increment their scores by a value higher than others. For instance, if your sponsored list includes ["The Avengers", "Vikings", "300"], you can run:

db.movies.aggregate([

{

$addFields: {

customScore: {

$cond: [{ $in: ["$title", ["The Avengers", "Vikings", "300"]] }, { $add: ["$score", 100] }, "$score"]

}

}

},

{ $sort: { customScore: -1 } }

])

This ensures that sponsored results are displayed first.

5. Sorting Based on Search Term Location

For scenarios where you want results sorted based on the location of the search term, you can leverage the index of the search term within the title. For example, if searching for "Computer" in titles like:

  • Introduction to Computer Science
  • Computer Science
  • Advanced Computer Science

You can use:

db.movies.aggregate([

{ $addFields: { indexValue: { $indexOfBytes: ["$title", "Computer"] } } },

{ $sort: { indexValue: 1 } }

])

This method will sort results based on the position of the search term, promoting more relevant titles to the top.

In conclusion, these techniques can significantly enhance your ability to sort and retrieve results efficiently from your MongoDB database. Thanks for taking the time to read this guide!