Skip to content
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

(DOCSP-40808) Indexes > Atlas search indexes #64

Merged
merged 81 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
81 commits
Select commit Hold shift + click to select a range
cf70bde
(DOCSP-40808) Added atlas page based on PHP docs.
Oct 25, 2024
778dac0
(DOCSP-40808) Replace php command names with cpp
Oct 28, 2024
6c86589
(DOCSP-40808) Edits.
elyse-mdb Oct 28, 2024
776681e
(DOCSP-40808) Added copyable to examples in replace page, and edited …
elyse-mdb Oct 28, 2024
6157ec6
(DOCSP-40808) Edits.
elyse-mdb Oct 28, 2024
ccbe7b5
(DOCSP-40808) Edits.
elyse-mdb Oct 28, 2024
155408c
(DOCSP-40808) Edits.
elyse-mdb Oct 28, 2024
de1efed
(DOCSP-40808) Edits.
elyse-mdb Oct 29, 2024
2d58257
(DOCSP-40808) Edits.
elyse-mdb Oct 29, 2024
926ef78
(DOCSP-40808) Edits.
elyse-mdb Oct 29, 2024
be826b7
(DOCSP-40808) Edits.
elyse-mdb Oct 29, 2024
312ae81
(DOCSP-408088) Added multiple index example.
elyse-mdb Oct 29, 2024
18add6d
(DOCSP-40808) Pull changes to compound and single indexes, add change…
elyse-mdb Oct 29, 2024
e531d29
(DOCSP-40808) Build error fixes.
elyse-mdb Oct 29, 2024
76d8cd7
(DOCSP-40808) Build error fixes.
elyse-mdb Oct 29, 2024
004209c
(DOCSP-40808) Edits.
elyse-mdb Oct 29, 2024
f9c6773
(DOCSP-40808) Reorganizing section introductions.
elyse-mdb Oct 29, 2024
383886a
(DOCSP-40808) Reorganizing section introductions.
elyse-mdb Oct 29, 2024
2b8b17a
(DOCSP-40808) edits.
elyse-mdb Oct 30, 2024
6095312
(DOCSP-40808) Added example for listing one search index.
elyse-mdb Oct 30, 2024
c6a26e8
(DOCSP-40808) Add examples for delete and update a search index.
elyse-mdb Oct 30, 2024
9089224
(DOCPS-40808) Fixing build errors.
elyse-mdb Oct 30, 2024
d80e007
(DOCSP-40808) Reorganizing text.
elyse-mdb Oct 30, 2024
ecbee6a
(DOCSP-40808) Reorganizing text.
elyse-mdb Oct 30, 2024
fbdb1e4
(DOCSP-40808) Reorganizing text.
elyse-mdb Oct 30, 2024
2023bf0
(DOCSP-40808) Added API links
elyse-mdb Oct 30, 2024
8458bd3
(DOCSP-40808) Output edit.
elyse-mdb Oct 30, 2024
9262eda
(DOCSP-40808) Updates.
elyse-mdb Oct 30, 2024
5393080
(DOCSP-40808) Updates.
elyse-mdb Oct 30, 2024
949ffe9
(DOCSP-40808) Fix page reference.
elyse-mdb Oct 30, 2024
32237bb
(DOCSP-40808) Breaking up the create an index section and adding text…
elyse-mdb Oct 30, 2024
d47cf3d
(DOCSP-40808) Added code examples to indexes landing page.
elyse-mdb Oct 30, 2024
8b46298
(DOCSP-40808) Note to tip
elyse-mdb Oct 30, 2024
492989c
(DOCSP-40808) De-wordi-fication.
elyse-mdb Oct 30, 2024
e40e93d
(DOCSP-40808) Fix reference.
elyse-mdb Oct 30, 2024
d69f646
(DOCSP-40808) Replace delete with remove
elyse-mdb Nov 4, 2024
2c3d51a
(DOCSP-40808) Added note.
elyse-mdb Nov 4, 2024
13c4fd9
(DOCSP-40808) Add new links and reorganize.
elyse-mdb Nov 4, 2024
17a09f6
(DOCSP-40808) Add new links and reorganize.
elyse-mdb Nov 4, 2024
021db76
(DOCSP-40808) Added code comment.
elyse-mdb Nov 4, 2024
54b3a41
(DOCSP-40808) Standardizing directions.
elyse-mdb Nov 4, 2024
9ce491f
(DOCSP-40808) range based vs iterator
elyse-mdb Nov 4, 2024
9b13e1e
(DOCSP-40808) range-based
elyse-mdb Nov 4, 2024
3838d8a
(DOCSP-40808) Edits.
elyse-mdb Nov 4, 2024
8b3cd5a
(DOCSP-40808) Simplify the list indexes.
elyse-mdb Nov 4, 2024
09d7a4f
(DOCSP-40808) Error fix.
elyse-mdb Nov 4, 2024
fc60e00
(DOCSP-40808) Note to important.
elyse-mdb Nov 4, 2024
f615e18
(DOCSP-40808) Edits.
elyse-mdb Nov 4, 2024
23f8fe2
(DOCSP-40808) Editing output.
elyse-mdb Nov 4, 2024
7f8d180
(DOCSP-40808) Editing output.
elyse-mdb Nov 4, 2024
ea91b59
(DOCSP-40808) Editing output.
elyse-mdb Nov 4, 2024
16832f1
(DOCSP-40808) Editing output.
elyse-mdb Nov 4, 2024
1d2d3f6
(DOCSP-40808) Make output consistent.
elyse-mdb Nov 4, 2024
5941528
(DOCSP-40808) Fix updated code.
elyse-mdb Nov 4, 2024
faad64f
(DOCSP-40808) Remove comments.
elyse-mdb Nov 4, 2024
6335c21
(DOCSP-40808) Fix build errors.
elyse-mdb Nov 4, 2024
0a3aed3
(DOCSP-40808) Edits.
elyse-mdb Nov 4, 2024
6b0547d
(DOCSP-40808) Edits.
elyse-mdb Nov 4, 2024
aff73f6
(DOCSP-40808) Fix code examples.
elyse-mdb Nov 4, 2024
04cc5b2
(DOCSP-40808) Edits.
elyse-mdb Nov 4, 2024
02c866d
(DOCSP-40808) Remove create multiple search indexes
elyse-mdb Nov 4, 2024
2439d44
(DOCSP-40808) Final edits.
elyse-mdb Nov 4, 2024
b1fd6fa
(DOCSP-40808) @mongoKart Review changes.
elyse-mdb Nov 5, 2024
7772f96
(DOCSP-40808) @mongoKart Review edits: Adding more context to create …
elyse-mdb Nov 5, 2024
08572ab
(DOCSP-40808) @mongoKart Review changes: more context for create a si…
elyse-mdb Nov 5, 2024
2917104
(DOCSP-40808) @mongoKart Replace all 'search index' with 'atlas searc…
elyse-mdb Nov 5, 2024
6daf5a9
(DOCSP-40808) @mongoKart Addressing feedback for create an index.
elyse-mdb Nov 5, 2024
5a741f9
(DOCSP-40808) @mongoKart addressing feedback.
elyse-mdb Nov 6, 2024
43857a1
(DOCSP-40808) Addressing review feedback.
elyse-mdb Nov 6, 2024
0994007
(DOCSP-40808) @mongoKart Feedback changes.
elyse-mdb Nov 6, 2024
702fa1a
(DOCSP-40808) Removed text about Atlas Search Queries because it is c…
elyse-mdb Nov 6, 2024
1c59657
(DOCSP-40808) @mongoKart Editing directions to create a new index.
elyse-mdb Nov 6, 2024
2631eb5
(DOCSP-40808) Edit.
elyse-mdb Nov 6, 2024
fab2a03
(DOCSP-40808) Link error fix.
elyse-mdb Nov 6, 2024
63898c3
(DOCSP-40808) @mongoKart reorganizing the create_one() directions.
elyse-mdb Nov 6, 2024
5791db8
(DOCSP-40808) Fix formatting in bulleted list.
elyse-mdb Nov 6, 2024
8db84aa
(DOCSP-40808) Typo.
elyse-mdb Nov 6, 2024
6a2b30e
(DOCSP-40808) @mongoKart Addressing feedback.
elyse-mdb Nov 7, 2024
b91abb3
(DOCSP-40808) @mongoKart Addressing feedback.
elyse-mdb Nov 7, 2024
026437a
(DOCSP-40808) Fix ambiguous wording.
elyse-mdb Nov 8, 2024
8a66ab3
(DOCSP-40808) @kevinAlbs Review changes.
elyse-mdb Nov 8, 2024
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
89 changes: 89 additions & 0 deletions source/includes/indexes/indexes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ int main(){
auto collection = db["movies"];
// end-db-coll

// start-siv
auto siv = collection.search_indexes();
// end-siv
{
// start-index-single
auto index_specification = make_document(kvp("title", 1));
Expand Down Expand Up @@ -65,5 +68,91 @@ int main(){
collection.indexes().drop_one("*");
// end-remove-all-wildcard
}
{
// start-create-static-search-index
// Create an index model with your index name and definition containing the fields you want to index
auto name = "myStaticIndex";
auto fields = make_document(kvp("title", make_document(kvp("type", "string"), kvp("analyzer","lucene.standard"))), kvp("year", make_document(kvp("type","number"))));
auto definition = make_document(kvp("mappings", make_document(kvp("dynamic", false), kvp("fields", fields))));
auto model = mongocxx::search_index_model(name, definition.view());

// Create the search index
auto result = siv.create_one(model);
std::cout << "New index name: " << result << std::endl;
// end-create-static-search-index
}
{
// start-create-dynamic-search-index
// Create an index model with your index name and definition
auto name = "myDynamicIndex";
auto definition = make_document(kvp("mappings", make_document(kvp("dynamic", true))));
auto model = mongocxx::search_index_model(name, definition.view());

// Create the search index
auto result = siv.create_one(model);
std::cout << "New index name: " << result << std::endl;
// end-create-dynamic-search-index
}
{
// start-create-multiple-search-indexes
// Create a vector to store Search index models
std::vector<mongocxx::search_index_model> models;

// Add an index model with dynamic mappings to the input vector
auto name_1 = "myDynamicIndex";
auto definition_1 = make_document(kvp("mappings", make_document(kvp("dynamic", true))));
auto model_1 = mongocxx::search_index_model(name_1, definition_1.view());
models.push_back(model_1);

// Add an index model with static mappings to the input vector
auto name_2 = "myStaticIndex";
auto fields = make_document(kvp("title", make_document(kvp("type", "string"), kvp("analyzer","lucene.standard"))), kvp("year", make_document(kvp("type","number"))));
auto definition_2 = make_document(kvp("mappings", make_document(kvp("dynamic", false), kvp("fields", fields))));
auto model_2 = mongocxx::search_index_model(name_2, definition_2.view());
models.push_back(model_2);

// Create the search indexes
auto result = siv.create_many(models);

// Print the search index names
std::cout << "New index names:" << std::endl;
for (const std::string& name : result) {
std::cout << name << std::endl;
}
// end-create-multiple-search-indexes
}

{
// start-list-search-indexes
auto cursor = siv.list();
for (mongocxx::cursor::iterator it = cursor.begin(); it != cursor.end(); ++it) {
std::cout << bsoncxx::to_json(*it) << std::endl;
}
// end-list-search-indexes
}
{
// start-list-search-index
auto cursor = siv.list("myDynamicIndex");
for (mongocxx::cursor::iterator it = cursor.begin(); it != cursor.end(); ++it) {
std::cout << bsoncxx::to_json(*it) << std::endl;
}
// end-list-search-index
// Print list() output using a range-based for loop
for (const auto &idx : cursor) {
std::cout << bsoncxx::to_json(idx) << std::endl;
}
}
{
// start-update-search-index
auto update_fields = make_document(kvp("title", make_document(kvp("type", "string"), kvp("analyzer","lucene.simple"))), kvp("year", make_document(kvp("type","number"))));
auto update_definition = make_document(kvp("mappings", make_document(kvp("dynamic", false), kvp("fields", update_fields))));
siv.update_one("myStaticIndex", update_definition.view());
// end-update-search-index
}
{
// start-remove-search-index
siv.drop_one("myDynamicIndex");
// end-remove-search-index
}

}
102 changes: 84 additions & 18 deletions source/includes/usage-examples/index-code-examples.cpp
elyse-mdb marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,25 +1,91 @@
// start-single-field
auto index_specification = make_document(kvp("<field name>", 1));
auto result = collection.create_index(index_specification.view());
/*
To build:
brew install mongo-cxx-driver
clang++ -o indexes main.cpp -std=c++17 $(pkg-config --libs --cflags libmongocxx)
*/
#include <iostream>

std::cout << "Index created: " << bsoncxx::to_json(result) << std::endl;
// end-single-field
#include <bsoncxx/builder/basic/document.hpp>
#include <bsoncxx/json.hpp>
#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>
#include <mongocxx/uri.hpp>

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

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

// start-remove-index
collection.indexes().drop_one("<index name>");
mongocxx::instance instance;
mongocxx::uri uri("<connectionString>");
mongocxx::client client(uri);

std::cout << "Index dropped." << std::endl;
// end-remove-index
auto db = client["<databaseName>"];
auto collection = db["<collectionName>"];

{
// start-single-field
auto index_specification = make_document(kvp("<fieldName>", 1));
auto result = collection.create_index(index_specification.view());

// start-remove-all-indexes
collection.indexes().drop_all();
std::cout << "Index created: " << bsoncxx::to_json(result) << std::endl;
// end-single-field
}
{
// start-compound-field
auto index_specification = make_document(kvp("<fieldName1>", -1), kvp("<fieldName2>", -1));
auto result = collection.create_index(index_specification.view());

std::cout << "All indexes removed." << std::endl;
// end-remove-all-indexes
std::cout << "Index created: " << bsoncxx::to_json(result) << std::endl;
// end-compound-field
}
{
// start-remove-index
collection.indexes().drop_one("<indexName>");

std::cout << "Index dropped." << std::endl;
// end-remove-index

// start-remove-all-indexes
collection.indexes().drop_all();

std::cout << "All indexes removed." << std::endl;
// end-remove-all-indexes
}
{
// start-create-search-index
// Create an index model with your index name and definition
auto siv = collection.search_indexes();
auto name = "<searchIndexName>";
auto definition = make_document(kvp("mappings", make_document(kvp("dynamic", true))));
auto model = mongocxx::search_index_model(name, definition.view());

// Create the search index
auto result = siv.create_one(model);
std::cout << "New index name: " << result << std::endl;
// end-create-search-index
}
{
// start-list-search-indexes
auto siv = collection.search_indexes();
auto result = siv.list();
for (const auto &idx : result) {
std::cout << bsoncxx::to_json(idx) << std::endl;
}
// end-list-search-indexes
}
{
// start-update-search-index
auto siv = collection.search_indexes();
auto update_fields = make_document(kvp("<fieldName>", make_document(kvp("type", "<fieldType>"))));
auto update_definition = make_document(kvp("mappings", make_document(kvp("dynamic", false), kvp("fields", update_fields))));
siv.update_one("<searchIndexName>", update_definition.view());
// end-update-search-index
}
{
// start-remove-search-index
auto siv = collection.search_indexes();
siv.drop_one("<searchIndexName>");
// end-remove-search-index
}
}
69 changes: 66 additions & 3 deletions source/indexes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Optimize Queries with Indexes
Work with Indexes </indexes/work-with-indexes>
Single Field Indexes </indexes/single-field-index>
Compound Indexes </indexes/compound-index>
Atlas Search Indexes </indexes/atlas-search-index>

