How to Sort Your Atlas Search Results for Your Perfomance Needs
On this page
Important
The procedures in this tutorial might not be sufficient to optimize search query performance for all use cases. To provide feedback on your use case, use the MongoDB Feedback Engine.
This tutorial describes the various ways to sort your Atlas Search results based on your performance needs. You can sort for speed, precision, or for both.
First, this tutorial creates an index with static mappings for the title
, year
and released
fields in the sample_mflix.movies
collection on mongot
. It also
uses the storedSource option for
the title
field only. Storing fields on mongot
allows you
to perform interim operations such as sort on documents with a minimum
number of fields and avoid full document lookup in the interim
stages. Then, the tutorial shows how to sort the query results using
the field stored on mongot
.
Before you begin, ensure that your Atlas cluster meets the requirements described in the Prerequisites.
Create the Atlas Search Index
In this section, you will create an Atlas Search index on the released
,
title
, and year
fields in the sample_mflix.movies
collection and configure the storesdSource
option for the title
field to store that field on mongot
.
Procedure
Navigate to the Atlas Search page for your project.
If it is not already displayed, select the organization that contains your desired project from the Organizations menu in the navigation bar.
If it is not already displayed, select your desired project from the Projects menu in the navigation bar.
Click your cluster's name.
Click the Search tab.
Enter the Index Name, and set the Database and Collection.
In the Index Name field, enter
default
.Note
If you name your index
default
, you don't need to specify anindex
parameter when using the $search pipeline stage. Otherwise, you must specify the index name using theindex
parameter.In the Database and Collection section, find the
sample_mflix
database, and select themovies
collection.
Specify an index definition.
Use the Visual Editor or the JSON Editor in the Atlas user interface to create an index definition that:
Indexes the
released
,title
, andyear
fields as date, string, and number field types respectively.Analyzes the title field using the
lucene.keyword
analyzer.Stores the
title
field onmongot
.
Sort Your Search Results
➤ Use the Select your language drop-down menu to set the language of the example in this section.
You can sort your search results in multiple ways. The queries in this section demonstrate how to sort your search results using the following:
returnStoredSource option for retrieving the field stored on
mongot
score options for altering the scores of matching results
Sort Numbers for Speed
Sort Strings for Precision
Note
This method is highly resource intensive and not recommended in production.