Skip to content

Commit a544cae

Browse files
authored
(DOCSP-40808) Indexes > Atlas search indexes (#64) (#72)
* (DOCSP-40808) Added atlas page based on PHP docs. * (DOCSP-40808) Replace php command names with cpp * (DOCSP-40808) Edits. * (DOCSP-40808) Added copyable to examples in replace page, and edited example for atlas search indexes. * (DOCSP-40808) Edits. * (DOCSP-40808) Edits. * (DOCSP-40808) Edits. * (DOCSP-40808) Edits. * (DOCSP-40808) Edits. * (DOCSP-40808) Edits. * (DOCSP-40808) Edits. * (DOCSP-408088) Added multiple index example. * (DOCSP-40808) Pull changes to compound and single indexes, add changes to index code examples file. * (DOCSP-40808) Build error fixes. * (DOCSP-40808) Build error fixes. * (DOCSP-40808) Edits. * (DOCSP-40808) Reorganizing section introductions. * (DOCSP-40808) Reorganizing section introductions. * (DOCSP-40808) edits. * (DOCSP-40808) Added example for listing one search index. * (DOCSP-40808) Add examples for delete and update a search index. * (DOCPS-40808) Fixing build errors. * (DOCSP-40808) Reorganizing text. * (DOCSP-40808) Reorganizing text. * (DOCSP-40808) Reorganizing text. * (DOCSP-40808) Added API links * (DOCSP-40808) Output edit. * (DOCSP-40808) Updates. * (DOCSP-40808) Updates. * (DOCSP-40808) Fix page reference. * (DOCSP-40808) Breaking up the create an index section and adding text to indexes landing page. * (DOCSP-40808) Added code examples to indexes landing page. * (DOCSP-40808) Note to tip * (DOCSP-40808) De-wordi-fication. * (DOCSP-40808) Fix reference. * (DOCSP-40808) Replace delete with remove * (DOCSP-40808) Added note. * (DOCSP-40808) Add new links and reorganize. * (DOCSP-40808) Add new links and reorganize. * (DOCSP-40808) Added code comment. * (DOCSP-40808) Standardizing directions. * (DOCSP-40808) range based vs iterator * (DOCSP-40808) range-based * (DOCSP-40808) Edits. * (DOCSP-40808) Simplify the list indexes. * (DOCSP-40808) Error fix. * (DOCSP-40808) Note to important. * (DOCSP-40808) Edits. * (DOCSP-40808) Editing output. * (DOCSP-40808) Editing output. * (DOCSP-40808) Editing output. * (DOCSP-40808) Editing output. * (DOCSP-40808) Make output consistent. * (DOCSP-40808) Fix updated code. * (DOCSP-40808) Remove comments. * (DOCSP-40808) Fix build errors. * (DOCSP-40808) Edits. * (DOCSP-40808) Edits. * (DOCSP-40808) Fix code examples. * (DOCSP-40808) Edits. * (DOCSP-40808) Remove create multiple search indexes * (DOCSP-40808) Final edits. * (DOCSP-40808) @mongoKart Review changes. * (DOCSP-40808) @mongoKart Review edits: Adding more context to create a search index. * (DOCSP-40808) @mongoKart Review changes: more context for create a single dynamic index. * (DOCSP-40808) @mongoKart Replace all 'search index' with 'atlas search index' * (DOCSP-40808) @mongoKart Addressing feedback for create an index. * (DOCSP-40808) @mongoKart addressing feedback. * (DOCSP-40808) Addressing review feedback. * (DOCSP-40808) @mongoKart Feedback changes. * (DOCSP-40808) Removed text about Atlas Search Queries because it is covered in the overview. * (DOCSP-40808) @mongoKart Editing directions to create a new index. * (DOCSP-40808) Edit. * (DOCSP-40808) Link error fix. * (DOCSP-40808) @mongoKart reorganizing the create_one() directions. * (DOCSP-40808) Fix formatting in bulleted list. * (DOCSP-40808) Typo. * (DOCSP-40808) @mongoKart Addressing feedback. * (DOCSP-40808) @mongoKart Addressing feedback. * (DOCSP-40808) Fix ambiguous wording. * (DOCSP-40808) @kevinAlbs Review changes.
1 parent 1e1d88e commit a544cae

File tree

7 files changed

+555
-22
lines changed

7 files changed

+555
-22
lines changed

source/includes/indexes/indexes.cpp

+89
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ int main(){
2121
auto collection = db["movies"];
2222
// end-db-coll
2323

24+
// start-siv
25+
auto siv = collection.search_indexes();
26+
// end-siv
2427
{
2528
// start-index-single
2629
auto index_specification = make_document(kvp("title", 1));
@@ -65,5 +68,91 @@ int main(){
6568
collection.indexes().drop_one("*");
6669
// end-remove-all-wildcard
6770
}
71+
{
72+
// start-create-static-search-index
73+
// Create an index model with your index name and definition containing the fields you want to index
74+
auto name = "myStaticIndex";
75+
auto fields = make_document(kvp("title", make_document(kvp("type", "string"), kvp("analyzer","lucene.standard"))), kvp("year", make_document(kvp("type","number"))));
76+
auto definition = make_document(kvp("mappings", make_document(kvp("dynamic", false), kvp("fields", fields))));
77+
auto model = mongocxx::search_index_model(name, definition.view());
78+
79+
// Create the search index
80+
auto result = siv.create_one(model);
81+
std::cout << "New index name: " << result << std::endl;
82+
// end-create-static-search-index
83+
}
84+
{
85+
// start-create-dynamic-search-index
86+
// Create an index model with your index name and definition
87+
auto name = "myDynamicIndex";
88+
auto definition = make_document(kvp("mappings", make_document(kvp("dynamic", true))));
89+
auto model = mongocxx::search_index_model(name, definition.view());
90+
91+
// Create the search index
92+
auto result = siv.create_one(model);
93+
std::cout << "New index name: " << result << std::endl;
94+
// end-create-dynamic-search-index
95+
}
96+
{
97+
// start-create-multiple-search-indexes
98+
// Create a vector to store Search index models
99+
std::vector<mongocxx::search_index_model> models;
100+
101+
// Add an index model with dynamic mappings to the input vector
102+
auto name_1 = "myDynamicIndex";
103+
auto definition_1 = make_document(kvp("mappings", make_document(kvp("dynamic", true))));
104+
auto model_1 = mongocxx::search_index_model(name_1, definition_1.view());
105+
models.push_back(model_1);
106+
107+
// Add an index model with static mappings to the input vector
108+
auto name_2 = "myStaticIndex";
109+
auto fields = make_document(kvp("title", make_document(kvp("type", "string"), kvp("analyzer","lucene.standard"))), kvp("year", make_document(kvp("type","number"))));
110+
auto definition_2 = make_document(kvp("mappings", make_document(kvp("dynamic", false), kvp("fields", fields))));
111+
auto model_2 = mongocxx::search_index_model(name_2, definition_2.view());
112+
models.push_back(model_2);
113+
114+
// Create the search indexes
115+
auto result = siv.create_many(models);
116+
117+
// Print the search index names
118+
std::cout << "New index names:" << std::endl;
119+
for (const std::string& name : result) {
120+
std::cout << name << std::endl;
121+
}
122+
// end-create-multiple-search-indexes
123+
}
124+
125+
{
126+
// start-list-search-indexes
127+
auto cursor = siv.list();
128+
for (mongocxx::cursor::iterator it = cursor.begin(); it != cursor.end(); ++it) {
129+
std::cout << bsoncxx::to_json(*it) << std::endl;
130+
}
131+
// end-list-search-indexes
132+
}
133+
{
134+
// start-list-search-index
135+
auto cursor = siv.list("myDynamicIndex");
136+
for (mongocxx::cursor::iterator it = cursor.begin(); it != cursor.end(); ++it) {
137+
std::cout << bsoncxx::to_json(*it) << std::endl;
138+
}
139+
// end-list-search-index
140+
// Print list() output using a range-based for loop
141+
for (const auto &idx : cursor) {
142+
std::cout << bsoncxx::to_json(idx) << std::endl;
143+
}
144+
}
145+
{
146+
// start-update-search-index
147+
auto update_fields = make_document(kvp("title", make_document(kvp("type", "string"), kvp("analyzer","lucene.simple"))), kvp("year", make_document(kvp("type","number"))));
148+
auto update_definition = make_document(kvp("mappings", make_document(kvp("dynamic", false), kvp("fields", update_fields))));
149+
siv.update_one("myStaticIndex", update_definition.view());
150+
// end-update-search-index
151+
}
152+
{
153+
// start-remove-search-index
154+
siv.drop_one("myDynamicIndex");
155+
// end-remove-search-index
156+
}
68157

69158
}
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,91 @@
1-
// start-single-field
2-
auto index_specification = make_document(kvp("<field name>", 1));
3-
auto result = collection.create_index(index_specification.view());
1+
/*
2+
To build:
3+
brew install mongo-cxx-driver
4+
clang++ -o indexes main.cpp -std=c++17 $(pkg-config --libs --cflags libmongocxx)
5+
*/
6+
#include <iostream>
47

5-
std::cout << "Index created: " << bsoncxx::to_json(result) << std::endl;
6-
// end-single-field
8+
#include <bsoncxx/builder/basic/document.hpp>
9+
#include <bsoncxx/json.hpp>
10+
#include <mongocxx/client.hpp>
11+
#include <mongocxx/instance.hpp>
12+
#include <mongocxx/uri.hpp>
713

8-
// start-compound-field
9-
auto index_specification = make_document(kvp("<field name 1>", -1), kvp("<field name 2>", -1));
10-
auto result = collection.create_index(index_specification.view());
14+
using bsoncxx::builder::basic::kvp;
15+
using bsoncxx::builder::basic::make_document;
1116

12-
std::cout << "Index created: " << bsoncxx::to_json(result) << std::endl;
13-
// end-compound-field
17+
int main() {
1418

15-
// start-remove-index
16-
collection.indexes().drop_one("<index name>");
19+
mongocxx::instance instance;
20+
mongocxx::uri uri("<connectionString>");
21+
mongocxx::client client(uri);
1722

18-
std::cout << "Index dropped." << std::endl;
19-
// end-remove-index
23+
auto db = client["<databaseName>"];
24+
auto collection = db["<collectionName>"];
25+
26+
{
27+
// start-single-field
28+
auto index_specification = make_document(kvp("<fieldName>", 1));
29+
auto result = collection.create_index(index_specification.view());
2030

21-
// start-remove-all-indexes
22-
collection.indexes().drop_all();
31+
std::cout << "Index created: " << bsoncxx::to_json(result) << std::endl;
32+
// end-single-field
33+
}
34+
{
35+
// start-compound-field
36+
auto index_specification = make_document(kvp("<fieldName1>", -1), kvp("<fieldName2>", -1));
37+
auto result = collection.create_index(index_specification.view());
2338

24-
std::cout << "All indexes removed." << std::endl;
25-
// end-remove-all-indexes
39+
std::cout << "Index created: " << bsoncxx::to_json(result) << std::endl;
40+
// end-compound-field
41+
}
42+
{
43+
// start-remove-index
44+
collection.indexes().drop_one("<indexName>");
45+
46+
std::cout << "Index dropped." << std::endl;
47+
// end-remove-index
48+
49+
// start-remove-all-indexes
50+
collection.indexes().drop_all();
51+
52+
std::cout << "All indexes removed." << std::endl;
53+
// end-remove-all-indexes
54+
}
55+
{
56+
// start-create-search-index
57+
// Create an index model with your index name and definition
58+
auto siv = collection.search_indexes();
59+
auto name = "<searchIndexName>";
60+
auto definition = make_document(kvp("mappings", make_document(kvp("dynamic", true))));
61+
auto model = mongocxx::search_index_model(name, definition.view());
62+
63+
// Create the search index
64+
auto result = siv.create_one(model);
65+
std::cout << "New index name: " << result << std::endl;
66+
// end-create-search-index
67+
}
68+
{
69+
// start-list-search-indexes
70+
auto siv = collection.search_indexes();
71+
auto result = siv.list();
72+
for (const auto &idx : result) {
73+
std::cout << bsoncxx::to_json(idx) << std::endl;
74+
}
75+
// end-list-search-indexes
76+
}
77+
{
78+
// start-update-search-index
79+
auto siv = collection.search_indexes();
80+
auto update_fields = make_document(kvp("<fieldName>", make_document(kvp("type", "<fieldType>"))));
81+
auto update_definition = make_document(kvp("mappings", make_document(kvp("dynamic", false), kvp("fields", update_fields))));
82+
siv.update_one("<searchIndexName>", update_definition.view());
83+
// end-update-search-index
84+
}
85+
{
86+
// start-remove-search-index
87+
auto siv = collection.search_indexes();
88+
siv.drop_one("<searchIndexName>");
89+
// end-remove-search-index
90+
}
91+
}

source/indexes.txt

+66-3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Optimize Queries with Indexes
2525
Work with Indexes </indexes/work-with-indexes>
2626
Single Field Indexes </indexes/single-field-index>
2727
Compound Indexes </indexes/compound-index>
28+
Atlas Search Indexes </indexes/atlas-search-index>
2829

2930
Overview
3031
--------
@@ -71,7 +72,7 @@ The following code shows how to create an ascending single-field index:
7172
:language: cli
7273
:visible: false
7374

74-
Index created: { "name" : "<field name>_1" }
75+
Index created: { "name" : "fieldName_1" }
7576

7677
To learn more about single-field indexes, see the
7778
:ref:`cpp-single-field-index` guide.
@@ -94,7 +95,7 @@ The following code shows how to create a descending compound index:
9495
:language: cli
9596
:visible: false
9697

97-
Index created: { "name" : "<field name 1>_-1_<field name 2>_-1" }
98+
Index created: { "name" : "fieldName1_-1_fieldName2_-1" }
9899

99100
To learn more about compound indexes, see the
100101
:ref:`cpp-compound-index` guide.
@@ -143,4 +144,66 @@ The following code shows how to remove all indexes in a collection:
143144
All indexes removed.
144145

145146
To learn more about removing indexes, see the
146-
:ref:`cpp-indexes-remove` section of the Work With Indexes guide.
147+
:ref:`cpp-indexes-remove` section of the Work With Indexes guide.
148+
149+
Atlas Search Index Management
150+
-----------------------------
151+
152+
The following sections contain code examples that describe how to manage Atlas Search indexes.
153+
To learn more about Atlas Search indexes, see the :ref:`Atlas Search Indexes <cpp-atlas-search-index>` guide.
154+
155+
Create Search Index
156+
~~~~~~~~~~~~~~~~~~~
157+
158+
The following code shows how to create an Atlas Search index that dynamically indexes all supported fields in the specified collection:
159+
160+
.. io-code-block::
161+
:copyable: true
162+
163+
.. input:: /includes/usage-examples/index-code-examples.cpp
164+
:start-after: start-create-search-index
165+
:end-before: end-create-search-index
166+
:language: cpp
167+
:dedent:
168+
169+
.. output::
170+
:language: cli
171+
:visible: false
172+
173+
New index name: searchIndexName
174+
175+
List Search Indexes
176+
~~~~~~~~~~~~~~~~~~~
177+
178+
The following code prints a list of Atlas Search indexes in the specified collection:
179+
180+
.. literalinclude:: /includes/usage-examples/index-code-examples.cpp
181+
:start-after: start-list-search-indexes
182+
:end-before: end-list-search-indexes
183+
:language: cpp
184+
:copyable:
185+
:dedent:
186+
187+
Update Search Indexes
188+
~~~~~~~~~~~~~~~~~~~~~
189+
190+
The following code updates an existing Atlas Search index with the specified new index definition:
191+
192+
.. literalinclude:: /includes/usage-examples/index-code-examples.cpp
193+
:start-after: start-update-search-index
194+
:end-before: end-update-search-index
195+
:language: cpp
196+
:copyable:
197+
:dedent:
198+
199+
Delete Search Indexes
200+
~~~~~~~~~~~~~~~~~~~~~
201+
202+
The following code deletes an Atlas Search index with the specified name:
203+
204+
.. literalinclude:: /includes/usage-examples/index-code-examples.cpp
205+
:start-after: start-remove-search-index
206+
:end-before: end-remove-search-index
207+
:language: cpp
208+
:copyable:
209+
:dedent:

0 commit comments

Comments
 (0)