Overview
--------
Expand Down Expand Up @@ -71,7 +72,7 @@ The following code shows how to create an ascending single-field index:
:language: cli
:visible: false

Index created: { "name" : "<field name>_1" }
Index created: { "name" : "fieldName_1" }

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

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

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

To learn more about removing indexes, see the
:ref:`cpp-indexes-remove` section of the Work With Indexes guide.
:ref:`cpp-indexes-remove` section of the Work With Indexes guide.

Atlas Search Index Management
-----------------------------

The following sections contain code examples that describe how to manage Atlas Search indexes.
To learn more about Atlas Search indexes, see the :ref:`Atlas Search Indexes <cpp-atlas-search-index>` guide.

Create Search Index
~~~~~~~~~~~~~~~~~~~

The following code shows how to create an Atlas Search index that dynamically indexes all supported fields in the specified collection:

.. io-code-block::
:copyable: true

.. input:: /includes/usage-examples/index-code-examples.cpp
:start-after: start-create-search-index
:end-before: end-create-search-index
:language: cpp
:dedent:

.. output::
:language: cli
:visible: false

New index name: searchIndexName

List Search Indexes
~~~~~~~~~~~~~~~~~~~

The following code prints a list of Atlas Search indexes in the specified collection:

.. literalinclude:: /includes/usage-examples/index-code-examples.cpp
:start-after: start-list-search-indexes
:end-before: end-list-search-indexes
:language: cpp
:copyable:
:dedent:

Update Search Indexes
~~~~~~~~~~~~~~~~~~~~~

The following code updates an existing Atlas Search index with the specified new index definition:

.. literalinclude:: /includes/usage-examples/index-code-examples.cpp
:start-after: start-update-search-index
:end-before: end-update-search-index
:language: cpp
:copyable:
:dedent:

Delete Search Indexes
~~~~~~~~~~~~~~~~~~~~~

The following code deletes an Atlas Search index with the specified name:

.. literalinclude:: /includes/usage-examples/index-code-examples.cpp
:start-after: start-remove-search-index
:end-before: end-remove-search-index
:language: cpp
:copyable:
:dedent:
Loading
Loading