Docs Menu

Docs HomeMongoDB Atlas

How to Sort Your Atlas Search Results for Your Perfomance Needs

On this page

  • Create the Atlas Search Index
  • Procedure
  • Sort Your Search Results
  • Sort Numbers for Speed
  • Sort Dates for Speed
  • Sort Strings for Precision
  • Sort Strings for Speed and Precision

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.

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.

1
  1. If it is not already displayed, select the organization that contains your desired project from the Organizations menu in the navigation bar.

  2. If it is not already displayed, select your desired project from the Projects menu in the navigation bar.

  3. Click your cluster's name.

  4. Click the Search tab.

2
3
  • For a guided experience, select Visual Editor.

  • To edit the raw index definition, select JSON Editor.

4
  1. In the Index Name field, enter default.

    Note

    If you name your index default, you don't need to specify an index parameter when using the $search pipeline stage. Otherwise, you must specify the index name using the index parameter.

  2. In the Database and Collection section, find the sample_mflix database, and select the movies collection.

5

Use the Visual Editor or the JSON Editor in the Atlas user interface to create an index definition that:

  • Indexes the released and year fields as date and number field types respectively.

  • Indexes the title field as string and autocomplete types.

  • Analyzes the title field using the lucene.keyword analyzer for the string type.

  • Stores the title field on mongot.

6
7

A modal window appears to let you know your index is building. Click the Close button.

8

The index should take about one minute to build. While it is building, the Status column reads Build in Progress. When it is finished building, the Status column reads Active.


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:

Note

This method is highly resource intensive and not recommended in production.

←  How to Define a Custom Analyzer and Run an Atlas Search Diacritic-Insensitive QueryHow to Run Atlas Search String Queries Against Date and Numeric Fields →
Share Feedback
© 2023 MongoDB, Inc.

About

  • Careers
  • Investor Relations
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2023 MongoDB, Inc.