synonyms

For the complete MongoDB documentation index, see www.mongodb.com/docs/llms.txt

<!-- Tab options on this page. Append to the .md URL to filter: ?tabs=<id,...> select specific tabs (e.g. ?tabs=nodejs,shell) ?allTabs=true include every tab (no param) default: one tab per tabset

Available tabs: other tabs: vib, jsonib -->

Define Synonym Mappings in Your MongoDB Search Index

The synonyms option in a MongoDB Search [index definition](https://www.mongodb.com/docs/search/indexes/index-definitions.md#std-label-ref-index-definitions specifies synonym mappings that allow you to index and search your collection for words that have the same or nearly the same meaning. To configure a MongoDB Search [index](https://www.mongodb.com/docs/search/indexes/index-definitions.md#std-label-ref-index-definitions with synonym mappings, you must:

  1. Add a collection of synonym documents to your cluster. Ensure that:

  2. Your collection is in the same database as the index that will reference the collection

  3. The documents are [properly formatted](https://www.mongodb.com/docs/search/indexes/synonyms.md#std-label-synonyms-coll-spec

  4. Reference the synonym source collection in a synonym mapping in the [index definition.](https://www.mongodb.com/docs/search/indexes/index-definitions.md#std-label-ref-index-definitions

A synonym mapping configures a MongoDB Search index to support queries that apply synonyms from a synonym source collection in the same database as the collection you are indexing. You can use synonyms only in queries that use the [text](https://www.mongodb.com/docs/search/query/operators-collectors/text.md#std-label-text-ref or [phrase](https://www.mongodb.com/docs/search/query/operators-collectors/phrase.md#std-label-phrase-ref operator.

Note: Free (M0) Tier Cluster Limitation

An index definition can have only one [synonym mapping.](https://www.mongodb.com/docs/search/indexes/synonyms.md#std-label-synonyms-ref

This page describes the format of the synonyms source collection and how to define synonym mappings that reference the synonym source collection in your MongoDB Search index.

Syntax

synonyms has the following syntax in an index definition:

{
  "synonyms": [
    {
      "name": "<synonym-mapping-name>",
      "source": {
        "collection": "<source-collection-name>"
      },
      "analyzer": "<synonym-mapping-analyzer>"
    }
  ]
}

Options

synonyms takes the following fields in an index definition:

Field Type Description Necessity
analyzer string Name of the [analyzer](https://www.mongodb.com/docs/search/indexes/analyzers/overview.md#std-label-analyzers-ref to use with this synonym mapping. You can use a synonym mapping to query only fields analyzed with the same analyzer. To use synonyms with stop words, you must either index the field using the [Standard Analyzer](https://www.mongodb.com/docs/search/indexes/analyzers/standard.md#std-label-ref-standard-analyzer or add the synonym entry without the stop word. You can use any MongoDB Search analyzer except the following: [Language analyzers:](https://www.mongodb.com/docs/search/indexes/analyzers/language.md#std-label-ref-language-analyzers lucene.kuromoji; lucene.cjk [Custom analyzer](https://www.mongodb.com/docs/search/indexes/analyzers/custom.md#std-label-custom-analyzers tokenizers and token filters: [nGram](https://www.mongodb.com/docs/search/indexes/analyzers/tokenizers.md#std-label-nGram-tokenizer-ref Tokenizer; [edgeGram](https://www.mongodb.com/docs/search/indexes/analyzers/tokenizers.md#std-label-edgeGram-tokenizer-ref Tokenizer; [daitchMokotoffSoundex](https://www.mongodb.com/docs/search/indexes/analyzers/token-filters.md#std-label-daitchmokotoffsoundex-tf-ref Token Filter; [nGram](https://www.mongodb.com/docs/search/indexes/analyzers/token-filters.md#std-label-ngram-tf-ref Token Filter; [edgeGram](https://www.mongodb.com/docs/search/indexes/analyzers/token-filters.md#std-label-edgegram-tf-ref Token Filter; [shingle](https://www.mongodb.com/docs/search/indexes/analyzers/token-filters.md#std-label-shingle-tf-ref Token Filter; [wordDelimiterGraph](https://www.mongodb.com/docs/search/indexes/analyzers/token-filters.md#std-label-wordDelimiterGraph-tf-ref Token Filter Required
name string Name of the synonym mapping. Name must be unique in the index definition. Value can't be an empty string. Required
source document [Source collection](https://www.mongodb.com/docs/search/indexes/synonyms.md#std-label-synonyms-coll-spec for synonyms. The source option takes the collection field. Required
source.collection string Name of the MongoDB collection that is in the same database as the MongoDB Search index. Documents in this collection must be in the format described in the [Synonyms Source Collection Documents.](https://www.mongodb.com/docs/search/indexes/synonyms.md#std-label-synonyms-coll-spec Required

Synonyms Source Collection Documents

Each document in the collection specified as the source for the synonyms describe how one or more words map to one or more synonyms of those words.

Note:

On free clusters, the synonyms collection can't exceed 10,000 documents.

Format of Synonyms Source Collection Documents

You must configure each document with the following fields:

Field Type Description Necessity
input array of strings Required for mappingType: explicit mappings. For explicit mappings, synonyms values are synonyms of each input token. Value can't be an empty or all-whitespace string. You can specify the same input value in multiple documents. Conditional
mappingType string Type of mapping. Value can be one of the following: equivalent - describes a set of tokens that are equivalent to one another. For an example of this mappingType, see [Example.](https://www.mongodb.com/docs/search/indexes/synonyms.md#std-label-synonyms-equivalent-mapping-type-eg; explicit - matches input tokens and replaces them with all alternative synonyms tokens. For an example of this mappingType, see [Example.](https://www.mongodb.com/docs/search/indexes/synonyms.md#std-label-synonyms-explicit-mapping-type-eg Required
synonyms array of strings Words that are synonyms of one another if mappingType is equivalent or synonyms of input tokens if mappingType is explicit. synonyms must have at least one value. To use synonyms with stop words, you must either add the synonym entry without the stop word or index the field using the [Standard Analyzer.](https://www.mongodb.com/docs/search/indexes/analyzers/standard.md#std-label-ref-standard-analyzer For an example of each mappingType see [Source Collection Document Examples.](https://www.mongodb.com/docs/search/indexes/synonyms.md#std-label-synonyms-mapping-type-egs Required

The documents in the collection can contain other fields. The documents in the collection are additive, and mappings are deduplicated. MongoDB Search synonyms are stored as a separate Atlas collection, which counts against the same storage quota as any other collection in Atlas. MongoDB Search might use more compute resources to apply synonyms from larger synonyms source collections.

Warning:

Don't include invalid synonym documents in the synonym source collection. MongoDB Search doesn't create indexes if the indexes use synonym mappings that reference collections with invalid documents. Only include synonym documents that are properly formatted in your synonym source collection.

MongoDB doesn't recommend adding synonym documents to synonym source collections in a production environment without first validating that they are properly formatted and behave as expected in a test environment.

Changes to Synonyms Source Collection Documents

If you make changes to your synonyms source collection:

  • You don't need to reindex because MongoDB Search watches for changes and automatically updates its internal synonym map.

  • The time it takes MongoDB Search to update the synonym mappings increases with the synonym source collection size. Note that the changes to synonym documents are reflected in your MongoDB Search query results eventually.

Collection Document Examples

Source Collection Document Examples

Atlas provides the documents for the following MongoDB Search mapping type examples in a collection named sample_synonyms. You can load these documents on your cluster in the same database as your collection. To load these documents on your cluster, when you [create the index](https://www.mongodb.com/docs/search/indexes/manage-indexes.md#std-label-ref-create-index for your collection, do the following:

When you select the Configuration Method, select the Visual Editor.

When you Add synonym mapping to your index, select Load sample collection from the Synonym source collection dropdown.

equivalent mappingType

In the following example source collection document, the mappingType is set to equivalent so that the tokens car, vehicle, and automobile are configured to be synonyms of one another.

{
  "mappingType": "equivalent",
  "synonyms": ["car", "vehicle", "automobile"]
}

For a [text](https://www.mongodb.com/docs/search/query/operators-collectors/text.md#std-label-text-ref query for car, vehicle, or automobile applying a synonym mapping that includes such a document, MongoDB Search returns documents that contain the term car, vehicle, or automobile.

explicit mappingType

In the following example source collection document, mappingType is set to explicit so that the tokens beer, brew, and pint are configured to be synonyms of the input token beer.

{
  "mappingType": "explicit",
  "input": ["beer"],
  "synonyms": ["beer", "brew", "pint"]
}

For a [text](https://www.mongodb.com/docs/search/query/operators-collectors/text.md#std-label-text-ref query for beer applying a synonym mapping that includes such a document, MongoDB Search returns documents that contain the terms “beer”, “brew”, or “pint” because the input token beer is explicitly mapped to all these synonyms tokens. However, for a query for pint, MongoDB Search does not find documents that contain beer because pint is not explicitly mapped to beer.

Synonym Source Collection Example

The following collection named synonymous_terms is an example synonym source collection that can be used with the movies collection in the sample_mflix database.

The sample_mflix.synonymous_terms collection contains the following documents:

{
  "mappingType": "equivalent",
  "synonyms": ["car", "vehicle", "automobile"]
}
{
  "mappingType": "explicit",
  "input": ["race"],
  "synonyms": ["contest", "rally"]
}
{
  "mappingType": "equivalent",
  "synonyms": ["dress", "apparel", "attire"]
}
{
  "mappingType": "explicit",
  "input": ["boat"],
  "synonyms": ["vessel", "sail"]
}

Index Definition Examples

The following examples for the sample_mflix.movies collection create index definitions that use [static and dynamic mappings](https://www.mongodb.com/docs/search/indexes/define-field-mappings.md#std-label-static-dynamic-mappings. These examples use the synonymous_terms synonym source collection described in the [Synonym Source Collection Example](https://www.mongodb.com/docs/search/indexes/synonyms.md#std-label-sample-synonyms-coll section to enable synonym mappings.

Use Static Mappings

This example performs the following actions:

  • Configures an index with a single text field and a single synonym mapping definition that uses the mapping configured in the synonymous_terms collection.

  • Analyzes the plot field with the lucene.english analyzer.

  • Enables synonyms from the synonymous_terms collection for queries on fields analyzed with the lucene.english analyzer.

You can use the Atlas Administration API to configure the index. Replace the following values in the sample curl command:

  • Replace {ACCESS-TOKEN} with your Service Account access token. To learn how to generate an access token, see [Make an API (Application Programming Interface) Request.](https://www.mongodb.com/docs/atlas/configure-api-access.md#std-label-example-api-request

  • Replace {groupId} with the project ID of the project where you want to create the MongoDB Search index.

  • Replace {clusterName} with the name of the cluster where you want to create the MongoDB Search index.

curl --header "Authorization: Bearer ${ACCESS_TOKEN}" \
    --header "Accept: application/vnd.atlas.2025-03-12+json" \
    --header "Content-Type: application/json" \
    -X POST "https://cloud.mongodb.com/api/atlas/v2/groups/{groupId}/clusters/{clusterName}/search/indexes" \
    -d '{ "collectionName": "movies",
        "database": "sample_mflix",
        "name": "default",
        "type": "search",
        "definition": {
            "mappings": {
              "dynamic": false,
              "fields": {
                "plot": {
                  "type": "string",
                  "analyzer": "lucene.english"
                }
              }
            },
            "synonyms": [
              {
                "analyzer": "lucene.english",
                "name": "my_synonyms",
                "source": {
                  "collection": "synonymous_terms"
                }
              }
            ]
        }
    }'

Use Dynamic Mappings

This example performs the following actions:

  • Configures an index for all document fields and a single synonym mapping definition that uses the mapping configured in the synonymous_terms collection.

  • Uses the default analyzer, lucene.standard, to analyze all the fields.

  • Enables synonyms from the synonymous_terms collection for queries on fields analyzed with the lucene.standard analyzer.

You can use the Atlas Administration API to configure the index. Replace the following values in the sample curl command:

  • Replace {ACCESS-TOKEN} with your Service Account access token. To learn how to generate an access token, see [Make an API (Application Programming Interface) Request.](https://www.mongodb.com/docs/atlas/configure-api-access.md#std-label-example-api-request

  • Replace {groupId} with the project ID of the project where you want to create the MongoDB Search index.

  • Replace {clusterName} with the name of the cluster where you want to create the MongoDB Search index.

curl --header "Authorization: Bearer ${ACCESS_TOKEN}" \
    --header "Accept: application/vnd.atlas.2025-03-12+json" \
    --header "Content-Type: application/json" \
    -X POST "https://cloud.mongodb.com/api/atlas/v2/groups/{groupId}/clusters/{clusterName}/search/indexes" \
    -d '{ "collectionName": "movies",
        "database": "sample_mflix",
        "name": "default",
        "type": "search",
        "definition": {
            "mappings": {
              "dynamic": true
            },
            "synonyms": [
              {
                "analyzer": "lucene.standard",
                "name": "my_synonyms",
                "source": {
                  "collection": "synonymous_terms"
                }
              }
            ]
        }
    }'

Learn More

To learn how to add a collection that configures words as synonyms to your cluster, and use this collection to create a MongoDB Search index and run MongoDB Search queries using synonyms, see [How to Use Synonyms with MongoDB Search.](https://www.mongodb.com/docs/search/tutorial/synonyms-tutorial.md#std-label-synonyms-tutorial

To see examples of text queries that use synonyms, see [Synonyms Examples](https://www.mongodb.com/docs/search/query/operators-collectors/text.md#std-label-text-ref-synonyms-eg in the [text](https://www.mongodb.com/docs/search/query/operators-collectors/text.md#std-label-text-ref reference page.

To see an example phrase queries that uses synonyms, see [Synonyms Example](https://www.mongodb.com/docs/search/query/operators-collectors/phrase.md#std-label-fts-phrase-synonyms-example in the [phrase](https://www.mongodb.com/docs/search/query/operators-collectors/phrase.md#std-label-phrase-ref reference page.

The following examples for the sample_mflix.movies collection create index definitions that use [static and dynamic mappings](https://www.mongodb.com/docs/search/indexes/define-field-mappings.md#std-label-static-dynamic-mappings. These examples use the synonymous_terms synonym source collection described in the [Synonym Source Collection Example](https://www.mongodb.com/docs/search/indexes/synonyms.md#std-label-sample-synonyms-coll section to enable synonym mappings.

Use Static Mappings

This example performs the following actions:

  • Configures an index with a single text field and a single synonym mapping definition that uses the mapping configured in the synonymous_terms collection.

  • Analyzes the plot field with the lucene.english analyzer.

  • Enables synonyms from the synonymous_terms collection for queries on fields analyzed with the lucene.english analyzer.

  • Create a static-index.json configuration file and specify the synonyms field.

```json { "name": "default", "collectionName": "movies", "database": "sample_mflix", "mappings": { "dynamic": false, "fields": { "plot": { "type": "string", "analyzer": "lucene.english" } } }, "synonyms": [ { "analyzer": "lucene.english", "name": "my_synonyms", "source": { "collection": "synonymous_terms" } } ] }

```

  1. Connect to the Atlas CLI

In your terminal, connect to your cluster from the Atlas CLI. To learn more, see Connect from the Atlas CLI.

  1. Create a MongoDB Search index.

Run the atlas clusters search indexes create command in your terminal, replacing <clusterName> with the name of the cluster, and <path-to-file> with the path to the static-index.json file:

shell atlas clusters search indexes create <clusterName> --file <path-to-file>

Use Dynamic Mappings

This example performs the following actions:

  • Configures an index for all document fields and a single synonym mapping definition that uses the mapping configured in the synonymous_terms collection.

  • Uses the default analyzer, lucene.standard, to analyze all the fields.

  • Enables synonyms from the synonymous_terms collection for queries on fields analyzed with the lucene.standard analyzer.

  • Create a dynamic-index.json configuration file and specify the synonyms field.

```json { "name": "default", "collectionName": "movies", "database": "sample_mflix", "mappings": { "dynamic": true }, "synonyms": [ { "analyzer": "lucene.standard", "name": "my_synonyms", "source": { "collection": "synonymous_terms" } } ] }

```

  1. Connect to the Atlas CLI

In your terminal, connect to your cluster from the Atlas CLI. To learn more, see Connect from the Atlas CLI.

  1. Create a MongoDB Search index.

Run the atlas clusters search indexes create command in your terminal, replacing <clusterName> with the name of the cluster, and <path-to-file> with the path to the dynamic-index.json file:

shell atlas clusters search indexes create <clusterName> --file <path-to-file>

Learn More

To learn how to add a collection that configures words as synonyms to your cluster, and use this collection to create a MongoDB Search index and run MongoDB Search queries using synonyms, see [How to Use Synonyms with MongoDB Search.](https://www.mongodb.com/docs/search/tutorial/synonyms-tutorial.md#std-label-synonyms-tutorial

To see examples of text queries that use synonyms, see [Synonyms Examples](https://www.mongodb.com/docs/search/query/operators-collectors/text.md#std-label-text-ref-synonyms-eg in the [text](https://www.mongodb.com/docs/search/query/operators-collectors/text.md#std-label-text-ref reference page.

To see an example phrase queries that uses synonyms, see [Synonyms Example](https://www.mongodb.com/docs/search/query/operators-collectors/phrase.md#std-label-fts-phrase-synonyms-example in the [phrase](https://www.mongodb.com/docs/search/query/operators-collectors/phrase.md#std-label-phrase-ref reference page.

The following examples for the sample_mflix.movies collection create index definitions that use [static and dynamic mappings](https://www.mongodb.com/docs/search/indexes/define-field-mappings.md#std-label-static-dynamic-mappings. These examples use the synonymous_terms synonym source collection described in the [Synonym Source Collection Example](https://www.mongodb.com/docs/search/indexes/synonyms.md#std-label-sample-synonyms-coll section to enable synonym mappings.

Use Static Mappings

This example performs the following actions:

  • Configures an index with a single text field and a single synonym mapping definition that uses the mapping configured in the synonymous_terms collection.

  • Analyzes the plot field with the lucene.english analyzer.

  • Enables synonyms from the synonymous_terms collection for queries on fields analyzed with the lucene.english analyzer.

  • Connect to your cluster using MongoDB Compass.

Open Compass and connect to your cluster. For detailed instructions, see [Connect to a Cluster via Compass.](https://www.mongodb.com/docs/atlas/compass-connection.md#std-label-atlas-connect-via-compass

  1. Specify the database and collection.

On the Database screen, click the sample_mflix database, then click the name of the movies collection.

  1. Create the MongoDB Search index.

Click the Indexes tab, then select Search Indexes.

Click Create Atlas Search Index to open the index creation dialog box.

Specify a name for the index.

Your MongoDB Search index is named default by default. If you keep this name, then your index will be the default Search index for any MongoDB Search query that does not specify a different index option in its [operators](https://www.mongodb.com/docs/search/query/operators-collectors/overview.md#std-label-fts-operators. If you are creating multiple indexes, we recommend that you maintain a consistent, descriptive naming convention across your indexes.

Specify the JSON (Javascript Object Notation) MongoDB Search index definition.

json { "mappings": { "dynamic": false, "fields": { "plot": { "type": "string", "analyzer": "lucene.english" } } }, "synonyms": [ { "analyzer": "lucene.english", "name": "my_synonyms", "source": { "collection": "synonymous_terms" } } ] }

Click Create Search Index.

Use Dynamic Mappings

This example performs the following actions:

  • Configures an index for all document fields and a single synonym mapping definition that uses the mapping configured in the synonymous_terms collection.

  • Uses the default analyzer, lucene.standard, to analyze all the fields.

  • Enables synonyms from the synonymous_terms collection for queries on fields analyzed with the lucene.standard analyzer.

  • Connect to your cluster using MongoDB Compass.

Open Compass and connect to your cluster. For detailed instructions, see [Connect to a Cluster via Compass.](https://www.mongodb.com/docs/atlas/compass-connection.md#std-label-atlas-connect-via-compass

  1. Specify the database and collection.

On the Database screen, click the sample_mflix database, then click the name of the movies collection.

  1. Create the MongoDB Search index.

Click the Indexes tab, then select Search Indexes.

Click Create Atlas Search Index to open the index creation dialog box.

Specify a name for the index.

Your MongoDB Search index is named default by default. If you keep this name, then your index will be the default Search index for any MongoDB Search query that does not specify a different index option in its [operators](https://www.mongodb.com/docs/search/query/operators-collectors/overview.md#std-label-fts-operators. If you are creating multiple indexes, we recommend that you maintain a consistent, descriptive naming convention across your indexes.

Specify the JSON (Javascript Object Notation) MongoDB Search index definition.

json { "mappings": { "dynamic": true }, "synonyms": [ { "analyzer": "lucene.standard", "name": "my_synonyms", "source": { "collection": "synonymous_terms" } } ] }

Click Create Search Index.

Learn More

To learn how to add a collection that configures words as synonyms to your cluster, and use this collection to create a MongoDB Search index and run MongoDB Search queries using synonyms, see [How to Use Synonyms with MongoDB Search.](https://www.mongodb.com/docs/search/tutorial/synonyms-tutorial.md#std-label-synonyms-tutorial

To see examples of text queries that use synonyms, see [Synonyms Examples](https://www.mongodb.com/docs/search/query/operators-collectors/text.md#std-label-text-ref-synonyms-eg in the [text](https://www.mongodb.com/docs/search/query/operators-collectors/text.md#std-label-text-ref reference page.

To see an example phrase queries that uses synonyms, see [Synonyms Example](https://www.mongodb.com/docs/search/query/operators-collectors/phrase.md#std-label-fts-phrase-synonyms-example in the [phrase](https://www.mongodb.com/docs/search/query/operators-collectors/phrase.md#std-label-phrase-ref reference page.

The following examples for the sample_mflix.movies collection create index definitions that use [static and dynamic mappings](https://www.mongodb.com/docs/search/indexes/define-field-mappings.md#std-label-static-dynamic-mappings. These examples use the synonymous_terms synonym source collection described in the [Synonym Source Collection Example](https://www.mongodb.com/docs/search/indexes/synonyms.md#std-label-sample-synonyms-coll section to enable synonym mappings.

Use Static Mappings

This example performs the following actions:

  • Configures an index with a single text field and a single synonym mapping definition that uses the mapping configured in the synonymous_terms collection.

  • Analyzes the plot field with the lucene.english analyzer.

  • Enables synonyms from the synonymous_terms collection for queries on fields analyzed with the lucene.english analyzer.

  • Connect to the deployment by using mongosh.

In your terminal, connect to your Atlas cloud-hosted deployment or local deployment from [mongosh](https://www.mongodb.com/docs/mongodb-shell.md#mongodb-binary-bin.mongosh. For detailed instructions on how to connect, see Connect to a Deployment.

  1. Switch to the database that contains the collection for which you want to create the index.

shell use sample_mflix

Output:

shell switched to db sample_mflix

  1. Run the db.collection.createSearchIndex() method to create the index.

javascript db.movies.createSearchIndex( "default", { "mappings": { "dynamic": false, "fields": { "plot": { "type": "string", "analyzer": "lucene.english" } } }, "synonyms": [ { "analyzer": "lucene.english", "name": "my_synonyms", "source": { "collection": "synonymous_terms" } } ]} )

Output:

text default

Use Dynamic Mappings

This example performs the following actions:

  • Configures an index for all document fields and a single synonym mapping definition that uses the mapping configured in the synonymous_terms collection.

  • Uses the default analyzer, lucene.standard, to analyze all the fields.

  • Enables synonyms from the synonymous_terms collection for queries on fields analyzed with the lucene.standard analyzer.

  • Connect to the deployment by using mongosh.

In your terminal, connect to your Atlas cloud-hosted deployment or local deployment from [mongosh](https://www.mongodb.com/docs/mongodb-shell.md#mongodb-binary-bin.mongosh. For detailed instructions on how to connect, see Connect to a Deployment.

  1. Switch to the database that contains the collection for which you want to create the index.

shell use sample_mflix

Output:

shell switched to db sample_mflix

  1. Run the db.collection.createSearchIndex() method to create the index.

javascript db.movies.createSearchIndex( "default", { "mappings": { "dynamic": true }, "synonyms": [ { "analyzer": "lucene.standard", "name": "my_synonyms", "source": { "collection": "synonymous_terms" } } ]} )

Output:

text default

Learn More

To learn how to add a collection that configures words as synonyms to your cluster, and use this collection to create a MongoDB Search index and run MongoDB Search queries using synonyms, see [How to Use Synonyms with MongoDB Search.](https://www.mongodb.com/docs/search/tutorial/synonyms-tutorial.md#std-label-synonyms-tutorial

To see examples of text queries that use synonyms, see [Synonyms Examples](https://www.mongodb.com/docs/search/query/operators-collectors/text.md#std-label-text-ref-synonyms-eg in the [text](https://www.mongodb.com/docs/search/query/operators-collectors/text.md#std-label-text-ref reference page.

To see an example phrase queries that uses synonyms, see [Synonyms Example](https://www.mongodb.com/docs/search/query/operators-collectors/phrase.md#std-label-fts-phrase-synonyms-example in the [phrase](https://www.mongodb.com/docs/search/query/operators-collectors/phrase.md#std-label-phrase-ref reference page.

The following examples for the sample_mflix.movies collection create index definitions that use [static and dynamic mappings](https://www.mongodb.com/docs/search/indexes/define-field-mappings.md#std-label-static-dynamic-mappings. These examples use the synonymous_terms synonym source collection described in the [Synonym Source Collection Example](https://www.mongodb.com/docs/search/indexes/synonyms.md#std-label-sample-synonyms-coll section to enable synonym mappings.

Use Static Mappings

This example performs the following actions:

  • Configures an index with a single text field and a single synonym mapping definition that uses the mapping configured in the synonymous_terms collection.

  • Analyzes the plot field with the lucene.english analyzer.

  • Enables synonyms from the synonymous_terms collection for queries on fields analyzed with the lucene.english analyzer.

You can use the Visual Editor or the JSON (Javascript Object Notation) Editor in the Atlas UI to configure the following index. To configure this index, after you select your configuration method, select the movies collection in the sample_mflix database.

Visual Editor

  1. Click Refine Your Index.

  2. In the Field Mappings section, click Add Field.

  3. Click Customized Configuration.

  4. Configure the following settings in the Add Field Mapping window:

Field Name Enable Dynamic Mapping Data Type Configuration
Select plot. Toggle to disable. Click Add Data Type.Select String from the dropdown.Select lucene.english under lucene.language from the Index Analyzer dropdown.
  1. Click Add.

  2. In the Synonyms Mappings section, click Add Synonym Mapping.

  3. Configure the following settings in the Add Synonym Mapping window:

Synonym mapping name Synonym source collection Analyzer
Enter my_synonyms Select synonymous_terms. Select lucene.english under lucene.language from dropdown.
  1. Click Add.

Use Dynamic Mappings

This example performs the following actions:

  • Configures an index for all document fields and a single synonym mapping definition that uses the mapping configured in the synonymous_terms collection.

  • Uses the default analyzer, lucene.standard, to analyze all the fields.

  • Enables synonyms from the synonymous_terms collection for queries on fields analyzed with the lucene.standard analyzer.

You can use the Visual Editor or the JSON (Javascript Object Notation) Editor in the Atlas UI to configure the following index. To configure this index, after you select your configuration method, select the movies collection in the sample_mflix database.

Visual Editor

  1. Click Refine Your Index.

  2. In the Synonyms Mappings section, click Add Synonym Mapping.

  3. Configure the following settings in the Add Synonym Mapping window:

Synonym mapping name Synonym source collection Analyzer
Enter my_synonyms Select synonymous_terms. Select lucene.standard from the dropdown if it isn't already selected.
  1. Click Add.

Learn More

To learn how to add a collection that configures words as synonyms to your cluster, and use this collection to create a MongoDB Search index and run MongoDB Search queries using synonyms, see [How to Use Synonyms with MongoDB Search.](https://www.mongodb.com/docs/search/tutorial/synonyms-tutorial.md#std-label-synonyms-tutorial

To see examples of text queries that use synonyms, see [Synonyms Examples](https://www.mongodb.com/docs/search/query/operators-collectors/text.md#std-label-text-ref-synonyms-eg in the [text](https://www.mongodb.com/docs/search/query/operators-collectors/text.md#std-label-text-ref reference page.

To see an example phrase queries that uses synonyms, see [Synonyms Example](https://www.mongodb.com/docs/search/query/operators-collectors/phrase.md#std-label-fts-phrase-synonyms-example in the [phrase](https://www.mongodb.com/docs/search/query/operators-collectors/phrase.md#std-label-phrase-ref reference page.

The following examples for the sample_mflix.movies collection create index definitions that use [static and dynamic mappings](https://www.mongodb.com/docs/search/indexes/define-field-mappings.md#std-label-static-dynamic-mappings. These examples use the synonymous_terms synonym source collection described in the [Synonym Source Collection Example](https://www.mongodb.com/docs/search/indexes/synonyms.md#std-label-sample-synonyms-coll section to enable synonym mappings.

Use Static Mappings

This example performs the following actions:

  • Configures an index with a single text field and a single synonym mapping definition that uses the mapping configured in the synonymous_terms collection.

  • Analyzes the plot field with the lucene.english analyzer.

  • Enables synonyms from the synonymous_terms collection for queries on fields analyzed with the lucene.english analyzer.

  • Set up your application.

To learn how to install the driver and configure your C application, see the Get Started tutorial in the MongoDB C Driver documentation.

  1. Define the index.

Create a create-static-index.c file in your application directory, and copy and paste the following code into the file.

```c #include #include

int main(int argc, char argv[]) { mongoc_client_t client; mongoc_collection_t collection; bson_error_t error; bson_t cmd; bson_t index_definition; bool success;

   /* Initialize the MongoDB C Driver. */
   mongoc_init();

   /* Connect to your MongoDB deployment. */
   client = mongoc_client_new("<connection-string>");
   collection = mongoc_client_get_collection (client, "sample_mflix", "movies");

   if (!client) {
       fprintf(stderr, "Failed to create client.\n");
       return 1;
   }

   /* Create the MongoDB Search index definition for the plot field with synonyms. */
   char *cmd_str = bson_strdup_printf (
       BSON_STR ({
           "createSearchIndexes": "%s",
           "indexes": [{
               "name": "default",
               "definition": {
                   "mappings": {
                       "dynamic": false,
                       "fields": {
                           "plot": {
                               "type": "string",
                               "analyzer": "lucene.english"
                           }
                       }
                   },
                   "synonyms": [
                       {
                           "analyzer": "lucene.english",
                           "name": "my_synonyms",
                           "source": {
                               "collection": "synonymous_terms"
                           }
                       }
                   ]
               }
           }]
       }),
       "movies"
   );
   bson_init_from_json (&cmd, cmd_str, -1, &error);
   bson_free (cmd_str);

   if (mongoc_collection_command_simple (collection, &cmd, NULL, NULL, &error)) {
       printf ("Index created!\n");
   } else {
       fprintf (stderr, "Failed to create search index: %s", error.message);
   }

   /* Clean up. */
   bson_destroy (&cmd);
   mongoc_collection_destroy (collection);
   mongoc_client_destroy(client);
   mongoc_cleanup();

   return 0;

} ```

  1. Create the index.

In your terminal, run the following commands to build and run this application:

shell cmake -S. -Bcmake-build cmake --build cmake-build --target index.out ./cmake-build/index.out

Output:

text Index created!

Use Dynamic Mappings

This example performs the following actions:

  • Configures an index for all document fields and a single synonym mapping definition that uses the mapping configured in the synonymous_terms collection.

  • Uses the default analyzer, lucene.standard, to analyze all the fields.

  • Enables synonyms from the synonymous_terms collection for queries on fields analyzed with the lucene.standard analyzer.

  • Set up your application.

To learn how to install the driver and configure your C application, see the Get Started tutorial in the MongoDB C Driver documentation.

  1. Define the index.

Create a create-dynamic-index.c file in your application directory, and copy and paste the following code into the file.

```c #include #include

int main(int argc, char argv[]) { mongoc_client_t client; mongoc_collection_t collection; bson_error_t error; bson_t cmd; bson_t index_definition; bool success;

   /* Initialize the MongoDB C Driver. */
   mongoc_init();

   /* Connect to your MongoDB deployment. */
   client = mongoc_client_new("<connection-string>");
   collection = mongoc_client_get_collection (client, "sample_mflix", "movies");

   if (!client) {
       fprintf(stderr, "Failed to create client.\n");
       return 1;
   }

   /* Create the MongoDB Search index definition for a dynamic index with synonyms. */
   char *cmd_str = bson_strdup_printf (
       BSON_STR ({
           "createSearchIndexes": "%s",
           "indexes": [{
               "name": "default",
               "definition": {
                   "mappings": {
                       "dynamic": true
                   },
                   "synonyms": [
                       {
                           "analyzer": "lucene.standard",
                           "name": "my_synonyms",
                           "source": {
                               "collection": "synonymous_terms"
                           }
                       }
                   ]
               }
           }]
       }),
       "movies"
   );
   bson_init_from_json (&cmd, cmd_str, -1, &error);
   bson_free (cmd_str);

   if (mongoc_collection_command_simple (collection, &cmd, NULL, NULL, &error)) {
       printf ("Index created!\n");
   } else {
       fprintf (stderr, "Failed to create search index: %s", error.message);
   }

   /* Clean up. */
   bson_destroy (&cmd);
   mongoc_collection_destroy (collection);
   mongoc_client_destroy(client);
   mongoc_cleanup();

   return 0;

} ```

  1. Create the index.

In your terminal, run the following commands to build and run this application:

shell cmake -S. -Bcmake-build cmake --build cmake-build --target index.out ./cmake-build/index.out

Output:

text Index created!

Learn More

To learn how to add a collection that configures words as synonyms to your cluster, and use this collection to create a MongoDB Search index and run MongoDB Search queries using synonyms, see [How to Use Synonyms with MongoDB Search.](https://www.mongodb.com/docs/search/tutorial/synonyms-tutorial.md#std-label-synonyms-tutorial

To see examples of text queries that use synonyms, see [Synonyms Examples](https://www.mongodb.com/docs/search/query/operators-collectors/text.md#std-label-text-ref-synonyms-eg in the [text](https://www.mongodb.com/docs/search/query/operators-collectors/text.md#std-label-text-ref reference page.

To see an example phrase queries that uses synonyms, see [Synonyms Example](https://www.mongodb.com/docs/search/query/operators-collectors/phrase.md#std-label-fts-phrase-synonyms-example in the [phrase](https://www.mongodb.com/docs/search/query/operators-collectors/phrase.md#std-label-phrase-ref reference page.

The following examples for the sample_mflix.movies collection create index definitions that use [static and dynamic mappings](https://www.mongodb.com/docs/search/indexes/define-field-mappings.md#std-label-static-dynamic-mappings. These examples use the synonymous_terms synonym source collection described in the [Synonym Source Collection Example](https://www.mongodb.com/docs/search/indexes/synonyms.md#std-label-sample-synonyms-coll section to enable synonym mappings.

Use Static Mappings

This example performs the following actions:

  • Configures an index with a single text field and a single synonym mapping definition that uses the mapping configured in the synonymous_terms collection.

  • Analyzes the plot field with the lucene.english analyzer.

  • Enables synonyms from the synonymous_terms collection for queries on fields analyzed with the lucene.english analyzer.

  • Set up your application

To learn how to install the driver and configure your C++ application, see the Get Started tutorial in the MongoDB C++ Driver documentation.

  1. Define the index.

Create a CreateStaticIndex.cpp file in your project directory, and copy and paste the following code into the file.

```cpp #include <iostream>

#include <bsoncxx/builder/basic/document.hpp> #include <bsoncxx/json.hpp> #include <mongocxx/client.hpp> #include <mongocxx/instance.hpp> #include <mongocxx/uri.hpp>

using bsoncxx::builder::basic::kvp; using bsoncxx::builder::basic::make_document;

int main() { // Initialize the MongoDB C++ driver mongocxx::instance instance{};

   // Connect to your MongoDB deployment
   mongocxx::uri uri("&lt;connection-string>");
   mongocxx::client client(uri);

   auto db = client["sample_mflix"];
   auto collection = db["movies"]

(本文超出站点单文档体积上限,已截断)