Docs Menu

Docs HomeMongoDB Atlas

How to Run Atlas Search Compound Queries with Weighted Fields

On this page

  • Create the Atlas Search Index With Dynamic Mapping
  • Run a Compound Query
  • Continue Learning

This tutorial describes how to create an index with dynamic mapping on the sample_mflix.movies collection. It shows how to run compound queries against the year field and alter the score using constant, function, and boost. It takes you through the following steps:

  1. Set up an Atlas Search index with dynamic mapping for the sample_mflix.movies collection.

  2. Run Atlas Search queries against the year field in the sample_mflix.movies collection and alter the score using specific a word in the title field.

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 that uses dynamic mapping to index the fields in the sample_mflix.movies collection.

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

You can create an Atlas Search index that uses dynamic mappings through the Visual Editor or JSON Editor in the Atlas User Interface. The following index definition dynamically indexes the fields of supported types in the movies collection.

  1. Click Next.

  2. Review the "default" index definition for the movies collection.

  1. Click Next.

  2. Review the index definition.

    Your index definition should look similar to the following:

    {
    "mappings": {
    "dynamic": true
    }
    }

    The above index definition dynamically indexes the fields of supported types in each document in the movies collection.

  3. Click Next.

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 examples in this section.


You can use the compound operator to combine two or more operators into a single query. Every document that Atlas Search returns for your query is assigned a score based on relevance, in order from highest score to lowest. In this section, you will connect to your Atlas cluster and run the sample queries using the compound operator against the title and year fields in the sample_mflix.movies collection. The sample queries use custom scoring to alter the relevance score returned by Atlas Search.

To learn more about compound queries using Atlas Search, take Unit 9 of the Intro To MongoDB Course on MongoDB University. The 1.5 hour unit includes an overview of Atlas Search and lessons on creating Atlas Search indexes, running $search queries using compound operators, and grouping results using facet.

←  How to Use Autocomplete with Atlas SearchHow to Run Atlas Search Queries Against Objects in Arrays →
Share Feedback
© 2023 MongoDB, Inc.

About

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