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 a sample synonyms source collection in the
sample_mflix
database. - Set up an Atlas Search index with synonym mapping 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.
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 a sample synonyms
source collection in the sample_mflix
database, then use that
synonyms source collection 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.
Click your cluster's Browse Collections button.¶
Create the sample synonyms collection in the sample_mflix
database.¶
- Expand the
sample_mflix
database and click the icon to open the Create Collection modal. - Type
sample_synonyms
in the Collection name field. - Click Create to create the collection in the
sample_mflix
database.
Load the sample data into the sample_synonyms
collection.¶
- Select the
sample_synonyms
collection if it's already not selected. - Click Insert Document for each of the sample documents to add to the collection.
- Click the JSON view ({}) to replace the default document.
Copy and paste the following sample documents, one at a time, and click Insert to add the documents, one at a time, to the collection.
{ "mappingType": "equivalent", "synonyms": ["car", "vehicle", "automobile"] } { "mappingType": "explicit", "input": ["boat"], "synonyms": ["boat", "vessel", "sail"] }
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 a synonym
mapping named mySynonyms
for the sample_mflix.movies
collection. The mySynonyms
mapping definition in the index
references the synonyms source collection named sample_synonyms
that you created in the sample_mflix
database and uses the
lucene.english
language analyzer.
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.
Click Create Index.¶
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
.The index name defaults to default. You can leave the default name in place or choose one of your own.
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.Index names must be unique within their namespace.
- 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.¶
{ "mappings": { "dynamic": false, "fields": { "title": { "analyzer": "lucene.english", "type": "string" } } }, "synonyms": [ { "analyzer": "lucene.english", "name": "mySynonyms", "source": { "collection": "sample_synonyms" } } ] }
The above index definition specifies:
- The language analyzer
lucene.english
as the default analyzer for both indexing and querying thetitle
field. - The name
mySynonyms
as the name for this synonym mapping. - The
sample_synonyms
collection as the source synonyms collection to look for synonyms for queries usingmySynonyms
mapping.mySynonyms
can be used in text queries over any field indexed with thelucene.english
analyzer (including thetitle
field, in this example).
Click Create Search Index.¶
Close the You're All Set! Modal Window.¶
A modal window appears to let you know your index is building. Click the Close button.
Wait for the index to finish building.¶
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
.
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 named mySynonyms
that is used by the queries.