Overview
In this guide, you can learn how to programmatically manage your MongoDB Search and MongoDB Vector Search indexes by using the Ruby driver.
The MongoDB Search feature enables you to perform full-text searches on collections hosted on MongoDB Atlas. To learn more about MongoDB Search, see the MongoDB Search Overview.
MongoDB Vector Search enables you to perform semantic searches on vector embeddings stored in MongoDB Atlas. To learn more about MongoDB Vector Search, see the MongoDB Vector Search Overview.
You can call the following methods to manage your MongoDB Search and MongoDB Vector Search indexes:
search_indexes#create_one
search_indexes#create_many
search_indexes#update_one
search_indexes#drop_one
Note
MongoDB Search and MongoDB Vector Search Index Management is Asynchronous
The Ruby driver manages MongoDB Search and MongoDB 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 MongoDB Search or MongoDB 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 a MongoDB 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 a MongoDB Search index if you do not
pass a type
parameter. To create a MongoDB 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 MongoDB Search
or MongoDB 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 both MongoDB Search and MongoDB 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 MongoDB Search Index Syntax or the How to Index Fields for MongoDB Vector Search guides in the Atlas manual.
Update a Search Index
To update a MongoDB Search or MongoDB 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 a MongoDB 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 a MongoDB Search or MongoDB 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 MongoDB Search and MongoDB 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 Search, see the MongoDB Search documentation.
To learn more about MongoDB Vector Search, see the Ruby driver's Run a MongoDB Vector Search Query guide or the MongoDB Vector Search documentation.
API Documentation
To learn more about any of the methods discussed in this guide, see the following API documentation: