> 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:

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

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

2. 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:

```json
{ 
  "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.

```json
{
  "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`.

```json
{
  "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:

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

```json
{
  "mappingType": "explicit",
  "input": ["race"],
  "synonyms": ["contest", "rally"]
}
```

```json
{
  "mappingType": "equivalent",
  "synonyms": ["dress", "apparel", "attire"]
}
```

```json
{
  "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.

```shell
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.

```shell
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.

1. 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"
         }
       }
     ]
   }

   ```

2. 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.](https://www.mongodb.com/docs/atlas/cli/current/connect-atlas-cli/)

3. Create a MongoDB Search index.

   Run the [atlas clusters search indexes create](https://www.mongodb.com/docs/atlas/cli/current/command/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.

1. 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"
         }
       }
     ]
   }

   ```

2. 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.](https://www.mongodb.com/docs/atlas/cli/current/connect-atlas-cli/)

3. Create a MongoDB Search index.

   Run the [atlas clusters search indexes create](https://www.mongodb.com/docs/atlas/cli/current/command/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.

1. Connect to your cluster using MongoDB Compass.

   Open [Compass](https://www.mongodb.com/try/download/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)

2. Specify the database and collection.

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

3. 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.

1. Connect to your cluster using MongoDB Compass.

   Open [Compass](https://www.mongodb.com/try/download/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)

2. Specify the database and collection.

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

3. 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.

1. 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.](https://www.mongodb.com/docs/mongodb-shell/connect/)

2. 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
   ```

3. 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.

1. 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.](https://www.mongodb.com/docs/mongodb-shell/connect/)

2. 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
   ```

3. 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. |

5. Click Add.

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

7. 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. |

8. 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. |

4. 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.

1. Set up your application.

   To learn how to install the driver and configure your C application, see the [Get Started](https://www.mongodb.com/docs/languages/c/c-driver/current/get-started/) tutorial in the MongoDB C Driver documentation.

2. 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 <mongoc/mongoc.h>
   #include <stdio.h>

   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;
   }
   ```

3. 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.

1. Set up your application.

   To learn how to install the driver and configure your C application, see the [Get Started](https://www.mongodb.com/docs/languages/c/c-driver/current/get-started/) tutorial in the MongoDB C Driver documentation.

2. 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 <mongoc/mongoc.h>
   #include <stdio.h>

   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;
   }
   ```

3. 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.

1. Set up your application

   To learn how to install the driver and configure your C++ application, see the [Get Started](https://www.mongodb.com/docs/languages/cpp/cpp-driver/current/get-started/) tutorial in the MongoDB C++ Driver documentation.

2. 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("<connection-string>");
       mongocxx::client client(uri);

       auto db = client["sample_mflix"];
       auto collection = db["movies"];
       auto siv = collection.search_indexes();

       // Create the MongoDB Search index definition for the plot field with synonyms
       auto name = "default";
       auto synonyms = bsoncxx::builder::basic::make_array(
           make_document(
               kvp("analyzer", "lucene.english"),
               kvp("name", "my_synonyms"),
               kvp("source", make_document(
                   kvp("collection", "synonymous_terms")
               ))
           )
       );

       auto definition = make_document(
           kvp("mappings", make_document(
               kvp("dynamic", false),
               kvp("fields", make_document(
                   kvp("plot", make_document(
                       kvp("type", "string"),
                       kvp("analyzer", "lucene.english")
                   ))
               ))
           )),
           kvp("synonyms", synonyms)
       );
       auto model = mongocxx::search_index_model(name, definition.view());

       try {
           // Execute the command to create the index
           auto result = siv.create_one(model);
           std::cout << "New index name: " << result << std::endl;
       } catch (const std::exception& e) {
           std::cerr << "Error creating Atlas Search index: " << e.what() << std::endl;
       }

   }
   ```

3. Create the index.

   ```shell
   c++ --std=c++17 CreateStaticIndex.cpp $(pkg-config --cflags --libs libmongocxx) -o ./app.out
   ./app.out
   ```

   **Output:**

   ```text
   New index name: 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.

1. Set up your application

   To learn how to install the driver and configure your C++ application, see the [Get Started](https://www.mongodb.com/docs/languages/cpp/cpp-driver/current/get-started/) tutorial in the MongoDB C++ Driver documentation.

2. Define the index.

   Create a `CreateDynamicIndex.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("<connection-string>");
       mongocxx::client client(uri);

       auto db = client["sample_mflix"];
       auto collection = db["movies"];
       auto siv = collection.search_indexes();

       // Define the synonym mapping for the lucene.standard analyzer
       auto name = "default";
       auto synonyms = bsoncxx::builder::basic::make_array(
           make_document(
               kvp("analyzer", "lucene.standard"),
               kvp("name", "my_synonyms"),
               kvp("source", make_document(
                   kvp("collection", "synonymous_terms")
               ))
           )
       );

       // Create the MongoDB Search index definition
       auto definition = make_document(
           kvp("mappings", make_document(
               kvp("dynamic", true))
           ),
           kvp("synonyms", synonyms)
       );
       auto model = mongocxx::search_index_model(name, definition.view());

       try {
           // Execute the command to create the index
           auto result = siv.create_one(model);
           std::cout << "New index name: " << result << std::endl;
       } catch (const std::exception& e) {
           std::cerr << "Error creating Atlas Search index: " << e.what() << std::endl;
       }

   }
   ```

3. Create the index.

   ```shell
   c++ --std=c++17 CreateDynamicIndex.cpp $(pkg-config --cflags --libs libmongocxx) -o ./app.out
   ./app.out
   ```

   **Output:**

   ```text
   New index name: 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.

1. Set up your application.

   To learn how to install the driver and configure your Go application, see the [Get Started](https://www.mongodb.com/docs/drivers/go/current/get-started/) tutorial in the MongoDB Go Driver documentation.

2. Define the index.

   Create a `create-static-index.go` file in your project directory, and copy and paste the following code into the file.

   ```go
   package main

   import (
   	"context"
   	"fmt"
   	"log"

   	"go.mongodb.org/mongo-driver/v2/bson"
   	"go.mongodb.org/mongo-driver/v2/mongo"
   	"go.mongodb.org/mongo-driver/v2/mongo/options"
   )

   func main() {
   	ctx := context.Background()

   	// Replace the placeholder with your MongoDB deployment's connection string
   	uri := "<connection-string>"
   	client, err := mongo.Connect(options.Client().
   		ApplyURI(uri))
   	if err != nil {
   		panic(err)
   	}

   	defer func() {
   		if err := client.Disconnect(context.TODO()); err != nil {
   			panic(err)
   		}
   	}()

   	coll := client.Database("sample_mflix").Collection("movies")
   	const indexName = "default"
   	opts := options.SearchIndexes().SetName(indexName).SetType("search")

   	// Create the search index definition

   	searchIndexModel := mongo.SearchIndexModel{
   		Definition: bson.M{
   			"mappings": bson.M{
   				"dynamic": false,
   				"fields": bson.M{
   					"plot": bson.M{
   						"type":     "string",
   						"analyzer": "lucene.english",
   					},
   				},
   			},
   			"synonyms": []bson.M{
   				{
   					"analyzer": "lucene.english",
   					"name":     "my_synonyms",
   					"source": bson.M{
   						"collection": "synonymous_terms",
   					},
   				},
   			},
   		},
   		Options: opts,
   	}

   	// Create the index
   	searchIndexName, err := coll.SearchIndexes().CreateOne(ctx, searchIndexModel)
   	if err != nil {
   		log.Fatalf("Failed to create the Atlas Search index: %v", err)
   	}

   	fmt.Println("Name of index created: " + searchIndexName)
   }

   ```

3. Create the index.

   ```shell
   go run create_static_index.go
   ```

   **Output:**

   ```text
   New index name: 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.

1. Set up your application.

   To learn how to install the driver and configure your Go application, see the [Get Started](https://www.mongodb.com/docs/drivers/go/current/get-started/) tutorial in the MongoDB Go Driver documentation.

2. Define the index.

   Create a `create-dynamic-index.go` file in your project directory, and copy and paste the following code into the file.

   ```go
   package main

   import (
   	"context"
   	"fmt"
   	"log"

   	"go.mongodb.org/mongo-driver/v2/bson"
   	"go.mongodb.org/mongo-driver/v2/mongo"
   	"go.mongodb.org/mongo-driver/v2/mongo/options"
   )

   func main() {
   	ctx := context.Background()

   	// Replace the placeholder with your MongoDB deployment's connection string
   	uri := "<connection-string>"
   	client, err := mongo.Connect(options.Client().
   		ApplyURI(uri))
   	if err != nil {
   		panic(err)
   	}

   	defer func() {
   		if err := client.Disconnect(context.TODO()); err != nil {
   			panic(err)
   		}
   	}()

   	coll := client.Database("sample_mflix").Collection("movies")
   	const indexName = "default"
   	opts := options.SearchIndexes().SetName(indexName).SetType("search")

   	// Create the search index definition
   	searchIndexModel := mongo.SearchIndexModel{
   		Definition: bson.M{
   			"mappings": bson.M{
   				"dynamic": true,
   			},
   			"synonyms": []bson.M{
   				{
   					"analyzer": "lucene.standard",
   					"name":     "my_synonyms",
   					"source": bson.M{
   						"collection": "synonymous_terms",
   					},
   				},
   			},
   		},
   		Options: opts,
   	}

   	// Create the index
   	searchIndexName, err := coll.SearchIndexes().CreateOne(ctx, searchIndexModel)
   	if err != nil {
   		log.Fatalf("Failed to create the Atlas Search index: %v", err)
   	}

   	fmt.Println("Name of index created: " + searchIndexName)
   }

   ```

3. Create the index.

   ```shell
   go run create_dynamic_index.go
   ```

   **Output:**

   ```text
   New index name: 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.

1. Set up your application.

   To learn how to install the driver and configure your Java application, see the [Get Started](https://www.mongodb.com/docs/drivers/java/sync/current/get-started/) tutorial in the MongoDB Java Driver documentation.

2. Define the index.

   In your project's base package directory, create a `CreateStaticIndex.java` file and copy and paste the following code into this file.

   ```java
   import com.mongodb.client.*;
   import org.bson.Document;

   import java.util.List;

   public class CreateStaticIndex {
       public static void main(String[] args) {
           // Replace the placeholder with your MongoDB deployment's connection string
           String connectionString = "<connection-string>";

           try (MongoClient mongoClient = MongoClients.create(connectionString)) {
               MongoDatabase database = mongoClient.getDatabase("sample_mflix");
               MongoCollection<Document> collection = database.getCollection("movies");
               String indexName = "default";

               // Define the synonym mapping for the lucene.english analyzer
               List<Document> synonymsList = List.of(
                       new Document("analyzer", "lucene.english")
                               .append("name", "my_synonyms")
                               .append("source", new Document("collection", "synonymous_terms"))
               );

               // Create the MongoDB Search index definition for the plot field with synonyms
               Document searchIdx = new Document(
                       "mappings",
                       new Document("dynamic", false)
                               .append("fields",
                                       new Document("plot",
                                               new Document("type", "string")
                                                       .append("analyzer", "lucene.english")
                                       )
                               )
               ).append("synonyms", synonymsList);

               // Create the index by using the Java Sync Driver
               collection.createSearchIndex(indexName, searchIdx);
               System.out.println("New index name: " + indexName);
           } catch (Exception e) {
               System.err.println("Error creating Atlas Search index: " + e.getMessage());
           }
       }
   }
   ```

3. Compile and run the file to create the index.

   Compile and run your application in your IDE or your shell.

   ```shell
   javac CreateStaticIndex.java
   java CreateStaticIndex
   ```

   **Output:**

   ```text
   New index name: 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.

1. Set up your application.

   To learn how to install the driver and configure your Java application, see the [Get Started](https://www.mongodb.com/docs/drivers/java/sync/current/get-started/) tutorial in the MongoDB Java Driver documentation.

2. Define the index.

   In your project's base package directory, create a `CreateDynamicIndex.java` file and copy and paste the following code into this file.

   ```java
   import com.mongodb.client.*;
   import org.bson.Document;

   import java.util.List;

   public class CreateDynamicIndex {
       public static void main(String[] args) {
           // Replace the placeholder with your MongoDB deployment's connection string
           String connectionString = "<connection-string>";

           try (MongoClient mongoClient = MongoClients.create(connectionString)) {
               MongoDatabase database = mongoClient.getDatabase("sample_mflix");
               MongoCollection<Document> collection = database.getCollection("movies");
               String indexName = "default";

               // Define the synonym mapping for the lucene.standard analyzer
               List<Document> synonymsList = List.of(
                       new Document("analyzer", "lucene.standard")
                               .append("name", "my_synonyms")
                               .append("source", new Document("collection", "synonymous_terms"))
               );

               // Create the MongoDB Search index definition
               Document searchIdx = new Document(
                       "mappings",
                       new Document("dynamic", true)
               ).append("synonyms", synonymsList);

               // Create the index by using the Java Sync Driver
               collection.createSearchIndex(indexName, searchIdx);
               System.out.println("New index name: " + indexName);
           } catch (Exception e) {
               System.err.println("Error creating Atlas Search index: " + e.getMessage());
           }
       }
   }
   ```

3. Compile and run the file to create the index.

   Compile and run your application in your IDE or your shell.

   ```shell
   javac CreateDynamicIndex.java
   java CreateDynamicIndex
   ```

   **Output:**

   ```text
   New index name: 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.

1. Set up your application.

   To learn how to install the driver and configure your Node.js application, see the [Get Started](https://www.mongodb.com/docs/drivers/node/current/get-started/) tutorial in the MongoDB Node.js Driver documentation.

2. Define the index.

   Create a `create-static-index.js` file in your project directory, and copy and paste the following code into the file.

   ```javascript
   const { MongoClient } = require("mongodb");

   // Replace the connection string with your MongoDB deployment's connection string
   const uri =  "<connection-string>";

   const client = new MongoClient(uri);

   async function run() {
     try {
       const database = client.db("sample_mflix");
       const collection = database.collection("movies");

       // Create the MongoDB Search index definition for the plot field with synonyms
       const index = {
           name: "default",
           definition: {
               "mappings": {
                   "dynamic": false,
                   "fields": {
                       "plot": {
                           "type": "string",
                           "analyzer": "lucene.english"
                       }
                   }
               },
               "synonyms": [
                   {
                       "analyzer": "lucene.english",
                       "name": "my_synonyms",
                       "source": {
                           "collection": "synonymous_terms"
                       }
                   }
               ]
           }
       }

       // Create the index
       const result = await collection.createSearchIndex(index);
       console.log("New index name: " + result);
     } finally {
       await client.close();
     }
   }

   run().catch(console.dir);
   ```

3. Create the index.

   ```shell
   node create-static-index.js
   ```

   **Output:**

   ```text
   New index name: 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.

1. Set up your application.

   To learn how to install the driver and configure your Node.js application, see the [Get Started](https://www.mongodb.com/docs/drivers/node/current/get-started/) tutorial in the MongoDB Node.js Driver documentation.

2. Define the index.

   Create a `create-dynamic-index.js` file in your project directory, and copy and paste the following code into the file.

   ```javascript
   const { MongoClient } = require("mongodb");

   // Replace the connection string with your MongoDB deployment's connection string
   const uri =  "<connection-string>";

   const client = new MongoClient(uri);

   async function run() {
     try {
       const database = client.db("sample_mflix");
       const collection = database.collection("movies");

       // Create the MongoDB Search index definition with dynamic mapping and synonyms
       const index = {
           name: "default",
           definition: {
               "mappings": {
                   "dynamic": true
               },
               "synonyms": [
                   {
                       "analyzer": "lucene.standard",
                       "name": "my_synonyms",
                       "source": {
                           "collection": "synonymous_terms"
                       }
                   }
               ]
           }
       }

       // Create the index
       const result = await collection.createSearchIndex(index);
       console.log("New index name: " + result);
     } finally {
       await client.close();
     }
   }

   run().catch(console.dir);
   ```

3. Create the index.

   ```shell
   node create-dynamic-index.js
   ```

   **Output:**

   ```text
   New index name: 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.

1. Set up your application.

   To learn how to install the driver and configure your Python application, see the [Get Started](https://www.mongodb.com/docs/drivers/python/pymongo-driver/current/get-started/) tutorial in the MongoDB Python Driver documentation.

2. Define the index.

   Create a `create-static-index.py` file in your project directory, and copy and paste the following code into the file.

   ```python
   from pymongo import MongoClient
   from pymongo.operations import SearchIndexModel

   # Connect to your Atlas deployment
   uri = "<connection-string>"
   client = MongoClient(uri)

   database = client["sample_mflix"]
   collection = database["movies"]

   # Create the MongoDB Search index definition for the plot field with synonyms
   search_index_model = SearchIndexModel(
       definition={
           "mappings": {
               "dynamic": False,
               "fields": {
                   "plot": {
                       "type": "string",
                       "analyzer": "lucene.english"
                   }
               }
           },
           "synonyms": [
               {
                   "analyzer": "lucene.english",
                   "name": "my_synonyms",
                   "source": {
                       "collection": "synonymous_terms"
                   }
               }
           ]
       },
       name="default",
   )

   # Create the index
   result = collection.create_search_index(model=search_index_model)
   print(f"New index name: {result}")
   ```

3. Create the index.

   ```shell
   python create-static-index.py
   ```

   **Output:**

   ```text
   New index name: 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.

1. Set up your application.

   To learn how to install the driver and configure your Python application, see the [Get Started](https://www.mongodb.com/docs/drivers/python/pymongo-driver/current/get-started/) tutorial in the MongoDB Python Driver documentation.

2. Define the index.

   Create a `create-dynamic-index.py` file in your project directory, and copy and paste the following code into the file.

   ```python
   from pymongo import MongoClient
   from pymongo.operations import SearchIndexModel

   # Connect to your Atlas deployment
   uri = "<connection-string>"
   client = MongoClient(uri)

   database = client["sample_mflix"]
   collection = database["movies"]

   # Create the MongoDB Search index definition
   search_index_model = SearchIndexModel(
       definition={
           "mappings": {
               "dynamic": True,
           },
           "synonyms": [
               {
                   "analyzer": "lucene.standard",
                   "name": "my_synonyms",
                   "source": {
                       "collection": "synonymous_terms"
                   }
               }
           ]
       },
       name="default",
   )

   # Create the index
   result = collection.create_search_index(model=search_index_model)
   print(f"New index name: {result}")
   ```

3. Create the index.

   ```shell
   python create-dynamic-index.py
   ```

   **Output:**

   ```text
   New index name: 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.

1. 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"
         }
       }
     ]
   }

   ```

2. Connect to the Atlas CLI

   In your terminal, connect to your local deployment from the Atlas CLI. To learn more, see [Connect from the Atlas CLI.](https://www.mongodb.com/docs/atlas/cli/current/connect-atlas-cli/)

3. Create a MongoDB Search index.

   Run the [atlas deployments search indexes create](https://www.mongodb.com/docs/atlas/cli/current/command/atlas-deployments-search-indexes-create/) command in your terminal, replacing `<path-to-file>` with the path to the `static-index.json` file:

   ```shell
   atlas deployments search indexes create --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.

1. 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"
         }
       }
     ]
   }

   ```

2. Connect to the Atlas CLI

   In your terminal, connect to your local deployment from the Atlas CLI. To learn more, see [Connect from the Atlas CLI.](https://www.mongodb.com/docs/atlas/cli/current/connect-atlas-cli/)

3. Create a MongoDB Search index.

   Run the [atlas deployments search indexes create](https://www.mongodb.com/docs/atlas/cli/current/command/atlas-deployments-search-indexes-create/) command in your terminal, replacing `<path-to-file>` with the path to the `dynamic-index.json` file:

   ```shell
   atlas deployments search indexes create --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.

1. Connect to your cluster using MongoDB Compass.

   Open [Compass](https://www.mongodb.com/try/download/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)

2. Specify the database and collection.

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

3. 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.

1. Connect to your cluster using MongoDB Compass.

   Open [Compass](https://www.mongodb.com/try/download/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)

2. Specify the database and collection.

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

3. 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.

1. 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.](https://www.mongodb.com/docs/mongodb-shell/connect/)

2. 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
   ```

3. 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.

1. 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.](https://www.mongodb.com/docs/mongodb-shell/connect/)

2. 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
   ```

3. 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.

1. Set up your application.

   To learn how to install the driver and configure your C application, see the [Get Started](https://www.mongodb.com/docs/languages/c/c-driver/current/get-started/) tutorial in the MongoDB C Driver documentation.

2. 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 <mongoc/mongoc.h>
   #include <stdio.h>

   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;
   }
   ```

3. 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.

1. Set up your application.

   To learn how to install the driver and configure your C application, see the [Get Started](https://www.mongodb.com/docs/languages/c/c-driver/current/get-started/) tutorial in the MongoDB C Driver documentation.

2. 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 <mongoc/mongoc.h>
   #include <stdio.h>

   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;
   }
   ```

3. 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.

1. Set up your application

   To learn how to install the driver and configure your C++ application, see the [Get Started](https://www.mongodb.com/docs/languages/cpp/cpp-driver/current/get-started/) tutorial in the MongoDB C++ Driver documentation.

2. 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("<connection-string>");
       mongocxx::client client(uri);

       auto db = client["sample_mflix"];
       auto collection = db["movies"];
       auto siv = collection.search_indexes();

       // Create the MongoDB Search index definition for the plot field with synonyms
       auto name = "default";
       auto synonyms = bsoncxx::builder::basic::make_array(
           make_document(
               kvp("analyzer", "lucene.english"),
               kvp("name", "my_synonyms"),
               kvp("source", make_document(
                   kvp("collection", "synonymous_terms")
               ))
           )
       );

       auto definition = make_document(
           kvp("mappings", make_document(
               kvp("dynamic", false),
               kvp("fields", make_document(
                   kvp("plot", make_document(
                       kvp("type", "string"),
                       kvp("analyzer", "lucene.english")
                   ))
               ))
           )),
           kvp("synonyms", synonyms)
       );
       auto model = mongocxx::search_index_model(name, definition.view());

       try {
           // Execute the command to create the index
           auto result = siv.create_one(model);
           std::cout << "New index name: " << result << std::endl;
       } catch (const std::exception& e) {
           std::cerr << "Error creating Atlas Search index: " << e.what() << std::endl;
       }

   }
   ```

3. Create the index.

   ```shell
   c++ --std=c++17 CreateStaticIndex.cpp $(pkg-config --cflags --libs libmongocxx) -o ./app.out
   ./app.out
   ```

   **Output:**

   ```text
   New index name: 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.

1. Set up your application

   To learn how to install the driver and configure your C++ application, see the [Get Started](https://www.mongodb.com/docs/languages/cpp/cpp-driver/current/get-started/) tutorial in the MongoDB C++ Driver documentation.

2. Define the index.

   Create a `CreateDynamicIndex.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("<connection-string>");
       mongocxx::client client(uri);

       auto db = client["sample_mflix"];
       auto collection = db["movies"];
       auto siv = collection.search_indexes();

       // Define the synonym mapping for the lucene.standard analyzer
       auto name = "default";
       auto synonyms = bsoncxx::builder::basic::make_array(
           make_document(
               kvp("analyzer", "lucene.standard"),
               kvp("name", "my_synonyms"),
               kvp("source", make_document(
                   kvp("collection", "synonymous_terms")
               ))
           )
       );

       // Create the MongoDB Search index definition
       auto definition = make_document(
           kvp("mappings", make_document(
               kvp("dynamic", true))
           ),
           kvp("synonyms", synonyms)
       );
       auto model = mongocxx::search_index_model(name, definition.view());

       try {
           // Execute the command to create the index
           auto result = siv.create_one(model);
           std::cout << "New index name: " << result << std::endl;
       } catch (const std::exception& e) {
           std::cerr << "Error creating Atlas Search index: " << e.what() << std::endl;
       }

   }
   ```

3. Create the index.

   ```shell
   c++ --std=c++17 CreateDynamicIndex.cpp $(pkg-config --cflags --libs libmongocxx) -o ./app.out
   ./app.out
   ```

   **Output:**

   ```text
   New index name: 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.

1. Set up your application.

   To learn how to install the driver and configure your Go application, see the [Get Started](https://www.mongodb.com/docs/drivers/go/current/get-started/) tutorial in the MongoDB Go Driver documentation.

2. Define the index.

   Create a `create-static-index.go` file in your project directory, and copy and paste the following code into the file.

   ```go
   package main

   import (
   	"context"
   	"fmt"
   	"log"

   	"go.mongodb.org/mongo-driver/v2/bson"
   	"go.mongodb.org/mongo-driver/v2/mongo"
   	"go.mongodb.org/mongo-driver/v2/mongo/options"
   )

   func main() {
   	ctx := context.Background()

   	// Replace the placeholder with your MongoDB deployment's connection string
   	uri := "<connection-string>"
   	client, err := mongo.Connect(options.Client().
   		ApplyURI(uri))
   	if err != nil {
   		panic(err)
   	}

   	defer func() {
   		if err := client.Disconnect(context.TODO()); err != nil {
   			panic(err)
   		}
   	}()

   	coll := client.Database("sample_mflix").Collection("movies")
   	const indexName = "default"
   	opts := options.SearchIndexes().SetName(indexName).SetType("search")

   	// Create the search index definition

   	searchIndexModel := mongo.SearchIndexModel{
   		Definition: bson.M{
   			"mappings": bson.M{
   				"dynamic": false,
   				"fields": bson.M{
   					"plot": bson.M{
   						"type":     "string",
   						"analyzer": "lucene.english",
   					},
   				},
   			},
   			"synonyms": []bson.M{
   				{
   					"analyzer": "lucene.english",
   					"name":     "my_synonyms",
   					"source": bson.M{
   						"collection": "synonymous_terms",
   					},
   				},
   			},
   		},
   		Options: opts,
   	}

   	// Create the index
   	searchIndexName, err := coll.SearchIndexes().CreateOne(ctx, searchIndexModel)
   	if err != nil {
   		log.Fatalf("Failed to create the Atlas Search index: %v", err)
   	}

   	fmt.Println("Name of index created: " + searchIndexName)
   }

   ```

3. Create the index.

   ```shell
   go run create_static_index.go
   ```

   **Output:**

   ```text
   New index name: 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.

1. Set up your application.

   To learn how to install the driver and configure your Go application, see the [Get Started](https://www.mongodb.com/docs/drivers/go/current/get-started/) tutorial in the MongoDB Go Driver documentation.

2. Define the index.

   Create a `create-dynamic-index.go` file in your project directory, and copy and paste the following code into the file.

   ```go
   package main

   import (
   	"context"
   	"fmt"
   	"log"

   	"go.mongodb.org/mongo-driver/v2/bson"
   	"go.mongodb.org/mongo-driver/v2/mongo"
   	"go.mongodb.org/mongo-driver/v2/mongo/options"
   )

   func main() {
   	ctx := context.Background()

   	// Replace the placeholder with your MongoDB deployment's connection string
   	uri := "<connection-string>"
   	client, err := mongo.Connect(options.Client().
   		ApplyURI(uri))
   	if err != nil {
   		panic(err)
   	}

   	defer func() {
   		if err := client.Disconnect(context.TODO()); err != nil {
   			panic(err)
   		}
   	}()

   	coll := client.Database("sample_mflix").Collection("movies")
   	const indexName = "default"
   	opts := options.SearchIndexes().SetName(indexName).SetType("search")

   	// Create the search index definition
   	searchIndexModel := mongo.SearchIndexModel{
   		Definition: bson.M{
   			"mappings": bson.M{
   				"dynamic": true,
   			},
   			"synonyms": []bson.M{
   				{
   					"analyzer": "lucene.standard",
   					"name":     "my_synonyms",
   					"source": bson.M{
   						"collection": "synonymous_terms",
   					},
   				},
   			},
   		},
   		Options: opts,
   	}

   	// Create the index
   	searchIndexName, err := coll.SearchIndexes().CreateOne(ctx, searchIndexModel)
   	if err != nil {
   		log.Fatalf("Failed to create the Atlas Search index: %v", err)
   	}

   	fmt.Println("Name of index created: " + searchIndexName)
   }

   ```

3. Create the index.

   ```shell
   go run create_dynamic_index.go
   ```

   **Output:**

   ```text
   New index name: 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.

1. Set up your application.

   To learn how to install the driver and configure your Java application, see the [Get Started](https://www.mongodb.com/docs/drivers/java/sync/current/get-started/) tutorial in the MongoDB Java Driver documentation.

2. Define the index.

   In your project's base package directory, create a `CreateStaticIndex.java` file and copy and paste the following code into this file.

   ```java
   import com.mongodb.client.*;
   import org.bson.Document;

   import java.util.List;

   public class CreateStaticIndex {
       public static void main(String[] args) {
           // Replace the placeholder with your MongoDB deployment's connection string
           String connectionString = "<connection-string>";

           try (MongoClient mongoClient = MongoClients.create(connectionString)) {
               MongoDatabase database = mongoClient.getDatabase("sample_mflix");
               MongoCollection<Document> collection = database.getCollection("movies");
               String indexName = "default";

               // Define the synonym mapping for the lucene.english analyzer
               List<Document> synonymsList = List.of(
                       new Document("analyzer", "lucene.english")
                               .append("name", "my_synonyms")
                               .append("source", new Document("collection", "synonymous_terms"))
               );

               // Create the MongoDB Search index definition for the plot field with synonyms
               Document searchIdx = new Document(
                       "mappings",
                       new Document("dynamic", false)
                               .append("fields",
                                       new Document("plot",
                                               new Document("type", "string")
                                                       .append("analyzer", "lucene.english")
                                       )
                               )
               ).append("synonyms", synonymsList);

               // Create the index by using the Java Sync Driver
               collection.createSearchIndex(indexName, searchIdx);
               System.out.println("New index name: " + indexName);
           } catch (Exception e) {
               System.err.println("Error creating Atlas Search index: " + e.getMessage());
           }
       }
   }
   ```

3. Compile and run the file to create the index.

   Compile and run your application in your IDE or your shell.

   ```shell
   javac CreateStaticIndex.java
   java CreateStaticIndex
   ```

   **Output:**

   ```text
   New index name: 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.

1. Set up your application.

   To learn how to install the driver and configure your Java application, see the [Get Started](https://www.mongodb.com/docs/drivers/java/sync/current/get-started/) tutorial in the MongoDB Java Driver documentation.

2. Define the index.

   In your project's base package directory, create a `CreateDynamicIndex.java` file and copy and paste the following code into this file.

   ```java
   import com.mongodb.client.*;
   import org.bson.Document;

   import java.util.List;

   public class CreateDynamicIndex {
       public static void main(String[] args) {
           // Replace the placeholder with your MongoDB deployment's connection string
           String connectionString = "<connection-string>";

           try (MongoClient mongoClient = MongoClients.create(connectionString)) {
               MongoDatabase database = mongoClient.getDatabase("sample_mflix");
               MongoCollection<Document> collection = database.getCollection("movies");
               String indexName = "default";

               // Define the synonym mapping for the lucene.standard analyzer
               List<Document> synonymsList = List.of(
                       new Document("analyzer", "lucene.standard")
                               .append("name", "my_synonyms")
                               .append("source", new Document("collection", "synonymous_terms"))
               );

               // Create the MongoDB Search index definition
               Document searchIdx = new Document(
                       "mappings",
                       new Document("dynamic", true)
               ).append("synonyms", synonymsList);

               // Create the index by using the Java Sync Driver
               collection.createSearchIndex(indexName, searchIdx);
               System.out.println("New index name: " + indexName);
           } catch (Exception e) {
               System.err.println("Error creating Atlas Search index: " + e.getMessage());
           }
       }
   }
   ```

3. Compile and run the file to create the index.

   Compile and run your application in your IDE or your shell.

   ```shell
   javac CreateDynamicIndex.java
   java CreateDynamicIndex
   ```

   **Output:**

   ```text
   New index name: 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.

1. Set up your application.

   To learn how to install the driver and configure your Node.js application, see the [Get Started](https://www.mongodb.com/docs/drivers/node/current/get-started/) tutorial in the MongoDB Node.js Driver documentation.

2. Define the index.

   Create a `create-static-index.js` file in your project directory, and copy and paste the following code into the file.

   ```javascript
   const { MongoClient } = require("mongodb");

   // Replace the connection string with your MongoDB deployment's connection string
   const uri =  "<connection-string>";

   const client = new MongoClient(uri);

   async function run() {
     try {
       const database = client.db("sample_mflix");
       const collection = database.collection("movies");

       // Create the MongoDB Search index definition for the plot field with synonyms
       const index = {
           name: "default",
           definition: {
               "mappings": {
                   "dynamic": false,
                   "fields": {
                       "plot": {
                           "type": "string",
                           "analyzer": "lucene.english"
                       }
                   }
               },
               "synonyms": [
                   {
                       "analyzer": "lucene.english",
                       "name": "my_synonyms",
                       "source": {
                           "collection": "synonymous_terms"
                       }
                   }
               ]
           }
       }

       // Create the index
       const result = await collection.createSearchIndex(index);
       console.log("New index name: " + result);
     } finally {
       await client.close();
     }
   }

   run().catch(console.dir);
   ```

3. Create the index.

   ```shell
   node create-static-index.js
   ```

   **Output:**

   ```text
   New index name: 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.

1. Set up your application.

   To learn how to install the driver and configure your Node.js application, see the [Get Started](https://www.mongodb.com/docs/drivers/node/current/get-started/) tutorial in the MongoDB Node.js Driver documentation.

2. Define the index.

   Create a `create-dynamic-index.js` file in your project directory, and copy and paste the following code into the file.

   ```javascript
   const { MongoClient } = require("mongodb");

   // Replace the connection string with your MongoDB deployment's connection string
   const uri =  "<connection-string>";

   const client = new MongoClient(uri);

   async function run() {
     try {
       const database = client.db("sample_mflix");
       const collection = database.collection("movies");

       // Create the MongoDB Search index definition with dynamic mapping and synonyms
       const index = {
           name: "default",
           definition: {
               "mappings": {
                   "dynamic": true
               },
               "synonyms": [
                   {
                       "analyzer": "lucene.standard",
                       "name": "my_synonyms",
                       "source": {
                           "collection": "synonymous_terms"
                       }
                   }
               ]
           }
       }

       // Create the index
       const result = await collection.createSearchIndex(index);
       console.log("New index name: " + result);
     } finally {
       await client.close();
     }
   }

   run().catch(console.dir);
   ```

3. Create the index.

   ```shell
   node create-dynamic-index.js
   ```

   **Output:**

   ```text
   New index name: 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.

1. Set up your application.

   To learn how to install the driver and configure your Python application, see the [Get Started](https://www.mongodb.com/docs/drivers/python/pymongo-driver/current/get-started/) tutorial in the MongoDB Python Driver documentation.

2. Define the index.

   Create a `create-static-index.py` file in your project directory, and copy and paste the following code into the file.

   ```python
   from pymongo import MongoClient
   from pymongo.operations import SearchIndexModel

   # Connect to your Atlas deployment
   uri = "<connection-string>"
   client = MongoClient(uri)

   database = client["sample_mflix"]
   collection = database["movies"]

   # Create the MongoDB Search index definition for the plot field with synonyms
   search_index_model = SearchIndexModel(
       definition={
           "mappings": {
               "dynamic": False,
               "fields": {
                   "plot": {
                       "type": "string",
                       "analyzer": "lucene.english"
                   }
               }
           },
           "synonyms": [
               {
                   "analyzer": "lucene.english",
                   "name": "my_synonyms",
                   "source": {
                       "collection": "synonymous_terms"
                   }
               }
           ]
       },
       name="default",
   )

   # Create the index
   result = collection.create_search_index(model=search_index_model)
   print(f"New index name: {result}")
   ```

3. Create the index.

   ```shell
   python create-static-index.py
   ```

   **Output:**

   ```text
   New index name: 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.

1. Set up your application.

   To learn how to install the driver and configure your Python application, see the [Get Started](https://www.mongodb.com/docs/drivers/python/pymongo-driver/current/get-started/) tutorial in the MongoDB Python Driver documentation.

2. Define the index.

   Create a `create-dynamic-index.py` file in your project directory, and copy and paste the following code into the file.

   ```python
   from pymongo import MongoClient
   from pymongo.operations import SearchIndexModel

   # Connect to your Atlas deployment
   uri = "<connection-string>"
   client = MongoClient(uri)

   database = client["sample_mflix"]
   collection = database["movies"]

   # Create the MongoDB Search index definition
   search_index_model = SearchIndexModel(
       definition={
           "mappings": {
               "dynamic": True,
           },
           "synonyms": [
               {
                   "analyzer": "lucene.standard",
                   "name": "my_synonyms",
                   "source": {
                       "collection": "synonymous_terms"
                   }
               }
           ]
       },
       name="default",
   )

   # Create the index
   result = collection.create_search_index(model=search_index_model)
   print(f"New index name: {result}")
   ```

3. Create the index.

   ```shell
   python create-dynamic-index.py
   ```

   **Output:**

   ```text
   New index name: 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.
