How to Run Compound Queries with Weighted Fields
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:
- Set up an Atlas Search index with dynamic mapping for the
sample_mflix.movies
collection. - Run Atlas Search queries against the
year
field in thesample_mflix.movies
collection and alter the score using specific a word in thetitle
field.
Before you begin, ensure that your Atlas cluster meets the requirements described in the Prerequisites.
Create the Atlas Search Index With Dynamic Mapping
In this section, you will create an Atlas Search index that uses dynamic
mapping to index the fields in the sample_mflix.movies
collection.
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
.NoteIf 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.
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.
Visual Editor
- Click Next.
- Review the
"default"
index definition for themovies
collection.
JSON Editor
- Click Next.
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.- Click Next.
Run a Compound Query
➤ 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.