Edit an Atlas Search Index
On this page
You can change the index definition of an existing Atlas Search index. You cannot rename an index; if you need to change an index's name, you must create a new index and delete the old one.
Required Access
The following table shows the modes of access each role supports.
Role | Action | Atlas UI | Atlas API | Atlas Search API | Atlas CLI |
---|---|---|---|---|---|
Project Data Access Read Only or higher role | To view Atlas Search analyzers and indexes. | ✓ | ✓ | ||
Project Data Access Admin or higher role | To create and manage Atlas Search analyzers and indexes, and
assign the role to your API Key. | ✓ | ✓ | ✓ | ✓ |
Project Owner role | ✓ | ✓ | |||
Organization Owner role | To create access list entries for your API
Key and send the request from a client that appears in the
access list for your API Key. | ✓ | ✓ | ||
To create, view, edit, and delete Atlas Search indexes using the
Atlas UI or API. | ✓ | ✓ | ✓ |
Edit an Atlas Search Index
You can edit an Atlas Search index in the Atlas UI, or programmatically by
using mongosh
, the Atlas CLI, the API, or a supported
MongoDB Driver in your preferred language.
Note
You must have at least the
atlasAdmin
role or dbAdmin
access to
the database that contains the indexes. To learn more,
see Built-in Roles or Specific Privileges.
➤ Use the Select your language drop-down menu to set the language of the example in this section.
To edit an Atlas Search index through the API:
Send a PATCH
request.
Send a PATCH
request with either the unique ID or name
of the Atlas Search index that you wish to modify to the
search/indexes/
endpoint.
curl --user "{PUBLIC-KEY}:{PRIVATE-KEY}" --digest \ --header "Accept: application/json" \ --header "Content-Type: application/json" \ --include \ --request PATCH "https://cloud.mongodb.com/api/atlas/v2/groups/{groupId}/clusters/{clusterName}/search/indexes/{indexId}" \ --data ' { "definition": { "analyzer": "lucene.<analyzer>", "analyzers": [ { "charFilters": [ ... ], "name": "string", "tokenFilters": [ ... ], "tokenizer": { ... } } ], "mappings": { "dynamic": true | false, "fields": { "property1": {}, ... } }, "searchAnalyzer": "lucene.<analyzer>", "storedSource": { "include | exclude": [...] }, "synonyms": [ { "analyzer": "lucene.<analyzer>", "name": "string", "source": { "collection": "string" } } ] } }'
To learn more about the syntax and parameters for either endpoint, see Update One By Name and Update One By ID.
To update a search index for a cluster using the Atlas CLI, run the following command:
atlas clusters search indexes update <indexId> [options]
To learn more about the command syntax and parameters, see the Atlas CLI documentation for atlas clusters search indexes update.
Tip
See: Related Links
In Atlas, go to the Clusters page for your project.
If it is not already displayed, select the organization that contains your desired project from the Organizations menu in the navigation bar.
If it is not already displayed, select your desired project from the Projects menu in the navigation bar.
If the Clusters page is not already displayed, click Database in the sidebar.
Go to the Atlas Search page for your cluster.
You can go the Atlas Search page from the sidebar, the Data Explorer, or your cluster details page.
In the sidebar, click Atlas Search under the Services heading.
From the Select data source dropdown, select your cluster and click Go to Atlas Search.
Click the Browse Collections button for your cluster.
Expand the database and select the collection.
Click the Search Indexes tab for the collection.
Click the cluster's name.
Click the Atlas Search tab.
Review current configuration settings and edit them as needed.
Review the following index configuration settings:
Field Name | Description | Necessity |
---|---|---|
Index Analyzer | Optional | |
Query Analyzer | Specifies the analyzer to apply to query text before searching the text. If you omit this field, the index inherits an analyzer by default in the following order:
Corresponds to the | Optional |
Dynamic Mapping | Specify dynamic or static mapping of fields. To disable dynamic
mapping, set Corresponds to the | Required |
Review the following advanced configuration settings:
Field Name | Description | Necessity |
---|---|---|
Field Mappings | Required if Dynamic Mapping in the Index Configurations section is disabled. NoteDefining your own field mappings is recommended for advanced users only. Specify the fields to index. To add the fields, you must do the following:
NoteUnlike compound indexes, the order of fields in the Atlas Search index definition is not important. Fields can be defined in any order. Corresponds to the | Conditional |
Stored Source Fields | Specify the fields to store on Atlas Search. You can choose one of the following:
To learn more about storing fields, see Define Stored Source Fields in Your Atlas Search Index. Corresponds to the | Optional |
Synonyms Mappings | Specify synonym mappings to use in your index. To learn more, see Define Synonym Mappings in Your Atlas Search Index. To add synonym mappings, you must specify the following settings for each synonym:
Corresponds to the | Optional. |
Review the following index configuration settings:
Field Name | Description | Necessity |
---|---|---|
analyzer | Optional | |
searchAnalyzer | Specifies the analyzer to apply to query text before searching the text. If you omit this field, the index inherits an analyzer by default in the following order: | Optional |
mappings.dynamic | Specify dynamic or static mapping of fields. To disable dynamic
mapping, set | Required |
Review the following advanced configuration settings:
Field Name | Description | Necessity |
---|---|---|
mappings.fields | Required if NoteDefining your own field mappings is recommended for advanced users only. Specify the fields that you would like to index. To learn more, see Define Field Mappings. NoteUnlike compound indexes, the order of fields in the Atlas Search index definition is not important. Fields can be defined in any order. | Conditional |
storedSource | Note
Specify fields in the documents to store on Atlas Search. Value can be one of the following:
If omitted, defaults to NoteYou can store fields of all supported Data Types on Atlas Search. | Optional |
synonyms | Specify synonym mappings to use in your index. To learn more, see Define Synonym Mappings in Your Atlas Search Index. NoteYou can use a synonym mapping to query only fields analyzed with the
same analyzer. By default, Atlas Search uses the standard analyzer ( | Optional |
To learn more about these index definition settings, see Review Atlas Search Index Syntax.
Click Save to apply the changes.
The index's status changes from Active to Building. In this state, you can continue to use the old index because Atlas Search does not delete the old index until the updated index is ready for use. Once the status returns to Active, the modified index is ready to use.
To edit an Atlas Search index through mongosh
, use
the db.collection.updateSearchIndex()
method.
The command has the following syntax. Specify the name of the index that you want to edit and define the new index definition. This definition replaces the index's existing definition. To learn more, see Review Atlas Search Index Syntax.
db.<collection>.updateSearchIndex( "<index-name>", /* updated search index definition */ )
Example
The following command updates a search
index named default
from the movies
collection
to use static mappings:
db.movies.updateSearchIndex( "default", { "mappings": { "dynamic": false, "fields": { "<field-name>": { "type": "<field-type>" } } } )
Note
The db.collection.updateSearchIndex()
command doesn't
return an output. You can use the Atlas UI to view the
index status.
To use the C Driver to edit your Atlas Search indexes, specify
the updated index information in your application and call the
mongoc_collection_command_simple()
method.
Example
Copy the following code example into the file.
The following sample application specifies the updateSearchIndex
command, an updated index definition, and an existing index name. Then,
the application converts the command and updated index information to BSON
and passes this information to the mongoc_collection_command_simple()
method to edit the search index.
int main (void) { mongoc_client_t *client = NULL; mongoc_collection_t *collection = NULL; mongoc_database_t *database = NULL; bson_error_t error; bson_t cmd = BSON_INITIALIZER; bool ok = true; mongoc_init(); // Connect to your Atlas deployment client = mongoc_client_new("<connectionString>"); if (!client) { fprintf(stderr, "Failed to create a MongoDB client.\n"); ok = false; goto cleanup; } // Access your database and collection database = mongoc_client_get_database(client, "<databaseName>"); collection = mongoc_database_get_collection(database, "<collectionName>"); // Specify the command and the updated index definition const char *cmd_str = BSON_STR({ "updateSearchIndex" : "<collectionName>", "definition" : {"mappings" : {"dynamic" : true}}, "name" : "<indexName>" }); // Convert your command to BSON if (!bson_init_from_json(&cmd, cmd_str, -1, &error)) { fprintf(stderr, "Failed to parse command: %s\n", error.message); ok = false; goto cleanup; } // Update the Atlas search index by running the command if (!mongoc_collection_command_simple (collection, &cmd, NULL, NULL, &error)) { fprintf(stderr, "Failed to run updateSearchIndex: %s\n", error.message); ok = false; goto cleanup; } printf ("Index updated!\n"); cleanup: mongoc_collection_destroy (collection); mongoc_database_destroy (database); mongoc_client_destroy (client); bson_destroy (&cmd); mongoc_cleanup (); return ok ? EXIT_SUCCESS : EXIT_FAILURE; }
Specify the following values and save the file.
Your Atlas connection string. To learn more, see Connect via Drivers.
The database and collection for which you want to update an index.
The name of the index that you want to update.
The fields to redefine your search index. To learn more, see Review Atlas Search Index Syntax.
To use the C++ Driver to edit an Atlas Search index, call the
update_one()
method on a search index view.
Example
Copy the following code example into the file.
The following sample application instantiates a search index view and specifies
a new Atlas Search index definition. Then, the application passes this definition and
an existing index name to the update_one()
method, which updates the existing
index to reflect the new definition document.
using namespace mongocxx; using bsoncxx::builder::basic::make_document; int main() { mongocxx::instance instance{}; try { // Connect to your Atlas deployment mongocxx::uri uri("<connectionString>"); mongocxx::client client(uri); // Access your database and collection auto db = client["<databaseName>"]; auto collection = db["<collectionName>"]; // Access the indexes in your collection auto siv = collection.search_indexes(); // Specify a new definiton and update your search index auto newDefinition = make_document(kvp("mappings", make_document(kvp("dynamic", true)))); siv.update_one("<indexName>", newDefinition.view()); } catch (const std::exception& e) { std::cout<< "Exception: " << e.what() << std::endl; } return 0; }
Specify the following values and save the file.
Your Atlas connection string. To learn more, see Connect via Drivers.
The database and collection for which you want to update an index.
The name of the index that you want to update.
The fields to redefine your search index. To learn more, see Review Atlas Search Index Syntax.
To use the .NET/C# Driver to edit an Atlas Search index,
use the Update()
or UpdateAsync()
method.
Example
The following sample application updates an existing index definition. Specify the following values:
Your Atlas connection string. To learn more, see Connect via Drivers.
The database and collection where you created the search index.
The new index definition to replace the existing definition. In the example, you update an index to use static mappings. You can alter this definition to suit your specific indexing needs. To learn more, see Review Atlas Search Index Syntax.
The name of the index that you want to update.
using MongoDB.Bson; using MongoDB.Driver; // connect to your Atlas deployment var uri = "<connection-string>"; var client = new MongoClient(uri); var db = client.GetDatabase("<databaseName>"); var collection = db.GetCollection<BsonDocument>("<collectionName"); // define your Atlas Search index var index = new BsonDocument { // updated search index definition { "mappings", new BsonDocument { { "dynamic", false }, { "fields", new BsonDocument { { "<field-name>", new BsonDocument { { "type", "<field-type>" } } } } } } } }; collection.SearchIndexes.Update("<index-name>", index);
To run the sample application, create a new .NET console project named
csharp-update-index
and copy the previous code example into the
Program.cs
file. Then, use the following command to run the project:
dotnet run csharp-update-index.csproj
Note
The Update()
method doesn't return an output.
You can use the Atlas UI to view the
index status.
Tip
API Documentation
To learn more about the methods on this page, see the API documentation for the .NET/C# driver.
To edit an Atlas Search index on a collection using the Java
Driver,
construct a document that modifies the search index settings, and
then pass the document to the updateSearchIndex()
method. You
must have Java Driver v4.11.0 or
higher.
Copy the following code example into the file.
1 import com.mongodb.client.MongoClient; 2 import com.mongodb.client.MongoClients; 3 import com.mongodb.client.MongoCollection; 4 import com.mongodb.client.MongoDatabase; 5 import org.bson.Document; 6 7 public class EditIndex { 8 public static void main(String[] args) { 9 // connect to your Atlas cluster 10 String uri = "<connection-string>"; 11 12 try (MongoClient mongoClient = MongoClients.create(uri)) { 13 // set namespace 14 MongoDatabase database = mongoClient.getDatabase("<document-name>"); 15 MongoCollection<Document> collection = database.getCollection("<collection-name>"); 16 // define field mappings 17 Document index = new Document("analyzer", "<analyzer-name>").append( 18 "mappings", new Document("dynamic", <true|false>) 19 .append("fields", new Document("<field-name>", 20 new Document("type", "<field-type>")))); 21 // run the updateSearchIndex() method 22 collection.updateSearchIndex("<index-name>", index); 23 } 24 } 25 }
Replace the following values in the code and then save the file.
<connection-string>
- Your Atlas connection string. To learn more, see Connect via Drivers.Note
In your connection string, don't include the writeConcern setting.
<database-name>
- The name of the database that contains the collection.<collection-name>
- The name of the collection for which you want to retrieve the index.<analyzer-name>
- The name of the analyzer.dynamic
- The flag that indicates whether or not to automatically index the fields.<field-name>
- The name of the field to index.<field-type>
- The field data type.<index-name>
- The name of the index.
Compile and run the file.
javac EditIndex.java java EditIndex
The updateSearchIndex()
method runs asynchronously. Use the
listSearchIndexes()
method to determine if the changes have
been applied to your index. To learn more about retrieving Atlas Search
indexes, see View an Atlas Search Index.
Tip
See also:
To edit an Atlas Search index through the Node Driver,
use the updateSearchIndex
helper method.
Example
You can use the following sample application named update-index.js
to update an existing index definition. Specify the following values:
Your Atlas connection string. To learn more, see Connect via Drivers.
The database and collection where you created the search index.
The new index definition to replace the existing definition. In the example, you update an index to use static mappings. You can alter this definition to suit your specific indexing needs. To learn more, see Review Atlas Search Index Syntax.
The name of the index that you want to update.
import { MongoClient } from "mongodb"; // connect to your Atlas deployment const uri = "<connection-string>"; const client = new MongoClient(uri); async function run() { try { const database = client.db("<databaseName>"); const collection = database.collection("<collectionName>"); // define your Atlas Search index const index = { /* updated search index definition */ "mappings": { "dynamic": false, "fields": { "<field-name>": { "type": "<field-type>" } } } } // run the helper method await collection.updateSearchIndex("<index-name>", index); } finally { await client.close(); } } run().catch(console.dir);
To run the sample application, use the following command:
node update-index.js
Note
The updateSearchIndex
method doesn't return an output.
You can use the Atlas UI to view the
index status.
To use the Python Driver to update your Atlas Search
indexes, call the update_search_index()
method on your collection.
Example
Copy the following code example into the file.
The following sample application specifies a new Atlas Search index definition.
Then, the application passes this definition and an existing index name to
the update_search_index()
method, which updates the existing index to
reflect the new definition document.
from pymongo.mongo_client import MongoClient def edit_index(): # Connect to your Atlas deployment uri = "<connectionString>" client = MongoClient(uri) # Access your database and collection database = client["<databaseName>"] collection = database["<collectionName>"] # Specify a new index definition definition = { "mappings": { "dynamic": True }, } # Update your search index collection.update_search_index("<indexName>", definition)
Specify the following values and save the file.
Your Atlas connection string. To learn more, see Connect via Drivers.
The database and collection for which you want to update an index.
The name of the index that you want to update.
The fields to redefine your search index. To learn more, see Review Atlas Search Index Syntax.