Skip to content

Commit c66b49b

Browse files
authored
DOCSP-45206 Atlas Search Index (#113)
* DOCSP-45206 Atlas Search Indexes * first draft * draft code * edits * using native api * edits * emphasize lines * RM typo * var rename * code spacing
1 parent db7bf96 commit c66b49b

File tree

3 files changed

+218
-1
lines changed

3 files changed

+218
-1
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
require 'mongo'
2+
3+
# Replace the placeholders with your credentials
4+
uri = "<connection string>"
5+
6+
# Sets the server_api field of the options object to Stable API version 1
7+
options = { server_api: { version: "1" }}
8+
9+
# Creates a new client and connect to the server
10+
client = Mongo::Client.new(uri, options)
11+
12+
database = client.use('sample_mflix')
13+
collection = database[:movies]
14+
15+
# start-create-search-index
16+
# Creates indexes on all dynamically indexable fields with a default index name
17+
collection.search_indexes.create_one(
18+
{ mappings: { dynamic: true } }
19+
)
20+
21+
# Creates an index on the specified field with the specified index name
22+
index_definition = {
23+
mappings: {
24+
dynamic: false,
25+
fields: {
26+
<field name>: { type: '<field type>' }
27+
}
28+
}
29+
}
30+
collection.search_indexes.create_one(index_definition, name: '<index name>')
31+
# end-create-search-index
32+
33+
# start-create-multiple-search-indexes
34+
index_spec_1 = {
35+
name: '<index 1 name>',
36+
definition: {
37+
mappings: {
38+
dynamic: false,
39+
fields: {
40+
<field name>: { type: '<field type>' }
41+
}
42+
}
43+
}
44+
}
45+
46+
index_spec_2 = {
47+
name: '<index 2 name>',
48+
definition: {
49+
mappings: {
50+
dynamic: false,
51+
fields: {
52+
<field name>: { type: '<field type>' }
53+
}
54+
}
55+
}
56+
}
57+
58+
collection.search_indexes.create_many([index_spec_1, index_spec_2])
59+
# end-create-multiple-search-indexes
60+
61+
# start-update-search-indexes
62+
updated_definition = {
63+
mappings: {
64+
dynamic: false,
65+
fields: { <updated field name>: { type: '<updated field type>' } }
66+
}
67+
}
68+
69+
# Specifies the index to update by using the index name
70+
collection.search_indexes.update_one(updated_definition, name: '<index name>')
71+
72+
# Specifies the index to update by using the index id
73+
collection.search_indexes.update_one(updated_definition, id: <index id>)
74+
# end-update-search-indexes
75+
76+
# start-drop-search-index
77+
# Specifies the index to delete by using the index name
78+
collection.search_indexes.drop_one(name: '<index name>')
79+
80+
# Specifies the index to delete by using the index id
81+
collection.search_indexes.drop_one(id: <index id>)
82+
# end-drop-search-index
83+
84+
# start-list-entire-spec
85+
puts collection.search_indexes.collect(&:to_json)
86+
# end-list-entire-spec
87+
88+
# start-list-certain-elements
89+
collection.search_indexes.each do |index_spec|
90+
p index_spec['id']
91+
p index_spec['name']
92+
p index_spec['status']
93+
p index_spec['queryable']
94+
p index_spec['latestDefinition']
95+
end
96+
# end-list-certain-elements

source/indexes.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Optimize Queries by Using Indexes
2525
Single Field </indexes/single-field-index>
2626
Compound </indexes/compound-index>
2727
Multikey </indexes/multikey-index>
28-
.. Atlas Search </indexes/atlas-search-index>
28+
Atlas Search </indexes/atlas-search-index>
2929

3030
Overview
3131
--------

source/indexes/atlas-search-index.txt

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
.. _ruby-atlas-search-index:
2+
3+
====================
4+
Atlas Search Indexes
5+
====================
6+
7+
.. contents:: On this page
8+
:local:
9+
:backlinks: none
10+
:depth: 1
11+
:class: singlecol
12+
13+
.. facet::
14+
:name: genre
15+
:values: reference
16+
17+
.. meta::
18+
:keywords: index, query, optimization, efficiency
19+
20+
Overview
21+
--------
22+
23+
:atlas:`Atlas Search </atlas-search>` enables you to perform full-text searches on
24+
collections hosted on MongoDB Atlas. With Atlas Search indexes, you can specify the
25+
behavior of the search and which fields to index.
26+
27+
You can call the following methods to manage you Atlas Search indexes:
28+
29+
- ``search_indexes#create_one``
30+
- ``search_indexes#create_many``
31+
- ``search_indexes#update_one``
32+
- ``search_indexes#drop_one``
33+
34+
The following sections provide code examples that demonstrate how to use
35+
each of the preceding commands.
36+
37+
.. _ruby-atlas-search-index-create:
38+
39+
Create a Search Index
40+
---------------------
41+
42+
To create one or more Atlas Search indexes, use the ``search_indexes#create_one``
43+
or the ``search_indexes#create_many`` method. Both methods return immediately,
44+
while the indexes are asynchronously created in the background.
45+
46+
The following code example shows how to create an Atlas Search index by providing
47+
an index definition and an optional name for the index:
48+
49+
.. literalinclude:: /includes/indexes/atlas-search-index.rb
50+
:language: ruby
51+
:start-after: start-create-search-index
52+
:end-before: end-create-search-index
53+
:emphasize-lines: 15
54+
55+
You can use ``search_indexes#create_many`` to create multiple Atlas Search indexes by
56+
providing an array of index specifications. Each index specification should include a definition
57+
key, which defines the index, and a name key to specify the index name. The following
58+
code example shows how to create multiple search indexes:
59+
60+
.. literalinclude:: /includes/indexes/atlas-search-index.rb
61+
:language: ruby
62+
:start-after: start-create-multiple-search-indexes
63+
:end-before: end-create-multiple-search-indexes
64+
:emphasize-lines: 25
65+
66+
For longer index definitions, it is helpful to define the index definitions outside
67+
of the method call. To learn more about the syntax of index definitions, see the
68+
:atlas:`Review Atlas Search Index Syntax </atlas-search/index-definitions>`
69+
guide in the Atlas manual.
70+
71+
Update a Search Index
72+
---------------------
73+
74+
To update an Atlas Search index, use the ``search_indexes#update_one`` method.
75+
76+
To update an index, you must provide a new index definition. You must specify
77+
the index you want to update by using either the ``name`` or ``id`` of the index.
78+
The following code shows how to update a search index:
79+
80+
.. literalinclude:: /includes/indexes/atlas-search-index.rb
81+
:language: ruby
82+
:start-after: start-update-search-indexes
83+
:end-before: end-update-search-indexes
84+
85+
Delete a Search Index
86+
---------------------
87+
88+
To delete an Atlas Search index, use the ``search_indexes#drop_one`` method.
89+
90+
To delete an index, you must provide the ``id`` or ``name`` of the index. The following
91+
code shows how to delete a search index from a collection:
92+
93+
.. literalinclude:: /includes/indexes/atlas-search-index.rb
94+
:language: ruby
95+
:start-after: start-drop-search-index
96+
:end-before: end-drop-search-index
97+
98+
List Search Indexes
99+
-------------------
100+
101+
You can use the ``search_indexes`` object to list the entire index specification
102+
of each index:
103+
104+
.. literalinclude:: /includes/indexes/atlas-search-index.rb
105+
:language: ruby
106+
:start-after: start-list-entire-spec
107+
:end-before: end-list-entire-spec
108+
109+
To list individual fields in the index specification for each index, iterate
110+
over the ``search_indexes`` object:
111+
112+
.. literalinclude:: /includes/indexes/atlas-search-index.rb
113+
:language: ruby
114+
:start-after: start-list-certain-elements
115+
:end-before: end-list-certain-elements
116+
117+
Additional Information
118+
----------------------
119+
120+
To learn more about MongoDB Atlas Search, see the
121+
:atlas:`Atlas Search </atlas-search/atlas-search-overview/>` documentation.

0 commit comments

Comments
 (0)