How to Use Synonyms with Atlas Search
On this page
This tutorial describes how to add a collection that configures words
as synonyms, create an index that defines synonym mappings on the
sample_mflix.movies
collection, and run Atlas Search queries against the
title
field using words that are configured as synonyms. It takes
you through the following steps:
Load one or more sample synonyms source collections in the
sample_mflix
database.Set up an Atlas Search index with one or more synonym mappings for the
sample_mflix.movies
collection.Run Atlas Search queries against the
title
field in thesample_mflix.movies
collection for words configured as synonyms in the synonyms source collection.
Before you begin, ensure that your Atlas cluster meets the requirements described in the Prerequisites.
Note
To create multiple synonym mappings and run the advanced sample
query in this tutorial, you will need an M10
or higher
cluster.
Load the Sample Synonyms Source Collection
Each document in the synonyms source collection describe how one or more words map to one or more synonyms of those words. To learn more about the fields and word mapping types in the synonyms source collection documents, see Format of Synonyms Source Collection Documents.
You must begin by creating the synonyms source collection and then add
the collection to the database where you intend to use the synonyms
source collection. In this section, you will create one or two sample
synonyms source collections in the sample_mflix
database, then use
the synonyms source collections with an index of the movies
collection in the same database.
Navigate to the Database Deployments 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.
If the Database Deployments page is not already displayed, click Databases in the sidebar.
Create one or more sample synonyms collections in the sample_mflix
database.
If you are running a free or shared tier cluster, follow the
steps in the Transportation Synonyms tab to create the collection
for a single synonym mapping definition in your index. If you have a
M10
or higher cluster and wish to create multiple synonym
mappings in your index, follow the steps in both the tabs to create
both the Transportation Synonyms and Attire Synonyms collections.
Create the Atlas Search Index With Synonym Mapping Definition
The synonym mapping in a collection's index specifies the synonyms source collection and the analyzer to use with the collection.
In this section, you will create an Atlas Search index that defines one
synonym mapping or multiple synonym mappings for the
sample_mflix.movies
collection. The mapping definition in the
index references the synonyms source collection that you created in the
sample_mflix
database.
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.
Select the JSON Editor Configuration Method and click Next.
The Visual Editor Configuration Method doesn't support synonym mapping definition.
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.
Replace the default index definition with the following index definition and then click Next.
To run the simple example query only, use the index definition in
the Single Synonym Mapping tab below. If you have an M10
or
higher cluster and if you loaded both the example synonyms source
collections, you can run both the simple and advanced example queries
using the index definition that specifies multiple synonym mappings
in the Multiple Synonym Mappings tab below.
Search the Collection
➤ Use the Select your language drop-down menu to set the language of the examples in this section.
Synonyms can be used only in queries that use the text
operator. In this section, you will connect to your Atlas cluster
and the run the sample queries using the text
operator against the
title
field in the sample_mflix.movies
collection. The sample
queries use words that are configured as synonyms of different mapping
types in the synonyms source collection, which is referenced in the
synonyms mapping that the queries use.