Docs Menu
Docs Home
/ / /
Ruby MongoDB Driver
/

Atlas Search Indexes

On this page

  • Overview
  • Create a Search Index
  • Update a Search Index
  • Delete a Search Index
  • List Search Indexes
  • Additional Information
  • API Documentation

Atlas Search enables you to perform full-text searches on collections hosted on MongoDB Atlas. With Atlas Search indexes, you can specify the behavior of the search and which fields to index.

You can call the following methods to manage you Atlas Search indexes:

  • search_indexes#create_one

  • search_indexes#create_many

  • search_indexes#update_one

  • search_indexes#drop_one

The following sections provide code examples that demonstrate how to use each of the preceding commands.

To create one or more Atlas Search indexes, use the search_indexes#create_one or the search_indexes#create_many method. Both methods return immediately, while the indexes are asynchronously created in the background.

The following code example shows how to create an Atlas Search index by providing an index definition and an optional name for the index:

# Creates indexes on all dynamically indexable fields with a default index name
collection.search_indexes.create_one(
{ mappings: { dynamic: true } }
)
# Creates an index on the specified field with the specified index name
index_definition = {
mappings: {
dynamic: false,
fields: {
<field name>: { type: '<field type>' }
}
}
}
collection.search_indexes.create_one(index_definition, name: '<index name>')

You can use search_indexes#create_many to create multiple Atlas Search indexes by providing an array of index specifications. Each index specification should include a definition key, which defines the index, and a name key to specify the index name. The following code example shows how to create multiple search indexes:

index_spec_1 = {
name: '<index 1 name>',
definition: {
mappings: {
dynamic: false,
fields: {
<field name>: { type: '<field type>' }
}
}
}
}
index_spec_2 = {
name: '<index 2 name>',
definition: {
mappings: {
dynamic: false,
fields: {
<field name>: { type: '<field type>' }
}
}
}
}
collection.search_indexes.create_many([index_spec_1, index_spec_2])

For longer index definitions, it is helpful to define the index definitions outside of the method call. To learn more about the syntax of index definitions, see the Review Atlas Search Index Syntax guide in the Atlas manual.

To update an Atlas Search index, use the search_indexes#update_one method.

To update an index, you must provide a new index definition. You must specify the index you want to update by using either the name or id of the index. The following code shows how to update a search index:

updated_definition = {
mappings: {
dynamic: false,
fields: { <updated field name>: { type: '<updated field type>' } }
}
}
# Specifies the index to update by using the index name
collection.search_indexes.update_one(updated_definition, name: '<index name>')
# Specifies the index to update by using the index id
collection.search_indexes.update_one(updated_definition, id: <index id>)

To delete an Atlas Search index, use the search_indexes#drop_one method.

To delete an index, you must provide the id or name of the index. The following code shows how to delete a search index from a collection:

# Specifies the index to delete by using the index name
collection.search_indexes.drop_one(name: '<index name>')
# Specifies the index to delete by using the index id
collection.search_indexes.drop_one(id: <index id>)

You can use the search_indexes object to list the entire index specification of each index:

puts collection.search_indexes.collect(&:to_json)

To list individual fields in the index specification for each index, iterate over the search_indexes object:

collection.search_indexes.each do |index_spec|
p index_spec['id']
p index_spec['name']
p index_spec['status']
p index_spec['queryable']
p index_spec['latestDefinition']
end

To learn more about MongoDB Atlas Search, see the Atlas Search documentation.

To learn more about any of the methods discussed in this guide, see the following API documentation:

Back

Multikey