Atlas search 인덱스
개요
In this guide, you can learn how to programmatically manage your Atlas Search and Atlas Vector Search indexes by using the C driver.
The Atlas Search feature enables you to perform full-text searches on collections hosted on MongoDB Atlas. To learn more about Atlas Search, see the Atlas Search 개요 in the Atlas documentation.
Atlas Vector Search enables you to perform semantic searches on vector embeddings stored in MongoDB Atlas. To learn more about Atlas Vector Search, see the Atlas Vector Search Overview in the Atlas documentation.
The following sections provide code examples that demonstrate how to create, list, update, and delete Atlas Search and Vector Search indexes.
검색 인덱스 만들기
To create an Atlas Search or Vector Search index, pass the createSearchIndexes
command to the mongoc_collection_command_simple()
function. You can use this command
to create one or multiple indexes.
다음 코드 예시에서는 Atlas Search 인덱스를 생성하는 방법을 보여줍니다.
bson_t cmd; bson_error_t error; char *cmd_str = bson_strdup_printf ( BSON_STR ({ "createSearchIndexes" : "%s", "indexes" : [ {"definition" : {"mappings" : {"dynamic" : false}}, "name" : "<index name>"} ] }), "<collection name>"); bson_init_from_json (&cmd, cmd_str, -1, &error); bson_free (cmd_str); if (mongoc_collection_command_simple (collection, &cmd, NULL, NULL, &error)) { printf ("Successfully created search index\n"); } else { fprintf (stderr, "Failed to create search index: %s", error.message); } bson_destroy (&cmd);
The following code example shows how to create an Atlas Vector Search index:
bson_t cmd; bson_error_t error; char * cmd_str = bson_strdup_printf( BSON_STR({ "createSearchIndexes": "%s", "indexes": [{ "name": "<index name>", "type": "vectorSearch", "definition": { "fields": [{ "type": "vector", "path": "plot_embedding", "numDimensions": 1536, "similarity": "euclidean" }] } }] }), "<collection name>" ); bson_init_from_json(&cmd, cmd_str, -1, &error); bson_free(cmd_str); if (mongoc_collection_command_simple(collection, &cmd, NULL, NULL, &error)) { printf("Successfully created Vector Search index\n"); } else { fprintf(stderr, "Failed to create Vector Search index: %s", error.message); } bson_destroy(&cmd);
The following code example shows how to create both search indexes in
one call to the mongoc_collection_command_simple()
function:
bson_t cmd; bson_error_t error; char * cmd_str = bson_strdup_printf( BSON_STR({ "createSearchIndexes": "%s", "indexes": [{ "definition": { "mappings": { "dynamic": false } }, "name": "<Atlas Search index name>" }, { "name": "<Vector Search index name>", "type": "vectorSearch", "definition": { "fields": [{ "type": "vector", "path": "plot_embedding", "numDimensions": 1536, "similarity": "euclidean" }] } } ] }), "<collection name>"); bson_init_from_json(&cmd, cmd_str, -1, &error); bson_free(cmd_str); if (mongoc_collection_command_simple(collection, &cmd, NULL, NULL, &error)) { printf("Successfully created search indexes\n"); } else { fprintf(stderr, "Failed to create search indexes: %s", error.message); } bson_destroy(&cmd);
To learn more about the syntax used to define each search index, see the following guides in the Atlas documentation:
검색 인덱스 나열
You can pass the $listSearchIndexes
aggregation stage to the
mongoc_collection_aggregate()
function to return all Atlas Search
and Vector Search indexes in a collection.
다음 코드 예시에서는 컬렉션의 검색 인덱스 목록을 출력하는 방법을 보여줍니다.
bson_t pipeline; const bson_t *doc; bson_error_t error; const char *pipeline_str = BSON_STR ({"pipeline" : [ {"$listSearchIndexes" : {}} ]}); bson_init_from_json (&pipeline, pipeline_str, -1, &error); mongoc_cursor_t *cursor = mongoc_collection_aggregate (collection, MONGOC_QUERY_NONE, &pipeline, NULL, NULL); while (mongoc_cursor_next (cursor, &doc)) { char *str = bson_as_canonical_extended_json (doc, NULL); printf ("%s\n", str); bson_free (str); } bson_destroy (&pipeline); mongoc_cursor_destroy (cursor);
검색 인덱스 업데이트
You can pass the updateSearchIndex
command to the mongoc_collection_command_simple()
function to update an Atlas Search or Vector Search index.
The following code shows how to update the Atlas Search index created in the 검색 인덱스 만들기 section of this guide to use dynamic mappings:
bson_t cmd; bson_error_t error; char *cmd_str = bson_strdup_printf ( BSON_STR ({ "updateSearchIndex" : "%s", "definition" : {"mappings" : {"dynamic" : true}}, "name" : "<index name>"}), "<collection name>"); bson_init_from_json (&cmd, cmd_str, -1, &error); bson_free (cmd_str); if (mongoc_collection_command_simple (collection, &cmd, NULL, NULL, &error)) { printf ("Successfully updated search index\n"); } else { fprintf (stderr, "Failed to create search index: %s", error.message); } bson_destroy (&cmd);
The following code shows how to update the Atlas Vector Search
index created in the 검색 인덱스 만들기 section of this guide
to use the cosine
similarity function:
bson_t cmd; bson_error_t error; char * cmd_str = bson_strdup_printf( BSON_STR({ "updateSearchIndex": "%s", "name": "<index name>", "definition": { "fields": [{ "type": "vector", "path": "plot_embedding", "numDimensions": 1536, "similarity": "cosine" }] } }), "<collection name>" ); bson_init_from_json(&cmd, cmd_str, -1, &error); bson_free(cmd_str); if (mongoc_collection_command_simple(collection, &cmd, NULL, NULL, &error)) { printf("Successfully updated search index\n"); } else { fprintf(stderr, "Failed to create search index: %s", error.message); } bson_destroy(&cmd);
검색 인덱스 삭제
You can pass the dropSearchIndexes
command to the mongoc_collection_command_simple()
function to delete an Atlas Search or Vector Search index.
다음 코드에서는 컬렉션에서 검색 인덱스를 삭제하는 방법을 보여줍니다.
bson_t cmd; bson_error_t error; char *cmd_str = bson_strdup_printf ( BSON_STR ({ "dropSearchIndexes" : "%s", "index" : "<index name>" }), "<collection name>"); bson_init_from_json (&cmd, cmd_str, -1, &error); if (mongoc_collection_command_simple (collection, &cmd, NULL, NULL, &error)) { printf ("Successfully deleted search index\n"); } else { fprintf (stderr, "Failed to delete search index: %s", error.message); } bson_destroy (&cmd);
추가 정보
To learn more about MongoDB Atlas Search, see Atlas search 인덱스 in the Atlas documentation.
To learn more about MongoDB Atlas Vector Search, see 벡터 검색을 위해 필드를 인덱싱하는 방법 in the Atlas documentation.
API 문서
이 가이드 에서 설명하는 함수에 대해 자세히 학습 다음 API 문서를 참조하세요.