-
Notifications
You must be signed in to change notification settings - Fork 455
CDRIVER-4612 document management of search indexes #1366
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
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
84097bf
align "Expected" and "Actual" output in match error
kevinAlbs 4baf28e
quote missing required field
kevinAlbs f197dac
add index-management tests
kevinAlbs 6215535
add search operations and implement without helpers
kevinAlbs 615d1df
document management of search indexes
kevinAlbs e4296d3
create and drop random collection name in example
kevinAlbs 30b4d28
note expectation to run against a MongoDB Atlas cluster
kevinAlbs 6b514af
note in error message to use MongoDB Atlas cluster
kevinAlbs ee78062
check that `$listSearchIndexes` is supported
kevinAlbs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,230 @@ | ||
// example-manage-search-indexes creates, lists, updates, and deletes an Atlas | ||
// search index from the `test.test` collection. | ||
// Example is expected to be run against a MongoDB Atlas cluster. | ||
|
||
#include <mongoc/mongoc.h> | ||
#include <stdlib.h> // abort | ||
|
||
#define HANDLE_ERROR(...) \ | ||
if (1) { \ | ||
fprintf (stderr, __VA_ARGS__); \ | ||
fprintf (stderr, "\n"); \ | ||
goto fail; \ | ||
} else \ | ||
(void) 0 | ||
|
||
#define ASSERT(stmt) \ | ||
if (!stmt) { \ | ||
fprintf (stderr, \ | ||
"assertion failed on line: %d, statement: %s\n", \ | ||
__LINE__, \ | ||
#stmt); \ | ||
abort (); \ | ||
} else \ | ||
(void) 0 | ||
|
||
int | ||
main (int argc, char *argv[]) | ||
{ | ||
mongoc_client_t *client = NULL; | ||
const char *uri_string = | ||
"mongodb://127.0.0.1/?appname=create-search-indexes-example"; | ||
mongoc_uri_t *uri = NULL; | ||
mongoc_collection_t *coll = NULL; | ||
bson_error_t error; | ||
bool ok = false; | ||
|
||
mongoc_init (); | ||
|
||
if (argc > 2) { | ||
HANDLE_ERROR ( | ||
"Unexpected arguments. Expected usage: %s [CONNECTION_STRING]", | ||
argv[0]); | ||
} | ||
|
||
if (argc > 1) { | ||
uri_string = argv[1]; | ||
} | ||
|
||
uri = mongoc_uri_new_with_error (uri_string, &error); | ||
if (!uri) { | ||
HANDLE_ERROR ("Failed to parse URI: %s", error.message); | ||
} | ||
client = mongoc_client_new_from_uri_with_error (uri, &error); | ||
if (!client) { | ||
HANDLE_ERROR ("Failed to create client: %s", error.message); | ||
} | ||
|
||
// Create a random collection name. | ||
char collname[25]; | ||
{ | ||
// There is a server-side limitation that prevents multiple search indexes | ||
// from being created with the same name, definition and collection name. | ||
// Atlas search index management operations are asynchronous. Dropping a | ||
// collection may not result in the index being dropped immediately. Use a | ||
// randomly generated collection name to avoid errors. | ||
bson_oid_t oid; | ||
bson_oid_init (&oid, NULL); | ||
bson_oid_to_string (&oid, collname); | ||
} | ||
|
||
// Create collection object. | ||
{ | ||
// Create the collection server-side to avoid the server error: | ||
// "Collection 'test.<collname>' does not exist." | ||
mongoc_database_t *db = mongoc_client_get_database (client, "test"); | ||
coll = mongoc_database_create_collection ( | ||
db, collname, NULL /* options */, &error); | ||
if (!coll) { | ||
mongoc_database_destroy (db); | ||
HANDLE_ERROR ("Failed to create collection: %s", error.message); | ||
} | ||
mongoc_database_destroy (db); | ||
} | ||
|
||
// Check that $listSearchIndexes pipeline stage is supported. | ||
// This is intended to check that a MongoDB Atlas cluster is used. | ||
{ | ||
const char *pipeline_str = | ||
BSON_STR ({"pipeline" : [ {"$listSearchIndexes" : {}} ]}); | ||
bson_t pipeline; | ||
ASSERT (bson_init_from_json (&pipeline, pipeline_str, -1, &error)); | ||
mongoc_cursor_t *cursor = | ||
mongoc_collection_aggregate (coll, | ||
MONGOC_QUERY_NONE, | ||
&pipeline, | ||
NULL /* opts */, | ||
NULL /* read_prefs */); | ||
const bson_t *got; | ||
while (mongoc_cursor_next (cursor, &got)) | ||
; | ||
if (mongoc_cursor_error (cursor, &error)) { | ||
bson_destroy (&pipeline); | ||
mongoc_cursor_destroy (cursor); | ||
HANDLE_ERROR ("Failed to run $listSearchIndexes with error: %s\n" | ||
"Does the URI point to a MongoDB Atlas cluster? %s", | ||
error.message, | ||
uri_string); | ||
} | ||
bson_destroy (&pipeline); | ||
mongoc_cursor_destroy (cursor); | ||
} | ||
|
||
{ | ||
// Create an Atlas Search Index ... begin | ||
bson_t cmd; | ||
// Create command. | ||
{ | ||
char *cmd_str = bson_strdup_printf ( | ||
BSON_STR ({ | ||
"createSearchIndexes" : "%s", | ||
"indexes" : [ { | ||
"definition" : {"mappings" : {"dynamic" : false}}, | ||
"name" : "test-index" | ||
} ] | ||
}), | ||
collname); | ||
ASSERT (bson_init_from_json (&cmd, cmd_str, -1, &error)); | ||
bson_free (cmd_str); | ||
} | ||
if (!mongoc_collection_command_simple ( | ||
coll, &cmd, NULL /* read_prefs */, NULL /* reply */, &error)) { | ||
bson_destroy (&cmd); | ||
HANDLE_ERROR ("Failed to run createSearchIndexes: %s", error.message); | ||
} | ||
printf ("Created index: \"test-index\"\n"); | ||
bson_destroy (&cmd); | ||
// Create an Atlas Search Index ... end | ||
} | ||
|
||
{ | ||
// List Atlas Search Indexes ... begin | ||
const char *pipeline_str = | ||
BSON_STR ({"pipeline" : [ {"$listSearchIndexes" : {}} ]}); | ||
bson_t pipeline; | ||
ASSERT (bson_init_from_json (&pipeline, pipeline_str, -1, &error)); | ||
mongoc_cursor_t *cursor = | ||
mongoc_collection_aggregate (coll, | ||
MONGOC_QUERY_NONE, | ||
&pipeline, | ||
NULL /* opts */, | ||
NULL /* read_prefs */); | ||
printf ("Listing indexes:\n"); | ||
const bson_t *got; | ||
while (mongoc_cursor_next (cursor, &got)) { | ||
char *got_str = bson_as_canonical_extended_json (got, NULL); | ||
printf (" %s\n", got_str); | ||
bson_free (got_str); | ||
} | ||
if (mongoc_cursor_error (cursor, &error)) { | ||
bson_destroy (&pipeline); | ||
mongoc_cursor_destroy (cursor); | ||
HANDLE_ERROR ("Failed to run $listSearchIndexes: %s", error.message); | ||
} | ||
bson_destroy (&pipeline); | ||
mongoc_cursor_destroy (cursor); | ||
// List Atlas Search Indexes ... end | ||
} | ||
|
||
{ | ||
// Update an Atlas Search Index ... begin | ||
bson_t cmd; | ||
// Create command. | ||
{ | ||
char *cmd_str = bson_strdup_printf ( | ||
BSON_STR ({ | ||
"updateSearchIndex" : "%s", | ||
"definition" : {"mappings" : {"dynamic" : true}}, | ||
"name" : "test-index" | ||
}), | ||
collname); | ||
ASSERT (bson_init_from_json (&cmd, cmd_str, -1, &error)); | ||
bson_free (cmd_str); | ||
} | ||
if (!mongoc_collection_command_simple ( | ||
coll, &cmd, NULL /* read_prefs */, NULL /* reply */, &error)) { | ||
bson_destroy (&cmd); | ||
HANDLE_ERROR ("Failed to run updateSearchIndex: %s", error.message); | ||
} | ||
printf ("Updated index: \"test-index\"\n"); | ||
bson_destroy (&cmd); | ||
// Update an Atlas Search Index ... end | ||
} | ||
|
||
{ | ||
// Drop an Atlas Search Index ... begin | ||
bson_t cmd; | ||
// Create command. | ||
{ | ||
char *cmd_str = bson_strdup_printf ( | ||
BSON_STR ({"dropSearchIndex" : "%s", "name" : "test-index"}), | ||
collname); | ||
ASSERT (bson_init_from_json (&cmd, cmd_str, -1, &error)); | ||
bson_free (cmd_str); | ||
} | ||
if (!mongoc_collection_command_simple ( | ||
coll, &cmd, NULL /* read_prefs */, NULL /* reply */, &error)) { | ||
bson_destroy (&cmd); | ||
HANDLE_ERROR ("Failed to run dropSearchIndex: %s", error.message); | ||
} | ||
printf ("Dropped index: \"test-index\"\n"); | ||
bson_destroy (&cmd); | ||
// Drop an Atlas Search Index ... end | ||
} | ||
|
||
// Drop created collection. | ||
{ | ||
if (!mongoc_collection_drop (coll, &error)) { | ||
HANDLE_ERROR ( | ||
"Failed to drop collection '%s': %s", collname, error.message); | ||
} | ||
} | ||
|
||
ok = true; | ||
fail: | ||
mongoc_collection_destroy (coll); | ||
mongoc_client_destroy (client); | ||
mongoc_uri_destroy (uri); | ||
mongoc_cleanup (); | ||
return ok ? EXIT_SUCCESS : EXIT_FAILURE; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this example is run against a non-Atlas server, this example fails on this command attempt and error with a "no such cmd" message. Can we consider adding an earlier "example must be run against Atlas" check and error to make the required conditions a bit more clear?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like that idea. ee78062 adds an earlier check that
$listSearchIndexes
is supported, and prints an error suggesting the example may not be using Atlas if an error occurred.