Skip to content

Commit

Permalink
(DOCSP-40808) Indexes > Atlas search indexes (#64) (#72)
Browse files Browse the repository at this point in the history
* (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.
  • Loading branch information
elyse-mdb authored Nov 8, 2024
1 parent 1e1d88e commit a544cae
Show file tree
Hide file tree
Showing 7 changed files with 555 additions and 22 deletions.
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
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

0 comments on commit a544cae

Please sign in to comment.