Skip to content

DOCSP-45206 Atlas Search Index #113

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions source/includes/indexes/atlas-search-index.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
require 'mongo'

# Replace the placeholders with your credentials
uri = "<connection string>"

# Sets the server_api field of the options object to Stable API version 1
options = { server_api: { version: "1" }}

# Creates a new client and connect to the server
client = Mongo::Client.new(uri, options)

database = client.use('sample_mflix')
collection = database[:movies]

# start-create-search-index
index_definition = {
name: '<index name>',
definition: {
mappings: {
dynamic: false,
fields: {
<field name 1>: {type: '<field type>'},
<field name 2>: {type: '<field type>'}
}
}
}
}
collection.database.command(
createSearchIndexes: '<collection name>',
indexes: [index_definition]
)
# end-create-search-index

# start-update-search-indexes
updated_definition = {
mappings: {
dynamic: false,
fields: { <updated field name>: { type: '<field type>' } }
}
}
collection.database.command(
updateSearchIndex: '<collection name>',
name: '<index name>',
definition: updated_definition
)
# end-update-search-indexes

# start-drop-search-index
collection.database.command(
dropSearchIndex: '<collection name>',
name: '<index name>'
)
# end-drop-search-index
2 changes: 1 addition & 1 deletion source/indexes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ Optimize Queries by Using Indexes
:maxdepth: 1

Single Field </indexes/single-field-index>
Atlas Search </indexes/atlas-search-index>
.. Compound </indexes/compound-index>
.. Multikey </indexes/multikey-index>
.. Atlas Search </indexes/atlas-search-index>

Overview
--------
Expand Down
113 changes: 113 additions & 0 deletions source/indexes/atlas-search-index.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
.. _ruby-atlas-search-index:

====================
Atlas Search Indexes
====================

.. contents:: On this page
:local:
:backlinks: none
:depth: 1
:class: singlecol

.. facet::
:name: genre
:values: reference

.. meta::
:keywords: index, query, optimization, efficiency

Overview
--------

:atlas:`Atlas Search </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 database commands to manage you Atlas Search indexes:

- ``createSearchIndexes``
- ``dropSearchIndex``
- ``updateSearchIndex``

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

.. _ruby-atlas-search-index-create:

Create a Search Index
---------------------

You can use the ``createSearchIndexes`` database command to create one or
more Atlas Search indexes.

The following code example shows how to create multiple Atlas Search indexes:

.. literalinclude:: /includes/indexes/atlas-search-index.rb
:language: ruby
:start-after: start-create-search-index
:end-before: end-create-search-index

To learn more about the syntax for the ``createSearchIndexes`` database command,
see the :manual:`createSearchIndexes </reference/command/createSearchIndexes/>`
reference in the {+mdb-server+} manual.

To learn more about the syntax for the index ``definition``, see the
:atlas:`Review Atlas Search Index Syntax </atlas-search/index-definitions>`
guide in the Atlas manual.

.. note:: Atlas Vector Search Indexes

You can also use the ``createSearchIndexes`` database command to create
Atlas Vector Search Indexes. Atlas Vector Search enables you to perform
semantic searches on vector embeddings stored in MongoDB Atlas. To learn
more about this feature, see the :atlas:`Atlas Vector Search Overview
</atlas-vector-search/vector-search-overview/>`.

Update a Search Index
---------------------

You can use the ``updateSearchIndex`` database command to update an Atlas Search index.

To update an index, you must provide a new index ``definition``. The
following code shows how to update a search index:

.. literalinclude:: /includes/indexes/atlas-search-index.rb
:language: ruby
:start-after: start-update-search-indexes
:end-before: end-update-search-indexes

To learn more about the syntax for the ``updateSearchIndex`` database command,
see the :manual:`updateSearchIndex </reference/command/updateSearchIndex/>`
reference in the {+mdb-server+} manual.

Delete a Search Index
---------------------

You can use the ``dropSearchIndex`` database command to delete an Atlas Search index.

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

.. literalinclude:: /includes/indexes/atlas-search-index.rb
:language: ruby
:start-after: start-drop-search-index
:end-before: end-drop-search-index

To learn more about the syntax for the ``dropSearchIndex`` database command,
see the :manual:`dropSearchIndex </reference/command/dropSearchIndex/>`
reference in the {+mdb-server+} manual.

Additional Information
----------------------

To learn more about MongoDB Atlas Search, see the
:atlas:`Atlas Search </atlas-search/atlas-search-overview/>` documentation.

API Documentation
~~~~~~~~~~~~~~~~~

To learn more about the {+driver-short+} methods used in this guide, see the following API documentation:

- `database <{+api-root+}/Mongo/Collection.html#database-instance_method>`__
- `command <{+api-root+}/Mongo/Database.html#command-instance_method>`__
Loading