Overview
In this guide, you can learn how to programmatically manage your Atlas Search and Atlas Vector Search indexes by using the Ruby driver.
The Atlas Search feature enables you to perform full-text searches on collections hosted on MongoDB Atlas. To learn more about Atlas Search, see the Atlas Search Overview.
Atlas Vector Search enables you to perform semantic searches on vector embeddings stored in MongoDB Atlas. To learn more about Atlas Vector Search, see the Atlas Vector Search Overview.
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
Note
Atlas Search and Vector Search Index Management is Asynchronous
The Ruby driver manages Atlas Search and Vector Search indexes asynchronously. The methods described in the following sections return the server response immediately, but the changes to your Search indexes take place in the background and might not complete until some time later.
The following sections provide code examples that demonstrate how to use each of the preceding commands.
Create a Search Index
To create a single Atlas Search or Vector Search index, use the
search_indexes#create_one
method. To create multiple indexes, use 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: { fullplot: { type: 'string' } } } } collection.search_indexes.create_one(index_definition, name: 'mySearchIndex')
Note
By default, the driver creates an Atlas Search index if you do not
pass a type
parameter. To create a Vector Search index, you must
set the type
parameter to 'vectorSearch'
when calling
create_one
.
You can use search_indexes#create_many
to create multiple Atlas
Search or Vector Search indexes by providing an array of index
specifications. Each index specification should include the following
components:
definition
parameter: Defines the indexname
parameter: Specifies the index nametype
parameter: Specifies the type of index ('search'
or'vectorSearch'
)
The following code example shows how to create Atlas Search and Vector Search indexes in one call:
index_spec_1 = { name: 'searchIndex_plot', type: 'search', definition: { mappings: { dynamic: false, fields: { plot: { type: 'string' } } } } } index_spec_2 = { name: 'vsIndex_plot_embedding', type: 'vectorSearch', definition: { fields: [ { type: "vector", path: "plot_embedding", numDimensions: 1536, similarity: "dotProduct" } ] } } 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.
Update a Search Index
To update an Atlas Search or Vector 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 an Atlas Search index:
updated_definition = { mappings: { dynamic: false, fields: { fullplot: { type: 'string' } } } } # Specifies the index to update by using the index name collection.search_indexes.update_one(updated_definition, name: 'searchIndex_plot') # Specifies the index to update by using the index id collection.search_indexes.update_one(updated_definition, id: <index id>)
Delete a Search Index
To delete an Atlas Search or Vector 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: 'searchIndex_plot') # Specifies the index to delete by using the index id collection.search_indexes.drop_one(id: <index id>)
List Search Indexes
You can use the search_indexes
object to list the entire index
specification of each Atlas Search and Vector Search index on a
collection:
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
Additional Information
To learn more about MongoDB Atlas Search, see the Atlas Search documentation.
API Documentation
To learn more about any of the methods discussed in this guide, see the following API documentation: