개요
In this guide, you can learn how to create and manage MongoDB Search and MongoDB Vector Search indexes. These indexes allow you to use the following features:
MongoDB Search: Perform fast, full-text searches
MongoDB Vector Search: Perform semantic (similarity) searches on vector embeddings
MongoDB Search and MongoDB Vector Search indexes specify which fields to index, specify how these fields are indexed, and set other optional configurations.
참고
MongoDB Search index-management methods run asynchronously. The driver methods can return a result before the desired action completes on the server.
This guide explains how to perform the following actions to manage your MongoDB Search and MongoDB Vector Search indexes:
참고
샘플 데이터
이 가이드 의 예제에서는 Atlas 샘플 데이터 세트 중 하나인 sample_mflix 데이터베이스 의 embedded_movies 컬렉션 사용합니다. Atlas 샘플 데이터를 가져오는 방법에 대한 지침은 Atlas 설명서에서 샘플 데이터 로드 를 참조하세요.
검색 인덱스 모델 만들기
To create a MongoDB Search or MongoDB Vector Search index, you must first build a CreateSearchIndexModel instance that sets your index specifications.
CreateSearchIndexModel 에는 다음과 같은 속성이 있습니다.
속성 | 유형 | 설명 |
|---|---|---|
|
| Specifies the index definition. If you omit this setting, the driver creates a MongoDB Search index with dynamic mappings. |
|
| 인덱스 이름을 설정합니다. 이 설정을 생략하면 운전자 이름을 |
|
| Sets the index type. If you omit this setting, the driver creates a MongoDB Search index by default. |
To learn more about MongoDB Search field mappings, see Define Field Mappings in the Atlas documentation.
To learn more about defining MongoDB Vector Search indexes, see How to Index Fields for Vector Search in the Atlas documentation.
예제 모델
다음 예시 CreateSearchIndexModel 인스턴스 만들어 search_idx이라는 인덱스 에 대한 사양을 제공합니다. 이 코드는 title 및 released 필드의 정적 매핑을 지정합니다.
var def = new BsonDocument { { "mappings", new BsonDocument { { "dynamic", false }, { "fields", new BsonDocument { { "title", new BsonDocument { {"type", "string" } } }, { "released", new BsonDocument { { "type", "date" } } } } } } } }; var indexModel = new CreateSearchIndexModel( "search_idx", SearchIndexType.Search, def );
다음 예시에서는 CreateSearchIndexModel 인스턴스를 생성하여 vs_idx라는 이름의 인덱스에 대한 사양을 제공합니다. 코드는 임베딩 경로를 plot_embedding으로 지정하고, 1536 차원을 인덱싱하며, "euclidean" 벡터 유사성 함수를 사용합니다.
var def = new BsonDocument { { "fields", new BsonArray { new BsonDocument { { "type", "vector" }, { "path", "plot_embedding" }, { "numDimensions", 1536 }, { "similarity", "euclidean" } } } } }; var indexModel = new CreateSearchIndexModel( "vs_idx", SearchIndexType.VectorSearch, def );
검색 인덱스 만들기
You can create a MongoDB Search or MongoDB Vector Search index on a collection by calling the SearchIndexes.CreateOne() method on an IMongoCollection instance. This method accepts an index model as a parameter, specified in a CreateSearchIndexModel instance.
예시
The following example creates a MongoDB Search index on the embedded_movies collection. The code creates a CreateSearchIndexModel that sets the index name and enables dynamic mapping. Then, the code passes the CreateSearchIndexModel instance to the SearchIndexes.CreateOne() method to create the MongoDB Search index:
var indexModel = new CreateSearchIndexModel( "example_index", SearchIndexType.Search, new BsonDocument { { "mappings", new BsonDocument { { "dynamic", true }, } } } ); var result = movieCollection.SearchIndexes.CreateOne(indexModel); Console.WriteLine("Created MongoDB Search index:\n{0}", result);
Created MongoDB Search index: "example_index"
다중 검색 인덱스 만들기
You can create multiple MongoDB Search and MongoDB Vector Search indexes by calling the SearchIndexes.CreateMany() method on an IMongoCollection instance. This method accepts an IEnumerable of CreateSearchIndexModel instances as a parameter.
예시
이 예에서는 다음 조치를 수행합니다.
Creates a
CreateSearchIndexModelinstance that specifies a MongoDB Search index namedas_idxCreates a
CreateSearchIndexModelinstance that specifies a MongoDB Vector Search index namedvs_idx두
CreateSearchIndexModel인스턴스의List을SearchIndexes.CreateMany()메서드에 전달합니다.Creates the MongoDB Search and MongoDB Vector Search indexes on the
embedded_moviescollection
var searchModel = new CreateSearchIndexModel( "as_idx", SearchIndexType.Search, new BsonDocument { { "mappings", new BsonDocument { { "dynamic", true }, } } } ); var vectorModel = new CreateSearchIndexModel( "vs_idx", SearchIndexType.VectorSearch, new BsonDocument { { "fields", new BsonArray { new BsonDocument { { "type", "vector" }, { "path", "plot_embedding" }, { "numDimensions", 1536 }, { "similarity", "euclidean" } } } } } ); var models = new List<CreateSearchIndexModel> { searchModel, vectorModel }; var indexes = movieCollection.SearchIndexes.CreateMany(models); Console.WriteLine("Created Search indexes:\n{0} {1}", indexes.ToArray());
Created Search indexes: as_idx vs_idx
검색 인덱스 나열
You can access information about a collection's existing MongoDB Search and MongoDB Vector Search indexes by calling the SearchIndexes.List() method on the collection.
예시
The following example accesses information about the MongoDB Search and MongoDB Vector Search indexes created in the Create Multiple Search Indexes section of this page. The code calls the SearchIndexes.List() method and prints a list of the MongoDB Search and MongoDB Vector Search indexes on the collection:
var indexesList = movieCollection.SearchIndexes.List().ToList(); foreach (var i in indexesList) { Console.WriteLine(i); }
{ "id": "...", "name": "as_idx", "status": "READY", "queryable": true, "latestDefinitionVersion": {...}, "latestDefinition": { "mappings": { "dynamic": true } }, "statusDetail": [...] } { "id": "...", "name": "vs_idx", "type": "vectorSearch", "status": "READY", "queryable": true, ..., "latestDefinition": { "fields": [{ "type": "vector", "path": "plot_embedding", "numDimensions": 1536, "similarity": "euclidean" }] }, "statusDetail": [...] }
검색 인덱스 업데이트
You can update a MongoDB Search or MongoDB Vector Search index by calling the SearchIndexes.Update() method on an IMongoCollection instance. This method accepts the following parameters:
업데이트할 인덱스의 이름
수정된 인덱스 정의 문서
예시
다음 예는 이 페이지의 다중 검색 인덱스 생성 섹션에서 생성된 vs_index라는 Vector Search 인덱스를 업데이트합니다. 코드는 "dotProduct"를 벡터 유사성 함수로 사용하도록 인덱스에 지시하는 새로운 인덱스 정의 문서를 생성합니다. 그런 다음 SearchIndexes.Update() 메서드를 호출하여 인덱스를 업데이트합니다.
var updatedDef = new BsonDocument { { "fields", new BsonArray { new BsonDocument { { "type", "vector" }, { "path", "plot_embedding" }, { "numDimensions", 1536 }, { "similarity", "dotProduct" } } } } }; movieCollection.SearchIndexes.Update("vs_index", updatedDef);
검색 인덱스 삭제
You can delete a MongoDB Search or MongoDB Vector Search index by calling the SearchIndexes.DropOne() method on an IMongoCollection instance. This method accepts the name of the index to delete as a parameter.
예시
The following example deletes the MongoDB Search index named example_index created in the Create a Search Index section of this page. The code passes the index name to the SearchIndexes.DropOne() method to delete the index:
movieCollection.SearchIndexes.DropOne("example_index");
추가 정보
.NET/ C# 드라이버 사용하여 만들 수 있는 다른 인덱스에 대해 학습하려면 인덱스 만들기 및 관리 가이드를 참조하세요.
To learn more about MongoDB Search, see the following Atlas documentation:
To learn more about MongoDB Vector Search, see the following Atlas documentation:
API 문서
이 가이드에서 사용되는 메서드 또는 유형에 대해 자세히 알아보려면 다음 API 문서를 참조하세요.