ベクトルAtlas Searchのフィールドにインデックスを付ける方法
項目一覧
vectorSearch
タイプを使用して、 $vectorSearch
クエリを実行中のフィールドにインデックスを作成できます。 クエリするベクトル埋め込みと、データを事前フィルタリングするために使用するブール値、日付、ObjectId、数値、string、または UUID 値のインデックスを定義できます。 データをフィルタリングすると、セマンティック検索の範囲を絞り込み、マルチテナント環境など特定のベクトル埋め込みが比較の対象とならないようにするのに役立ちます。
Atlas Vector Search インデックスを 作成 するには、Atlas UI、Atlas Administration API、Atlas CLI、mongosh
、またはサポートされている MongoDB ドライバー を使用できます。
注意
非推奨のknnBeta演算子を使用して、 vectorSearch
型のインデックス定義を使用してインデックス付けされたフィールドをクエリすることはできません。
Considerations
vectorSearch
型のインデックス定義では、単一要素のみを含む配列のインデックスを作成できます。 ドキュメントの配列内のフィールド、またはオブジェクトの配列内のフィールドをインデックスすることはできません。 ドット表記を使用してドキュメント内のフィールドにインデックスを付けることができます。
構文
次の構文は、 vectorSearch
インデックス タイプを定義します。
1 { 2 "fields":[ 3 { 4 "type": "vector", 5 "path": "<field-to-index>", 6 "numDimensions": <number-of-dimensions>, 7 "similarity": "euclidean | cosine | dotProduct" 8 }, 9 { 10 "type": "filter", 11 "path": "<field-to-index>" 12 }, 13 ... 14 ] 15 }
型についてvector
ベクトル$vectorSearch
フィールドには、 パイプライン ステージを使用してクエリを実行するための BSON データ型の数値の配列が含まれている必要があります。double
fields
配列内でベクトル フィールドをvector
型としてインデックス化する必要があります。
次の構文は、 vector
フィールド型を定義します。
1 { 2 "fields":[ 3 { 4 "type": "vector", 5 "path": <field-to-index>, 6 "numDimensions": <number-of-dimensions>, 7 "similarity": "euclidean | cosine | dotProduct" 8 }, 9 ... 10 ] 11 }
型についてfilter
オプションで、ブール値、日付、数値、 ObjectId 、 string 、 UUID フィールドをインデックスして、データを事前にフィルタリングできます。 データをフィルタリングすると、セマンティック検索の範囲を絞り込み、すべてのベクトルが比較の対象とならないようにするのに役立ちます。 これらのフィールドは、 fields
配列内でfilter
型を使用してインデックス化する必要があります。
次の構文は、 filter
フィールド型を定義します。
1 { 2 "fields":[ 3 { 4 "type": "vector", 5 ... 6 }, 7 { 8 "type": "filter", 9 "path": "<field-to-index>" 10 }, 11 ... 12 ] 13 }
注意
データを事前にフィルタリングしても、Atlas Vector Search が$vectorSearch
クエリに$vectorSearchScore
を使用して返すスコアには影響しません。
Atlas Vector Search インデックス フィールド
Atlas Vector Search インデックス定義には次のフィールドがあります。
オプション | タイプ | 必要性 | 目的 | ||||
---|---|---|---|---|---|---|---|
fields | ドキュメントの配列 | 必須 | インデックスするベクトル フィールドとフィルター フィールド(ドキュメントごとに 1 つ)。 少なくとも 1 つのドキュメントには、ベクトル フィールドのフィールド定義が含まれている必要があります。 オプションで、ブール値、日付、数値、 ObjectId 、 string 、および UUID フィールドをドキュメントごとに 1 つずつインデックスして、データを事前にフィルタリングすることもできます。 | ||||
fields.type | string | 必須 |
| ||||
fields.path | string | 必須 | インデックスを作成するフィールドの名前。 ネストされたフィールドの場合は、埋め込みフィールドへのパスを指定するために ドット表記 を使用します。 2 つの連続したドット(
| ||||
fields.numDimensions | 整数 | 必須 | Atlas Vector Search がインデックス作成時およびクエリ時に強制されるベクトル次元の数。 4096 以下の値を指定する必要があります。 このフィールドはvector 型フィールドにのみ設定できます。 | ||||
fields.similarity | string | 必須 | 上位 K 近傍の検索に使用するベクトル類似度関数。 このフィールドは
|
Atlas Vector Search インデックスの作成
Atlas Search インデックスは、簡単に検索可能な形式でデータを分類するデータ構造であり、タームとタームを含むドキュメントの間をマッピングします。Atlas Search インデックスを使用すると、特定の識別子を使用してドキュメントをすばやく検索できます。Atlas Search を使用して Atlas クラスター内のデータをクエリするには、Atlas Search インデックスを構成する必要があります。
Atlas Search インデックスは、単一または複数のフィールドで作成できます。データのソートやフィルタリングによく使用するフィールドはインデックスを作成して、クエリ時に関連データを含むドキュメントをすばやく取得できるようにすることをお勧めします。
Atlas UI、Atlas Administration API、Atlas CLI を使用して、Atlas クラスター上の任意の種類のデータとともに、長さが4096次元以下のベクトル埋め込みを含むすべてのコレクションに対する Atlas Vector Search インデックスを作成できます。 、 mongosh
、またはサポートされているMongoDB ドライバー。
前提条件
Atlas Vector Search インデックスを作成するには、次の前提条件を持つ Atlas クラスターが必要です。
MongoDB バージョン
6.0.11
、7.0.2
、または 以上Atlas Vector Search インデックスの作成対象のコレクション
注意
mongosh
コマンドまたはドライバーヘルパーメソッドを使用して、すべての Atlas クラスター階層に Atlas Search インデックスを作成できます。
必要なアクセス権
Atlas Vector Search インデックスを作成および管理するには、 Project Data Access Admin
以上のロールが必要です。
無料クラスターと共有クラスターの制限
インデックス作成数の上限は以下のとおりです。
M0
クラスターで 3 件。M2
クラスターで 5 件M5
クラスターで 10 件
M10+
クラスターで作成できるインデックス数に上限はありません。
手順
➤ [言語を選択 ] ドロップダウン メニューを使用して、インデックスの作成に使用するクライアントを選択します。
注意
この手順には、 sample_mflix
データベース内のembedded_movies
コレクションのインデックス定義の例が含まれています。 サンプル データを クラスターにロードし、このコレクション用のサンプル Atlas Search インデックスを作成すると、このコレクションに対してサンプル$vectorSearch
クエリを実行できます。 実行できるサンプル クエリの詳細については、「 $vectorSearch の例 」を参照してください。
Atlas Administration API を使用してコレクション用 Atlas Vector Search インデックスを作成するには、必要なパラメータを指定して Atlas Search indexes
エンドポイントにPOST
リクエストを送信します。
1 curl --user "{PUBLIC-KEY}:{PRIVATE-KEY}" --digest \ 2 --header "Accept: application/json" \ 3 --header "Content-Type: application/json" \ 4 --include \ 5 --request POST "https://cloud.mongodb.com/api/atlas/v2/groups/{groupId}/clusters/{clusterName}/search/indexes" \ 6 --data ' 7 { 8 "database": "<name-of-database>", 9 "collectionName": "<name-of-collection>", 10 "type": "vectorSearch", 11 "name": "<index-name>", 12 "definition": { 13 "fields":[ 14 { 15 "type": "vector", 16 "path": <field-to-index>, 17 "numDimensions": <number-of-dimensions>, 18 "similarity": "euclidean | cosine | dotProduct" 19 }, 20 { 21 "type": "filter", 22 "path": "<field-to-index>" 23 }, 24 ... 25 } 26 ] 27 }'
エンドポイントの構文とパラメータの詳細については、「 Atlas Search インデックスを 1 つ作成する 」を参照してください。
例
次のインデックス定義は、Atlas Vector Search インデックスで、 plot_embedding
フィールドを タイプとして、vector
フィールドとgenres
year
フィールドを タイプとしてインデックス化します。filter
plot_embedding
フィールドには、OpenAI のtext-embedding-ada-002
埋め込みモデルを使用して作成された埋め込みが含まれます。 インデックス定義では、 1536
ベクトル次元を指定し、 euclidean
距離を使用して類似性を測定します。
次のインデックス定義は、ベクトル検索を実行するためのベクトル埋め込みフィールドのみをインデックスします。
1 curl --user "{PUBLIC-KEY}:{PRIVATE-KEY}" --digest \ 2 --header "Accept: application/json" \ 3 --header "Content-Type: application/json" \ 4 --include \ 5 --request POST "https://cloud.mongodb.com/api/atlas/v2/groups/{groupId}/clusters/{clusterName}/search/indexes" \ 6 --data ' 7 { 8 "database": "sample_mflix", 9 "collectionName": "embedded_movies", 10 "type": "vectorSearch", 11 "name": "vector_index", 12 "definition: { 13 "fields":[ 14 { 15 "type": "vector", 16 "path": "plot_embedding", 17 "numDimensions": 1536, 18 "similarity": "euclidean" 19 } 20 ] 21 } 22 }'
このインデックス定義は、次のフィールドをインデックスします。
データを事前にフィルタリングするためのstringフィールド(
genres
)と数値フィールド(year
)。事前フィルタリングされたデータに対してベクトル検索を実行するためのベクトル埋め込みフィールド(
plot_embedding
)。
1 curl --user "{PUBLIC-KEY}:{PRIVATE-KEY}" --digest \ 2 --header "Accept: application/json" \ 3 --header "Content-Type: application/json" \ 4 --include \ 5 --request POST "https://cloud.mongodb.com/api/atlas/v2/groups/{groupId}/clusters/{clusterName}/search/indexes" \ 6 --data ' 7 { 8 "database": "sample_mflix", 9 "collectionName": "embedded_movies", 10 "type": "vectorSearch", 11 "name": "vector_index", 12 "definition: { 13 "fields":[ 14 { 15 "type": "vector", 16 "path": "plot_embedding", 17 "numDimensions": 1536, 18 "similarity": "euclidean" 19 }, 20 { 21 "type": "filter", 22 "path": "genres" 23 }, 24 { 25 "type": "filter", 26 "path": "year" 27 } 28 ] 29 } 30 }'
Atlas CLI v 1.14.3以降を使用してコレクションに Atlas Vector Search インデックスを作成するには、次の手順を実行します。
.json
ファイルを作成し、そのファイル内にインデックスを定義します。
インデックス定義は、次の形式のようになります。
1 { 2 "database": "<name-of-database>", 3 "collectionName": "<name-of-collection>", 4 "type": "vectorSearch", 5 "name": "<index-name>", 6 "fields":[ 7 { 8 "type": "vector", 9 "path": "<field-to-index>", 10 "numDimensions": <number-of-dimensions>, 11 "similarity": "euclidean | cosine | dotProduct" 12 }, 13 { 14 "type": "filter", 15 "path": "<field-to-index>" 16 }, 17 ... 18 ] 19 }
例
vector-index.json
という名前のファイルを作成します。
以下のプレースホルダー値を置き換えて、ファイルを保存します。
<name-of-database> | インデックスを作成するコレクションを含むデータベース。 |
<name-of-collection> | インデックスを作成するコレクション。 |
<index-name> | インデックスの名前。 インデックス名を省略すると、Atlas Vector Search はインデックスに vector_index という名前を付けます。 |
<number-of-dimensions> | Atlas Vector Search がインデックス作成時およびクエリ時に強制されるベクトル次元の数。 |
<field-to-index> | インデックスするベクトル フィールドとフィルター フィールド。 |
例
次のインデックス定義をコピーして、 vector-index.json
ファイルに貼り付けます。 次のインデックス定義は、Atlas Vector Search インデックスで、 plot_embedding
フィールドを タイプとして、vector
フィールドとgenres
year
フィールドを タイプとしてインデックス化します。filter
plot_embedding
フィールドには、OpenAI のtext-embedding-ada-002
埋め込みモデルを使用して作成された埋め込みが含まれます。 インデックス定義では、 1536
ベクトル次元を指定し、 euclidean
距離を使用して類似性を測定します。
次のインデックス定義は、ベクトル検索を実行するためのベクトル埋め込みフィールドのみをインデックスします。
1 { 2 "database": "sample_mflix", 3 "collectionName": "embedded_movies", 4 "type": "vectorSearch", 5 "name": "vector_index", 6 "fields": [ 7 { 8 "type": "vector", 9 "path": "plot_embedding", 10 "numDimensions": 1536, 11 "similarity": "euclidean" 12 } 13 ] 14 }
このインデックス定義は、次のフィールドをインデックスします。
データを事前にフィルタリングするためのstringフィールド(
genres
)と数値フィールド(year
)。事前フィルタリングされたデータに対してベクトル検索を実行するためのベクトル埋め込みフィールド(
plot_embedding
)。
1 { 2 "database": "sample_mflix", 3 "collectionName": "embedded_movies", 4 "type": "vectorSearch", 5 "name": "vector_index", 6 "fields":[ 7 { 8 "type": "vector", 9 "path": "plot_embedding", 10 "numDimensions": 1536, 11 "similarity": "euclidean" 12 }, 13 { 14 "type": "filter", 15 "path": "genres" 16 }, 17 { 18 "type": "filter", 19 "path": "year" 20 } 21 ] 22 }
以下のコマンドを実行してインデックスを作成します。
atlas clusters search indexes create --clusterName [cluster_name] --file [vector_index].json
コマンドで、次のプレースホルダー値を置き換えます。
cluster_name
は、インデックスを作成するコレクションを含む Atlas クラスターの名前です。vector_index
は、Atlas Vector Search インデックスのインデックス定義を含むJSONファイルの名前です。
例
atlas clusters search indexes create --clusterName [cluster_name] --file vector-index.json
コマンド構文規則とパラメーターについて詳しくは、Atlas CLI ドキュメントのAtlas クラスター検索インデックス作成コマンド を参照してください。
Atlasで、プロジェクトのGo Clusters{0 ページに します。
まだ表示されていない場合は、希望するプロジェクトを含む組織を選択しますナビゲーション バーのOrganizationsメニュー
まだ表示されていない場合は、ナビゲーション バーのProjectsメニューから目的のプロジェクトを選択します。
Clusters ページがまだ表示されていない場合は、サイドバーの Database をクリックします。
[ Clusters (クラスター) ] ページが表示されます。
GoAtlas Searchクラスターの ページに します。
GoAtlas Searchページには、サイドバー、Data Explorer 、またはクラスターの詳細ページから できます。
サイドバーで、 Services見出しの下のAtlas Searchをクリックします。
[ Select data sourceドロップダウンからクラスターを選択し、[ Go to Atlas Search ] をクリックします。
Atlas Searchページが表示されます。
クラスターの [Browse Collections] ボタンをクリックします。
データベースを展開し、コレクションを選択します。
コレクションのSearch Indexesタブをクリックします。
Atlas Searchページが表示されます。
クラスタの名前をクリックします。
[Atlas Search] タブをクリックします。
Atlas Searchページが表示されます。
Index Nameを入力し、Database and Collection を設定します。
Index Name フィールドにインデックス名を入力します。
インデックス名は、インデックスのタイプに関係なく、名前空間内で一意である必要があります。
例
サンプル インデックスの名前としてvector_indexと入力します。 このコレクションにvector_indexという名前のインデックスがすでに存在する場合は、インデックスに別の名前を入力します。
Database and Collectionセクションで、 データベースを検索し、 コレクションを選択します。
Tip
Data Explorerからこのページに移動した場合、Atlas はData Explorerで選択したデータベースとコレクションを事前選択するため、この手順をスキップできます。
例
Database and Collectionセクションで、
sample_mflix
データベースを検索し、embedded_movies
コレクションを選択します。
インデックスの定義を指定します。
Atlas Vector Search インデックスは次の例のようになります。
1 { 2 "fields":[ 3 { 4 "type": "vector", 5 "path": <field-to-index>, 6 "numDimensions": <number-of-dimensions>, 7 "similarity": "euclidean | cosine | dotProduct" 8 }, 9 { 10 "type": "filter", 11 "path": "<field-to-index>" 12 }, 13 ... 14 ] 15 }
インデックス内のフィールドの詳細については、 「ベクトル検索のフィールドにインデックスを作成する方法」 を参照してください。
例
次のインデックス定義は、Atlas Vector Search インデックスで、 plot_embedding
フィールドを タイプとして、vector
フィールドとgenres
year
フィールドを タイプとしてインデックス化します。filter
plot_embedding
フィールドには、OpenAI のtext-embedding-ada-002
埋め込みモデルを使用して作成された埋め込みが含まれます。 インデックス定義では、 1536
ベクトル次元を指定し、 euclidean
距離を使用して類似性を測定します。
次のインデックス定義は、ベクトル検索を実行するためのベクトル埋め込みフィールドのみをインデックスします。
1 { 2 "fields": [{ 3 "type": "vector", 4 "path": "plot_embedding", 5 "numDimensions": 1536, 6 "similarity": "euclidean" 7 }] 8 }
このインデックス定義は、次のフィールドをインデックスします。
データを事前にフィルタリングするためのstringフィールド(
genres
)と数値フィールド(year
)。事前フィルタリングされたデータに対してベクトル検索を実行するためのベクトル埋め込みフィールド(
plot_embedding
)。
1 { 2 "fields": [{ 3 "type": "vector", 4 "path": "plot_embedding", 5 "numDimensions": 1536, 6 "similarity": "euclidean" 7 }, 8 { 9 "type": "filter", 10 "path": "genres" 11 }, 12 { 13 "type": "filter", 14 "path": "year" 15 }] 16 }
mongosh
v 2.1.2以降を使用してコレクションに Atlas Vector Search インデックスを作成するには、次の手順を実行します。
を使用して Atlasmongosh
クラスターに接続します。
詳しくは、「 mongosh
経由で接続 」を参照してください。
db.collection.createSearchIndex()
メソッドを実行します。
db.collection.createSearchIndex()
メソッドの構文は次のとおりです。
1 db.<collectionName>.createSearchIndex( 2 "<index-name>", 3 "vectorSearch", //index type 4 { 5 fields: [ 6 { 7 "type": "vector", 8 "numDimensions": <number-of-dimensions>, 9 "path": "<field-to-index>", 10 "similarity": "euclidean | cosine | dotProduct" 11 }, 12 { 13 "type": "filter", 14 "path": "<field-to-index>" 15 }, 16 ... 17 ] 18 } 19 );
例
次のインデックス定義は、Atlas Vector Search インデックスで、 plot_embedding
フィールドを タイプとして、vector
フィールドとgenres
year
フィールドを タイプとしてインデックス化します。filter
plot_embedding
フィールドには、OpenAI のtext-embedding-ada-002
埋め込みモデルを使用して作成された埋め込みが含まれます。 インデックス定義では、 1536
ベクトル次元を指定し、 euclidean
距離を使用して類似性を測定します。
次のインデックス定義は、ベクトル検索を実行するためのベクトル埋め込みフィールドのみをインデックスします。
1 db.embedded_movies.createSearchIndex( 2 "vector_index", 3 "vectorSearch", 4 { 5 "fields": [ 6 { 7 "type": "vector", 8 "path": "plot_embedding", 9 "numDimensions": 1536, 10 "similarity": "euclidean" 11 } 12 ] 13 } 14 );
このインデックス定義は、次のフィールドをインデックスします。
データを事前にフィルタリングするためのstringフィールド(
genres
)と数値フィールド(year
)。事前フィルタリングされたデータに対してベクトル検索を実行するためのベクトル埋め込みフィールド(
plot_embedding
)。
1 db.embedded_movies.createSearchIndex( 2 "vector_index", 3 "vectorSearch", 4 { 5 "fields": [ 6 { 7 "type": "vector", 8 "path": "plot_embedding", 9 "numDimensions": 1536, 10 "similarity": "euclidean" 11 }, 12 { 13 "type": "filter", 14 "path": "genres" 15 }, 16 { 17 "type": "filter", 18 "path": "year" 19 } 20 ] 21 } 22 );
MongoDB Goドライバーv 1.16.0以降を使用してコレクション用に Atlas ベクトル検索インデックスを作成するには、次の手順を実行します。
.go
ファイルを作成し、そのファイル内にインデックスを定義します。
1 package main 2 3 import ( 4 "context" 5 "fmt" 6 "log" 7 8 "go.mongodb.org/mongo-driver/mongo" 9 "go.mongodb.org/mongo-driver/mongo/options" 10 ) 11 12 func main() { 13 14 ctx := context.TODO() 15 16 // Replace the placeholder with your Atlas connection string 17 const uri = "<connectionString>" 18 19 // Connect to your Atlas cluster 20 clientOptions := options.Client().ApplyURI(uri) 21 client, err := mongo.Connect(ctx, clientOptions) 22 if err != nil { 23 log.Fatal(err) 24 } 25 defer client.Disconnect(ctx) 26 27 // Set the namespace 28 coll := client.Database("<databaseName>").Collection("<collectionName>") 29 30 // Define the index details 31 type vectorDefinitionField struct { 32 Type string `bson:"type"` 33 Path string `bson:"path"` 34 NumDimensions int `bson:"numDimensions"` 35 Similarity string `bson:"similarity"` 36 } 37 38 type vectorDefinition struct { 39 Fields []vectorDefinitionField `bson:"fields"` 40 } 41 42 indexName := "<indexName>" 43 opts := options.SearchIndexes().SetName(indexName).SetType("vectorSearch") 44 45 indexModel := mongo.SearchIndexModel{ 46 Definition: vectorDefinition{ 47 Fields: []vectorDefinitionField{{ 48 Type: "vector", 49 Path: "<fieldToIndex>", 50 NumDimensions: <numberOfDimensions>, 51 Similarity: "euclidean | cosine | dotProduct"}}, 52 }, 53 Options: opts, 54 } 55 56 // Create the index 57 name, err := coll.SearchIndexes().CreateOne(ctx, indexModel) 58 if err != nil { 59 panic(err) 60 } 61 fmt.Println("Name of Index Created: " + name) 62 }
例
vector-index.go
という名前のファイルを作成します。
以下の値を置き換えて、ファイルを保存します。
<connectionString> | Atlas接続string 。 詳しくは、「ドライバーによる接続 」を参照してください。 |
<databaseName> | インデックスを作成するコレクションを含むデータベース。 |
<collectionName> | インデックスを作成するコレクション。 |
<indexName> | インデックスの名前。 インデックス名を省略すると、Atlas Search はインデックスに vector_index という名前を付けます。 |
<numberOfDimensions> | Atlas Vector Search がインデックス作成時およびクエリ時に強制されるベクトル次元の数。 |
<fieldToIndex> | インデックスするベクトル フィールドとフィルター フィールド。 |
例
以下をコピーしてvector-index.go
ファイルに貼り付け、 <connectionString>
プレースホルダー値を置き換えます。 The following index definition indexes the plot_embedding
field as the vector
type and the genres
and year
fields as the filter
type in an Atlas Vector Search index. plot_embedding
フィールドには、OpenAI のtext-embedding-ada-002
埋め込みモデルを使用して作成された埋め込みが含まれます。 The index definition specifies 1536
vector dimensions and measures similarity using euclidean
distance.
次のインデックス定義は、ベクトル検索を実行するためにベクトル埋め込みフィールド( plot_embedding
)のみをインデックスします。
1 package main 2 3 import ( 4 "context" 5 "fmt" 6 "log" 7 8 "go.mongodb.org/mongo-driver/mongo" 9 "go.mongodb.org/mongo-driver/mongo/options" 10 ) 11 12 func main() { 13 14 ctx := context.TODO() 15 16 // Replace the placeholder with your Atlas connection string 17 const uri = "<connectionString>" 18 19 // Connect to your Atlas cluster 20 clientOptions := options.Client().ApplyURI(uri) 21 client, err := mongo.Connect(ctx, clientOptions) 22 if err != nil { 23 log.Fatal(err) 24 } 25 defer client.Disconnect(ctx) 26 27 // Set the namespace 28 coll := client.Database("sample_mflix").Collection("embedded_movies") 29 30 // Define the index details 31 type vectorDefinitionField struct { 32 Type string `bson:"type"` 33 Path string `bson:"path"` 34 NumDimensions int `bson:"numDimensions"` 35 Similarity string `bson:"similarity"` 36 } 37 38 type vectorDefinition struct { 39 Fields []vectorDefinitionField `bson:"fields"` 40 } 41 42 indexName := "vector_index" 43 opts := options.SearchIndexes().SetName(indexName).SetType("vectorSearch") 44 45 indexModel := mongo.SearchIndexModel{ 46 Definition: vectorDefinition{ 47 Fields: []vectorDefinitionField{{ 48 Type: "vector", 49 Path: "plot_embedding", 50 NumDimensions: 1536, 51 Similarity: "euclidean"}}, 52 }, 53 Options: opts, 54 } 55 56 // Create the index 57 name, err := coll.SearchIndexes().CreateOne(ctx, indexModel) 58 if err != nil { 59 panic(err) 60 } 61 fmt.Println("Name of Index Created: " + name) 62 }
このインデックス定義は、次のフィールドをインデックスします。
データを事前にフィルタリングするためのstringフィールド(
genres
)と数値フィールド(year
)。事前フィルタリングされたデータに対してベクトル検索を実行するためのベクトル埋め込みフィールド(
plot_embedding
)。
1 package main 2 3 import ( 4 "context" 5 "fmt" 6 "log" 7 8 "go.mongodb.org/mongo-driver/bson" 9 "go.mongodb.org/mongo-driver/mongo" 10 "go.mongodb.org/mongo-driver/mongo/options" 11 ) 12 13 func main() { 14 15 ctx := context.TODO() 16 17 // Replace the placeholder with your Atlas connection string 18 const uri = "<connectionString>" 19 20 // Connect to your Atlas cluster 21 clientOptions := options.Client().ApplyURI(uri) 22 client, err := mongo.Connect(ctx, clientOptions) 23 if err != nil { 24 log.Fatal(err) 25 } 26 defer client.Disconnect(ctx) 27 28 // Set the namespace 29 coll := client.Database("sample_mflix").Collection("embedded_movies") 30 indexName := "vector_index" 31 opts := options.SearchIndexes().SetName(indexName).SetType("vectorSearch") 32 33 type vectorDefinitionField struct { 34 Type string `bson:"type"` 35 Path string `bson:"path"` 36 NumDimensions int `bson:"numDimensions"` 37 Similarity string `bson:"similarity"` 38 } 39 40 type filterField struct { 41 Type string `bson:"type"` 42 Path string `bson:"path"` 43 } 44 45 type indexDefinition struct { 46 Fields []vectorDefinitionField `bson:"fields"` 47 } 48 49 vectorDefinition := vectorDefinitionField{ 50 Type: "vector", 51 Path: "plot_embedding", 52 NumDimensions: 1536, 53 Similarity: "euclidean"} 54 genreFilterDefinition := filterField{"filter", "genres"} 55 yearFilterDefinition := filterField{"filter", "year"} 56 57 indexModel := mongo.SearchIndexModel{ 58 Definition: bson.D{{"fields", [3]interface{}{ 59 vectorDefinition, 60 genreFilterDefinition, 61 yearFilterDefinition}}}, 62 Options: opts, 63 } 64 name, err := coll.SearchIndexes().CreateOne(ctx, indexModel) 65 if err != nil { 66 panic(err) 67 } 68 fmt.Println("Name of Index Created: " + name) 69 }
MongoDB Node ドライバーv 6.6.0以降を使用してコレクション用に Atlas Vector Search インデックスを作成するには、次の手順を実行します。
.js
ファイルを作成し、そのファイル内にインデックスを定義します。
1 const { MongoClient } = require("mongodb"); 2 3 // connect to your Atlas deployment 4 const uri = "<connectionString>"; 5 6 const client = new MongoClient(uri); 7 8 async function run() { 9 try { 10 const database = client.db("<databaseName>"); 11 const collection = database.collection("<collectionName>"); 12 13 // define your Atlas Vector Search index 14 const index = { 15 name: "<indexName>", 16 type: "vectorSearch", 17 definition: { 18 "fields": [ 19 { 20 "type": "vector", 21 "numDimensions": <numberOfDimensions>, 22 "path": "<fieldToIndex>", 23 "similarity": "euclidean | cosine | dotProduct" 24 }, 25 { 26 "type": "filter", 27 "path": "<fieldToIndex>" 28 }, 29 ... 30 ] 31 } 32 } 33 34 // run the helper method 35 const result = await collection.createSearchIndex(index); 36 console.log(result); 37 } finally { 38 await client.close(); 39 } 40 } 41 run().catch(console.dir);
例
vector-index.js
という名前のファイルを作成します。
以下の値を置き換えて、ファイルを保存します。
<connectionString> | Atlas接続string 。 詳しくは、「ドライバーによる接続 」を参照してください。 |
<databaseName> | インデックスを作成するコレクションを含むデータベース。 |
<collectionName> | インデックスを作成するコレクション。 |
<indexName> | インデックスの名前。 インデックス名を省略すると、Atlas Search はインデックスに vector_index という名前を付けます。 |
<numberOfDimensions> | Atlas Vector Search がインデックス作成時およびクエリ時に強制されるベクトル次元の数。 |
<fieldToIndex> | インデックスするベクトル フィールドとフィルター フィールド。 |
例
以下をコピーしてvector-index.js
ファイルに貼り付け、 <connectionString>
プレースホルダー値を置き換えます。 The following index definition indexes the plot_embedding
field as the vector
type and the genres
and year
fields as the filter
type in an Atlas Vector Search index. plot_embedding
フィールドには、OpenAI のtext-embedding-ada-002
埋め込みモデルを使用して作成された埋め込みが含まれます。 The index definition specifies 1536
vector dimensions and measures similarity using euclidean
distance.
次のインデックス定義は、ベクトル検索を実行するためにベクトル埋め込みフィールド( plot_embedding
)のみをインデックスします。
1 const { MongoClient } = require("mongodb"); 2 3 // connect to your Atlas deployment 4 const uri = "<connectionString>"; 5 6 const client = new MongoClient(uri); 7 8 async function run() { 9 try { 10 const database = client.db("sample_mflix"); 11 const collection = database.collection("embedded_movies"); 12 13 // define your Atlas Vector Search index 14 const index = { 15 name: "vector_index", 16 type: "vectorSearch", 17 definition: { 18 "fields": [ 19 { 20 "type": "vector", 21 "numDimensions": 1536, 22 "path": "plot_embedding", 23 "similarity": "euclidean" 24 } 25 ] 26 } 27 } 28 29 // run the helper method 30 const result = await collection.createSearchIndex(index); 31 console.log(result); 32 } finally { 33 await client.close(); 34 } 35 } 36 run().catch(console.dir);
このインデックス定義は、次のフィールドをインデックスします。
データを事前にフィルタリングするためのstringフィールド(
genres
)と数値フィールド(year
)。事前フィルタリングされたデータに対してベクトル検索を実行するためのベクトル埋め込みフィールド(
plot_embedding
)。
1 const { MongoClient } = require("mongodb"); 2 3 // connect to your Atlas deployment 4 const uri = "<connectionString>"; 5 6 const client = new MongoClient(uri); 7 8 async function run() { 9 try { 10 const database = client.db("sample_mflix"); 11 const collection = database.collection("embedded_movies"); 12 13 // define your Atlas Vector Search index 14 const index = { 15 name: "vector_index", 16 type: "vectorSearch", 17 definition: { 18 "fields": [ 19 { 20 "type": "vector", 21 "numDimensions": 1536, 22 "path": "plot_embedding", 23 "similarity": "euclidean" 24 }, 25 { 26 "type": "filter", 27 "path": "genres" 28 }, 29 { 30 "type": "filter", 31 "path": "year" 32 } 33 ] 34 } 35 } 36 37 // run the helper method 38 const result = await collection.createSearchIndex(index); 39 console.log(result); 40 } finally { 41 await client.close(); 42 } 43 } 44 run().catch(console.dir);
PyMongoドライバー v 4.7以降を使用してコレクション用に Atlas Vector Search インデックスを作成するには、次の手順を実行します。
.py
ファイルを作成し、そのファイル内にインデックスを定義します。
1 import pymongo 2 from pymongo.mongo_client import MongoClient 3 from pymongo.operations import SearchIndexModel 4 5 # Connect to your Atlas deployment 6 uri = "<connectionString>" 7 client = MongoClient(uri) 8 9 # Access your database and collection 10 database = client["<databaseName>"] 11 collection = database["<collectionName>"] 12 13 # Create your index model, then create the search index 14 search_index_model = SearchIndexModel( 15 definition={ 16 "fields": [ 17 { 18 "type": "vector", 19 "numDimensions": <numberofDimensions>, 20 "path": "<fieldToIndex>", 21 "similarity": "euclidean | cosine | dotProduct" 22 }, 23 { 24 "type": "filter", 25 "path": "<fieldToIndex>" 26 }, 27 ... 28 ] 29 }, 30 name="<index name>", 31 type="vectorSearch", 32 ) 33 34 result = collection.create_search_index(model=search_index_model) 35 print(result)
詳細については、「 create_search_index() 」を参照してください。 使用して複数のドキュメントを挿入できます。
1 from pymongo.mongo_client import MongoClient 2 from pymongo.operations import SearchIndexModel 3 4 def create_indexes(): 5 # Connect to your Atlas deployment 6 uri = "<connectionString>" 7 client = MongoClient(uri) 8 9 # Access your database and collection 10 database = client["<databaseName>"] 11 collection = database["<collectionName>"] 12 13 # Create your index models and add them to an array 14 first_model = SearchIndexModel( 15 definition={ 16 "fields": [ 17 { 18 "type": "vector", 19 "numDimensions": <numberOfDimensions>, 20 "path": "<fieldToIndex>", 21 "similarity": "euclidean | cosine | dotProduct" 22 }, 23 { 24 "type": "filter", 25 "path": "<fieldToIndex>" 26 }, 27 ... 28 ] 29 }, 30 name="<indexName>", 31 type="vectorSearch", 32 ) 33 34 second_model = SearchIndexModel( 35 definition={ 36 "fields": [ 37 { 38 "type": "vector", 39 "numDimensions": <numberOfDimensions>, 40 "path": "<fieldToIndex>", 41 "similarity": "euclidean | cosine | dotProduct" 42 }, 43 { 44 "type": "filter", 45 "path": "<fieldToIndex>" 46 }, 47 ... 48 ] 49 }, 50 name="<index name>", 51 type="vectorSearch", 52 ) 53 54 ... 55 56 idx_models = [first_model, second_model, ...] 57 58 # Create the search indexes 59 result = collection.create_search_indexes(models=idx_models) 60 print(result)
詳細については、「 create_search_indexs() 」を参照してください。 使用して複数のドキュメントを挿入できます。
例
vector-index.py
という名前のファイルを作成します。
以下の値を置き換えて、ファイルを保存します。
<connectionString> | Atlas接続string 。 詳しくは、「ドライバーによる接続 」を参照してください。 |
<databaseName> | インデックスを作成するコレクションを含むデータベース。 |
<collectionName> | インデックスを作成するコレクション。 |
<indexName> | インデックスの名前。 インデックス名を省略すると、Atlas Search はインデックスに vector_index という名前を付けます。 |
<numberOfDimensions> | Atlas Vector Search がインデックス作成時およびクエリ時に強制されるベクトル次元の数。 |
<fieldToIndex> | インデックスするベクトル フィールドとフィルター フィールド。 |
例
以下をコピーしてvector-index.py
に貼り付け、 <connectionString>
プレースホルダー値を置き換えます。 次のインデックス定義は、Atlas Vector Search インデックスで、 フィールドを タイプとしてインデックス化し、 フィールドとplot_embedding
vector
genres
year
フィールドを タイプとしてインデックス化します。filter
plot_embedding
フィールドには、OpenAI のtext-embedding-ada-002
埋め込みモデルを使用して作成された埋め込みが含まれます。 インデックス定義では、 1536
ベクトル次元を指定し、 euclidean
距離を使用して類似性を測定します。
次のインデックス定義は、ベクトル検索を実行するためにベクトル埋め込みフィールド( plot_embedding
)のみをインデックスします。
1 from pymongo.mongo_client import MongoClient 2 from pymongo.operations import SearchIndexModel 3 4 # Connect to your Atlas deployment 5 uri = "<connectionString>" 6 client = MongoClient(uri) 7 8 # Access your database and collection 9 database = client["sample_mflix"] 10 collection = database["embedded_movies"] 11 12 # Create your index model, then create the search index 13 search_index_model = SearchIndexModel( 14 definition={ 15 "fields": [ 16 { 17 "type": "vector", 18 "path": "plot_embedding", 19 "numDimensions": 1536, 20 "similarity": "euclidean" 21 } 22 ] 23 }, 24 name="vector_index", 25 type="vectorSearch", 26 ) 27 28 result = collection.create_search_index(model=search_index_model) 29 print(result)
このインデックス定義は、次のフィールドをインデックスします。
データを事前にフィルタリングするためのstringフィールド(
genres
)と数値フィールド(year
)。事前フィルタリングされたデータに対してベクトル検索を実行するためのベクトル埋め込みフィールド(
plot_embedding
)。
1 from pymongo.mongo_client import MongoClient 2 from pymongo.operations import SearchIndexModel 3 4 # Connect to your Atlas deployment 5 uri = "<connectionString>" 6 client = MongoClient(uri) 7 8 # Access your database and collection 9 database = client["sample_mflix"] 10 collection = database["embedded_movies"] 11 12 # Create your index model, then create the search index 13 search_index_model = SearchIndexModel( 14 definition={ 15 "fields": [ 16 { 17 "type": "vector", 18 "path": "plot_embedding", 19 "numDimensions": 1536, 20 "similarity": "euclidean" 21 }, 22 { 23 "type": "filter", 24 "path": "genres" 25 }, 26 { 27 "type": "filter", 28 "path": "year" 29 } 30 ] 31 }, 32 name="vector_index", 33 type="vectorSearch", 34 ) 35 36 result = collection.create_search_index(model=search_index_model) 37 print(result)
Atlas Vector Search インデックスの表示
Atlas UI、Atlas 管理 API、Atlas CLI、 mongosh
、またはサポートされているMongoDB ドライバーから、すべてのコレクションの Atlas Vector Search インデックスを表示できます。
必要なアクセス権
Atlas Vector Search インデックスを表示するには、 Project Search Index Editor
以上のロールが必要です。
注意
mongosh
コマンドまたはドライバーヘルパーメソッドを使用して、すべての Atlas クラスター階層で Atlas Search インデックスを取得できます。
手順
➤ [言語の選択]ドロップダウン メニューを使用して、このセクション内の例の言語を設定します。
Atlas Administration API を使用してコレクションのすべての Atlas Vector Search インデックスを検索するには、データベースとコレクションの名前を指定して Atlas Search indexes
エンドポイントにGET
リクエストを送信します。
1 curl --user "{PUBLIC-KEY}:{PRIVATE-KEY}" --digest \ 2 --header "Accept: application/json" \ 3 --include \ 4 --request GET "https://cloud.mongodb.com/api/atlas/v2/groups/{groupId}/clusters/{clusterName}/search/indexes/{databaseName}/{collectionName}"
エンドポイントの構文とパラメータの詳細については、「 1 つのコレクションのすべての Atlas Search インデックスを返す 」を参照してください。
Administration Atlas Vector Searchを使用してコレクション用の インデックスをAtlasAPI 1GET
つ取得するには、取得対象のインデックスの一意のAtlas Searchindexes
IDまたは名前(4 行)を指定して エンドポイントに リクエストを送信します。
1 curl --user "{PUBLIC-KEY}:{PRIVATE-KEY}" --digest \ 2 --header "Accept: application/json" \ 3 --include \ 4 --request GET "https://cloud.mongodb.com/api/atlas/v2/groups/{groupId}/clusters/{clusterName}/search/indexes/{indexId} | https://cloud.mongodb.com/api/atlas/v2/groups/{groupId}/clusters/{clusterName}/search/indexes/{databaseName}/{collectionName}/{indexName|indexId}"
エンドポイントの構文とパラメータの詳細については、 「 名前で 1 つの取得 」および「 ID で 1 つの取得 」を参照してください。
Atlas CLI を使用してコレクションの Atlas Vector Search インデックスを返すには、次の手順を実行します。
次のコマンドを実行して、コレクションのインデックスを検索します。
atlas clusters search indexes list --clusterName [cluster_name] --db <db-name> --collection <collection-name>
コマンドで、次のプレースホルダー値を置き換えます。
cluster-name
- インデックス作成されたコレクションを含む Atlas クラスターの名前。db-name
- インデックスを検索するコレクションを含むデータベースの名前。collection-name
- インデックスを検索するコレクションの名前。
コマンド構文規則とパラメーターについて詳しくは、Atlas CLI ドキュメントのAtlas クラスター検索インデックス リストコマンド を参照してください。
Atlas で、プロジェクトの [Clusters] ページに移動します。
まだ表示されていない場合は、希望するプロジェクトを含む組織を選択しますナビゲーション バーのOrganizationsメニュー
まだ表示されていない場合は、ナビゲーション バーのProjectsメニューから目的のプロジェクトを選択します。
Clusters ページがまだ表示されていない場合は、サイドバーの Database をクリックします。
[ Clusters (クラスター) ] ページが表示されます。
GoAtlas Searchクラスターの ページに します。
GoAtlas Searchページには、サイドバー、Data Explorer 、またはクラスターの詳細ページから できます。
サイドバーで、 Services見出しの下のAtlas Searchをクリックします。
[ Select data sourceドロップダウンからクラスターを選択し、[ Go to Atlas Search ] をクリックします。
Atlas Searchページが表示されます。
クラスターの [Browse Collections] ボタンをクリックします。
データベースを展開し、コレクションを選択します。
コレクションのSearch Indexesタブをクリックします。
Atlas Searchページが表示されます。
クラスタの名前をクリックします。
[Atlas Search] タブをクリックします。
Atlas Searchページが表示されます。
ページには、ページのインデックスに関する次の詳細が表示されます。
名前 | インデックスを識別するラベル。 |
インデックス タイプ | Atlas Search または Atlas Vector Search インデックスを示すラベル。 値には、次のものが含まれます。
|
インデックス フィールド | このインデックスがインデックスを作成するフィールドを含むリスト。 |
ステータス | クラスターのプライマリ ノード上のインデックスの現在の状態。 有効な値については、「インデックス ステータス 」を参照してください。 |
サイズ | プライマリ ノードのインデックスのサイズ。 |
ドキュメント | コレクション内のドキュメントの総数のうち、インデックスが作成されたドキュメントの数。 |
アクション | インデックスに対して実行できるアクション。 ここでは、次の作業が可能です。 Search Tester UI では、 |
mongosh
を使用してコレクションの Atlas Vector Search インデックスを表示するには、次の手順を実行します。
を使用して Atlasmongosh
クラスターに接続します。
詳しくは、「 mongosh
経由で接続 」を参照してください。
db.collection.getSearchIndexes()
メソッドを実行します。
db.collection.getSearchIndexes()
メソッドの構文は次のとおりです。
1 db.<collectionName>.getSearchIndexes( "<index-name>" );
MongoDB Goドライバーv 1.16.0以降を使用してコレクションの Atlas ベクトル検索インデックスを表示するには、次の手順を実行します。
.go
ファイルを作成し、SearchIndexes().List()
メソッドを使用してインデックスを取得します。
1 package main 2 3 import ( 4 "context" 5 "encoding/json" 6 "fmt" 7 "log" 8 9 "go.mongodb.org/mongo-driver/bson" 10 "go.mongodb.org/mongo-driver/mongo" 11 "go.mongodb.org/mongo-driver/mongo/options" 12 ) 13 14 func main() { 15 16 ctx := context.TODO() 17 18 // Replace the placeholder with your Atlas connection string 19 const uri = "<connectionString>" 20 21 // Connect to your Atlas cluster 22 clientOptions := options.Client().ApplyURI(uri) 23 client, err := mongo.Connect(ctx, clientOptions) 24 if err != nil { 25 log.Fatal(err) 26 } 27 defer client.Disconnect(ctx) 28 29 // Set the namespace 30 coll := client.Database("<databaseName>").Collection("<collectionName>") 31 32 // Specify the options for the index to retrieve 33 indexName := "<indexName>" 34 opts := options.SearchIndexes().SetName(indexName) 35 36 // Get the index 37 cursor, err := coll.SearchIndexes().List(ctx, opts) 38 39 // Print the index details to the console as JSON 40 var results []bson.M 41 if err := cursor.All(ctx, &results); err != nil { 42 log.Panic(err) 43 } 44 res, _ := json.Marshal(results) 45 fmt.Println(string(res)) 46 }
以下の値を置き換えて、ファイルを保存します。
<connectionString> | Atlas 接続文字列。詳しくは、「ドライバーによる接続」を参照してください。 |
<databaseName> | コレクションを含むデータベース。 |
<collectionName> | インデックスを検索するコレクション。 |
<indexName> | 特定のインデックスを検索する場合は、インデックスの名前。 コレクションのすべてのインデックスを返すには、この値を省略します。 |
MongoDB Node ドライバーv 6.6.0以降を使用してコレクションの Atlas Vector Search インデックスを表示するには、次の手順を実行します。
.js
ファイルを作成し、listSearchIndexes()
メソッドを使用してインデックスを取得します。
1 const { MongoClient } = require("mongodb"); 2 3 // connect to your Atlas deployment 4 const uri = "<connectionString>"; 5 6 const client = new MongoClient(uri); 7 8 async function run() { 9 try { 10 const database = client.db("<databaseName>"); 11 const collection = database.collection("<collectionName>"); 12 13 // run the helper method 14 const result = await collection.listSearchIndexes("<indexName>").toArray(); 15 console.log(result); 16 } finally { 17 await client.close(); 18 } 19 } 20 run().catch(console.dir);
以下の値を置き換えて、ファイルを保存します。
<connectionString> | Atlas 接続文字列。詳しくは、「ドライバーによる接続」を参照してください。 |
<databaseName> | コレクションを含むデータベース。 |
<collectionName> | インデックスを検索するコレクション。 |
<indexName> | 特定のインデックスを検索する場合は、インデックスの名前。 コレクションのすべてのインデックスを返すには、この値を省略します。 |
PyMongoドライバー v 4.7以降を使用してコレクションの Atlas Vector Search インデックスを表示するには、次の手順を実行します。
.py
ファイルを作成し、list_search_indexes()
メソッドを使用してコレクションのインデックスを取得します。
1 from pymongo.mongo_client import MongoClient 2 3 # Connect to your Atlas deployment 4 uri = "<connectionString>" 5 client = MongoClient(uri) 6 7 # Access your database and collection 8 database = client["<databaseName>"] 9 collection = database["<collectionName>"] 10 11 # Get a list of the collection's search indexes and print them 12 cursor = collection.list_search_indexes() 13 for index in cursor: 14 print(index)
詳しくは、 list_search_indexs() を参照してください 使用して複数のドキュメントを挿入できます。
以下の値を置き換えて、ファイルを保存します。
<connectionString> | Atlas 接続文字列。詳しくは、「ドライバーによる接続」を参照してください。 |
<databaseName> | コレクションを含むデータベースの名前。 |
<collectionName> | コレクションの名前。 |
Atlas Vector Search インデックスの編集
既存の Atlas Vector Search インデックスのインデックス定義は、Atlas UI、Atlas Administration API、Atlas CLI、 mongosh
、またはサポートされているMongoDB ドライバーから変更できます。 インデックスの名前を変更したり、インデックス タイプを変更したりすることはできません。 インデックスの名前またはタイプを変更する必要がある場合は、新しいインデックスを作成し、古いインデックスを削除する必要があります。
重要
インデックスを編集すると、Atlas Vector Search はそのインデックスを再構築します。 インデックスの再ビルド中も、古いインデックス定義を使用してベクトル検索クエリを引き続き実行できます。 インデックスの再構築が完了すると、古いインデックスは自動的に置き換えられます。 このプロセスは、標準の Atlas Search インデックスと同じプロセスに従います。
必要なアクセス権
Atlas Vector Search インデックスを編集するには、 Project Search Index Editor
以上のロールが必要です。
注意
mongosh
コマンドまたはドライバーヘルパーメソッドを使用して、すべての Atlas クラスター階層の Atlas Search インデックスを編集できます。
手順
➤ [言語を選択 ] ドロップダウン メニューを使用して、インデックスの編集に使用するクライアントを選択します。
Atlas Administration API を使用してコレクションの Atlas Vector Search インデックスを編集するには、編集するインデックスの一意の ID または名前( 4行)を指定して Atlas Search indexes
エンドポイントにPATCH
リクエストを送信します。
1 curl --user "{PUBLIC-KEY}:{PRIVATE-KEY}" --digest --include \ 2 --header "Accept: application/json" \ 3 --header "Content-Type: application/json" \ 4 --request PATCH "https://cloud.mongodb.com/api/atlas/v2/groups/{groupId}/clusters/{clusterName}/search/indexes/{indexId} | https://cloud.mongodb.com/api/atlas/v2/groups/{groupId}/clusters/{clusterName}/search/indexes/{databaseName}/{collectionName}/{indexName|indexId}" \ 5 --data' 6 { 7 "database": "<name-of-database>", 8 "collectionName": "<name-of-collection>", 9 "type": "vectorSearch", 10 "name": "<index-name>", 11 "definition": { 12 "fields":[ 13 { 14 "type": "vector", 15 "path": <field-to-index>, 16 "numDimensions": <number-of-dimensions>, 17 "similarity": "euclidean | cosine | dotProduct" 18 }, 19 { 20 "type": "filter", 21 "path": "<field-to-index>" 22 }, 23 ... 24 } 25 ] 26 }'
エンドポイントの構文とパラメータの詳細については、「名前を使用して 1 つの更新 」および「 ID を使用して 1 つの更新 」を参照してください。
Atlas CLI を使用してコレクションの Atlas Vector Search インデックスを編集するには、次の手順を実行します。
.json
ファイルを作成し、ファイル内でインデックスへの変更を定義します。
インデックス定義は、次の形式のようになります。
1 { 2 "database": "<name-of-database>", 3 "collectionName": "<name-of-collection>", 4 "type": "vectorSearch", 5 "name": "<index-name>", 6 "fields":[ 7 { 8 "type": "vector", 9 "path": "<field-to-index>", 10 "numDimensions": <number-of-dimensions>, 11 "similarity": "euclidean | cosine | dotProduct" 12 }, 13 { 14 "type": "filter", 15 "path": "<field-to-index>" 16 }, 17 ... 18 ] 19 }
以下のプレースホルダー値を置き換えて、ファイルを保存します。
<name-of-database> | インデックスを作成するコレクションを含むデータベース。 |
<name-of-collection> | インデックスを作成するコレクション。 |
<index-name> | インデックスの名前。 インデックス名を省略すると、Atlas Vector Search はインデックスに vector_index という名前を付けます。 |
<number-of-dimensions> | Atlas Vector Search がインデックス作成時およびクエリ時に強制されるベクトル次元の数。 |
<field-to-index> | インデックスするベクトル フィールドとフィルター フィールド。 |
以下のコマンドを実行して、インデックスを更新します。
atlas clusters search indexes update <indexId> --clusterName [cluster_name] --file [vector-_index].json
コマンドで、次のプレースホルダー値を置き換えます。
cluster_name
- インデックスを更新するコレクションを含む Atlas クラスターの名前。vector_index
- Atlas Vector Search インデックスの変更されたインデックス定義を含むJSONファイルの名前
コマンド構文規則とパラメーターについて詳しくは、Atlas CLI ドキュメントのAtlas クラスター検索インデックス アップデートコマンド を参照してください。
Atlas で、プロジェクトの [Clusters] ページに移動します。
まだ表示されていない場合は、希望するプロジェクトを含む組織を選択しますナビゲーション バーのOrganizationsメニュー
まだ表示されていない場合は、ナビゲーション バーのProjectsメニューから目的のプロジェクトを選択します。
Clusters ページがまだ表示されていない場合は、サイドバーの Database をクリックします。
[ Clusters (クラスター) ] ページが表示されます。
GoAtlas Searchクラスターの ページに します。
GoAtlas Searchページには、サイドバー、Data Explorer 、またはクラスターの詳細ページから できます。
サイドバーで、 Services見出しの下のAtlas Searchをクリックします。
[ Select data sourceドロップダウンからクラスターを選択し、[ Go to Atlas Search ] をクリックします。
Atlas Searchページが表示されます。
クラスターの [Browse Collections] ボタンをクリックします。
データベースを展開し、コレクションを選択します。
コレクションのSearch Indexesタブをクリックします。
Atlas Searchページが表示されます。
クラスタの名前をクリックします。
[Atlas Search] タブをクリックします。
Atlas Searchページが表示されます。
インデックスを編集します。
編集する
vectorSearch
型インデックスを見つけます。そのインデックスのActionsドロップダウンから [ Edit Indexをクリックします。
現在の構成設定を確認し、必要に応じて編集します。
Atlas Vector Search インデックスのフィールドの詳細については、 「ベクトル検索のフィールドにインデックスを作成する方法」 を参照してください。
変更を適用するには、 Saveをクリックします。
インデックスのステータスがActiveからBuildingに変わります。 この状態では、アップデートされたインデックスが使用可能になるまで、Atlas Vector Search が古いインデックスを削除しないため、古いインデックスを使用し続けることができます。 ステータスがActiveに戻ったら、変更されたインデックスは使用可能になります。
mongosh
を使用してコレクションの Atlas Vector Search インデックスを編集するには、次の手順を実行します。
を使用して Atlasmongosh
クラスターに接続します。
詳しくは、「 mongosh
経由で接続 」を参照してください。
db.collection.updateSearchIndex()
メソッドを実行します。
db.collection.updateSearchIndex()
メソッドの構文は次のとおりです。
1 db.<collectionName>.updateSearchIndex( 2 "<index-name>", 3 { 4 fields: [ 5 { 6 "type": "vector", 7 "numDimensions": <number-of-dimensions>, 8 "path": "<field-to-index>", 9 "similarity": "euclidean | cosine | dotProduct" 10 }, 11 { 12 "type": "filter", 13 "path": "<field-to-index>" 14 }, 15 ... 16 ] 17 } 18 );
MongoDB Goドライバーv 1.16.0以降を使用してコレクションの Atlas ベクトル検索インデックスを更新するには、次の手順を実行します。
go
ファイルを作成し、ファイルでインデックスの変更を定義します。
1 package main 2 3 import ( 4 "context" 5 "fmt" 6 "log" 7 8 "go.mongodb.org/mongo-driver/mongo" 9 "go.mongodb.org/mongo-driver/mongo/options" 10 ) 11 12 func main() { 13 14 ctx := context.TODO() 15 16 // Replace the placeholder with your Atlas connection string 17 const uri = "<connection-string>" 18 19 // Connect to your Atlas cluster 20 clientOptions := options.Client().ApplyURI(uri) 21 client, err := mongo.Connect(ctx, clientOptions) 22 if err != nil { 23 log.Fatal(err) 24 } 25 defer client.Disconnect(ctx) 26 27 // Set the namespace 28 coll := client.Database("<databaseName>").Collection("<collectionName>") 29 indexName := "<indexName>" 30 31 type vectorDefinitionField struct { 32 Type string `bson:"type"` 33 Path string `bson:"path"` 34 NumDimensions int `bson:"numDimensions"` 35 Similarity string `bson:"similarity"` 36 } 37 38 type vectorDefinition struct { 39 Fields []vectorDefinitionField `bson:"fields"` 40 } 41 42 definition := vectorDefinition{ 43 Fields: []vectorDefinitionField{{ 44 Type: "vector", 45 Path: "<fieldToIndex>", 46 NumDimensions: <numberOfDimensions>, 47 Similarity: "euclidean | cosine | dotProduct"}}, 48 } 49 coll.SearchIndexes().UpdateOne(ctx, indexName, definition) 50 51 if err != nil { 52 panic(err) 53 } 54 55 fmt.Println("Successfully updated the search index") 56 }
以下の値を置き換えて、ファイルを保存します。
<connectionString> | Atlas接続string 。 詳しくは、「ドライバーによる接続 」を参照してください。 |
<databaseName> | インデックスを作成するコレクションを含むデータベース。 |
<collectionName> | インデックスを作成するコレクション。 |
<indexName> | インデックスの名前。 インデックス名を省略すると、Atlas Search はインデックスに vector_index という名前を付けます。 |
<numberOfDimensions> | Atlas Vector Search がインデックス作成時およびクエリ時に強制されるベクトル次元の数。 |
<fieldToIndex> | インデックスするベクトル フィールドとフィルター フィールド。 |
MongoDB Node ドライバーv 6.6.0以降を使用してコレクションの Atlas Vector Search インデックスを更新するには、次の手順を実行します。
.js
ファイルを作成し、ファイルでインデックスの変更を定義します。
1 const { MongoClient } = require("mongodb"); 2 3 // connect to your Atlas deployment 4 const uri = "<connection-string>"; 5 6 const client = new MongoClient(uri); 7 8 async function run() { 9 try { 10 const database = client.db("<databaseName>"); 11 const collection = database.collection("<collectionName>"); 12 13 // define your Atlas Search index 14 const index = { 15 name: "<indexName>", 16 type: "vectorSearch", 17 //updated search index definition 18 definition: { 19 "fields": [ 20 { 21 "type": "vector", 22 "numDimensions": <numberOfDimensions>, 23 "path": "<field-to-index>", 24 "similarity": "euclidean | cosine | dotProduct" 25 }, 26 { 27 "type": "filter", 28 "path": "<fieldToIndex>" 29 }, 30 ... 31 ] 32 } 33 } 34 35 // run the helper method 36 await collection.updateSearchIndex("<index-name>", index); 37 } finally { 38 await client.close(); 39 } 40 } 41 run().catch(console.dir);
以下の値を置き換えて、ファイルを保存します。
<connectionString> | Atlas接続string 。 詳しくは、「ドライバーによる接続 」を参照してください。 |
<databaseName> | インデックスを作成するコレクションを含むデータベース。 |
<collectionName> | インデックスを作成するコレクション。 |
<indexName> | インデックスの名前。 インデックス名を省略すると、Atlas Search はインデックスに vector_index という名前を付けます。 |
<numberOfDimensions> | Atlas Vector Search がインデックス作成時およびクエリ時に強制されるベクトル次元の数。 |
<fieldToIndex> | インデックスするベクトル フィールドとフィルター フィールド。 |
PyMongoドライバー v 4.7以降を使用してコレクションの Atlas Vector Search インデックスを更新するには、次の手順を実行します。
.py
ファイルを作成し、ファイルでインデックスの変更を定義します。
1 from pymongo.mongo_client import MongoClient 2 3 # Connect to your Atlas deployment 4 uri = "<connectionString>" 5 client = MongoClient(uri) 6 7 # Access your database and collection 8 database = client["<databaseName>"] 9 collection = database["<collectionName>"] 10 11 definition = { 12 "fields": [ 13 { 14 "type": "vector", 15 "numDimensions": <numberofDimensions>, 16 "path": "<fieldToIndex>", 17 "similarity": "euclidean | cosine | dotProduct" 18 }, 19 { 20 "type": "filter", 21 "path": "<fieldToIndex>" 22 }, 23 ... 24 ] 25 } 26 27 # Update your search index 28 collection.update_search_index("<indexName>", definition)
詳しくは、 update_search_index() 使用して複数のドキュメントを挿入できます。
以下の値を置き換えて、ファイルを保存します。
<connectionString> | Atlas接続string 。 詳しくは、「ドライバーによる接続 」を参照してください。 |
<databaseName> | インデックスを作成するコレクションを含むデータベース。 |
<collectionName> | インデックスを作成するコレクション。 |
<indexName> | インデックスの構成。 インデックス名を省略すると、Atlas Search はインデックスに vector_index という名前を付けます。 |
<numberOfDimensions> | Atlas Vector Search がインデックス作成時およびクエリ時に強制されるベクトル次元の数。 |
<fieldToIndex> | インデックスするベクトル フィールドとフィルター フィールド。 |
Atlas Vector Search インデックスの削除
Atlas Vector Search インデックスは、Atlas UI、Atlas Administration API、Atlas CLI、 mongosh
、またはサポートされているMongoDB ドライバーからいつでも削除できます。
必要なアクセス権
Atlas Vector Search インデックスを削除するには、 Project Search Index Editor
以上のロールが必要です。
注意
mongosh
コマンドまたはドライバーヘルパーメソッドを使用して、すべての Atlas クラスター階層上の Atlas Search インデックスを削除できます。
手順
➤ [言語を選択 ] ドロップダウン メニューを使用して、インデックスを削除するために使用するクライアントを選択します。
Atlas Administration API を使用してコレクションの Atlas Vector Search インデックスを削除するには、一意の ID または削除するインデックスの名前を指定して、Atlas Search indexes
エンドポイントにDELETE
リクエストを送信します。
1 curl --user "{PUBLIC-KEY}:{PRIVATE-KEY}" --digest \ 2 --header "Accept: application/json" \ 3 --include \ 4 --request DELETE "https://cloud.mongodb.com/api/atlas/v2/groups/{groupId}/clusters/{clusterName}/search/indexes/{indexId} | https://cloud.mongodb.com/api/atlas/v2/groups/{groupId}/clusters/{clusterName}/search/indexes/{databaseName}/{collectionName}/{indexName|indexId}"
エンドポイントの構文とパラメータの詳細については、「名前を使用して1 つの検索インデックスを削除 」および「 ID を使用して 1 つの検索インデックスを削除 」を参照してください。
Atlas CLI を使用してコレクションの Atlas Vector Search インデックスを削除するには、次の手順を実行します。
コマンドを実行してインデックスを削除します。
atlas clusters search indexes delete <indexId> [options]
コマンドで、 indexId
プレースホルダー値を削除するインデックスの一意の識別子に置き換えます。
コマンド構文規則とパラメーターについて詳しくは、Atlas CLI ドキュメントのAtlas クラスター検索インデックスの削除コマンドを参照してください。
Atlas で、プロジェクトの [Clusters] ページに移動します。
まだ表示されていない場合は、希望するプロジェクトを含む組織を選択しますナビゲーション バーのOrganizationsメニュー
まだ表示されていない場合は、ナビゲーション バーのProjectsメニューから目的のプロジェクトを選択します。
Clusters ページがまだ表示されていない場合は、サイドバーの Database をクリックします。
[ Clusters (クラスター) ] ページが表示されます。
GoAtlas Searchクラスターの ページに します。
GoAtlas Searchページには、サイドバー、Data Explorer 、またはクラスターの詳細ページから できます。
サイドバーで、 Services見出しの下のAtlas Searchをクリックします。
[ Select data sourceドロップダウンからクラスターを選択し、[ Go to Atlas Search ] をクリックします。
Atlas Searchページが表示されます。
クラスターの [Browse Collections] ボタンをクリックします。
データベースを展開し、コレクションを選択します。
コレクションのSearch Indexesタブをクリックします。
Atlas Searchページが表示されます。
クラスタの名前をクリックします。
[Atlas Search] タブをクリックします。
Atlas Searchページが表示されます。
mongosh
を使用してコレクションの Atlas Vector Search インデックスを削除するには、次の手順を実行します。
を使用して Atlasmongosh
クラスターに接続します。
詳しくは、「 mongosh
経由で接続 」を参照してください。
db.collection.dropSearchIndex()
メソッドを実行します。
db.collection.dropSearchIndex()
メソッドの構文は次のとおりです。
1 db.<collectionName>.dropSearchIndex( "<index-name>" );
MongoDB Goドライバーv 1.16.0以降を使用してコレクションの Atlas ベクトル検索インデックスを削除するには、次の手順を実行します。
.go
ファイルを作成し、SearchIndexes().DropOne()
メソッドを使用してインデックスを削除します。
1 package main 2 3 import ( 4 "context" 5 "fmt" 6 "log" 7 8 "go.mongodb.org/mongo-driver/mongo" 9 "go.mongodb.org/mongo-driver/mongo/options" 10 ) 11 12 func main() { 13 14 ctx := context.TODO() 15 16 // Replace the placeholder with your Atlas connection string 17 const uri = "<connectionString>" 18 19 // Connect to your Atlas cluster 20 clientOptions := options.Client().ApplyURI(uri) 21 client, err := mongo.Connect(ctx, clientOptions) 22 if err != nil { 23 log.Fatal(err) 24 } 25 defer client.Disconnect(ctx) 26 27 // Set the namespace 28 coll := client.Database("<databaseName>").Collection("<collectionName>") 29 indexName := "<indexName>" 30 31 coll.SearchIndexes().DropOne(ctx, indexName) 32 33 if err != nil { 34 panic(err) 35 } 36 37 fmt.Println("Successfully dropped the Vector Search index") 38 }
以下の値を置き換えて、ファイルを保存します。
<connectionString> | Atlas 接続文字列。詳しくは、「ドライバーによる接続」を参照してください。 |
<databaseName> | インデックスを作成するコレクションを含むデータベース。 |
<collectionName> | インデックスを作成するコレクション。 |
<indexName> | インデックスの名前。インデックス名を省略すると、Atlas Search はインデックスに vector_index という名前を付けます。 |
MongoDB Node ドライバーv 6.6.0以降を使用してコレクションの Atlas Vector Search インデックスを削除するには、次の手順を実行します。
.js
ファイルを作成し、dropSearchIndex()
メソッドを使用してインデックスを削除します。
1 const { MongoClient } = require("mongodb"); 2 3 // connect to your Atlas deployment 4 const uri = "<connectionString>"; 5 6 const client = new MongoClient(uri); 7 8 async function run() { 9 try { 10 const database = client.db("<databaseName>"); 11 const collection = database.collection("<collectionName>"); 12 13 // run the helper method 14 await collection.dropSearchIndex("<indexName>"); 15 16 } finally { 17 await client.close(); 18 } 19 } 20 run().catch(console.dir);
以下の値を置き換えて、ファイルを保存します。
<connectionString> | Atlas 接続文字列。詳しくは、「ドライバーによる接続」を参照してください。 |
<databaseName> | インデックスを作成するコレクションを含むデータベース。 |
<collectionName> | インデックスを作成するコレクション。 |
<indexName> | インデックスの名前。インデックス名を省略すると、Atlas Search はインデックスに vector_index という名前を付けます。 |
PyMongoドライバー v 4.7以降を使用してコレクションの Atlas Vector Search インデックスを削除するには、次の手順を実行します。
.py
ファイルを作成し、drop_search_index()
メソッドを使用してインデックスを削除します。
1 from pymongo.mongo_client import MongoClient 2 3 # Connect to your Atlas deployment 4 uri = "<connectionString>" 5 client = MongoClient(uri) 6 7 # Access your database and collection 8 database = client["<databaseName>"] 9 collection = database["<collectionName>"] 10 11 # Delete your search index 12 collection.drop_search_index("<indexName>")
詳細については、 drop_search_index() 使用して複数のドキュメントを挿入できます。
以下の値を置き換えて、ファイルを保存します。
<connectionString> | Atlas 接続文字列。詳しくは、「ドライバーによる接続」を参照してください。 |
<databaseName> | コレクションを含むデータベースの名前。 |
<collectionName> | コレクションの名前。 |
<indexName> | 削除するインデックスの名前。 |
インデックスのステータス
Atlas Vector Search インデックスを作成すると、 Status列にはクラスターのプライマリ ノードにおけるインデックスの現在の状態が表示されます。 クラスタ内のすべてのノードにおけるインデックスの状態を表示するには、ステータスの下にあるView status detailsリンクをクリックします。
Status 列が Active の場合、インデックスは使用可能です。そのほかの状態の場合は、インデックスに対するクエリに不完全な結果が返される場合があります。
ステータス | 説明 |
---|---|
開始前 | Atlas によるインデックスの構築は開始されていません。 |
最初の同期 | Atlas はインデックスを構築中、または編集したインデックスを再構築中です。インデックスがこの状態のとき、次の状況が発生します。
|
アクティブ | インデックスをいつでも使用できます。 |
リカバリ中 | レプリケーションでエラーが発生しました。この状態は通常、現在のレプリケーション ポイントが mongod oplog で使用できなくなった場合に発生します。既存のインデックスがアップデートされ、ステータスが Active に変わるまで、引き続き既存のインデックスをクエリできます。View status detailsモーダル ウィンドウのエラーを使用して、問題のトラブルシューティングを行います。詳しくは、「Atlas Search の問題の修正」を参照してください。 |
失敗 | Atlas によるインデックスの構築は失敗しました。View status details モーダル ウィンドウのエラーを使用して、問題のトラブルシューティングを行います。詳しくは、「Atlas Search の問題の修正」を参照してください。 |
削除中 | Atlas はクラスター ノードからインデックスを削除しています。 |
Atlas によるインデックスの構築中および構築の完了後、Documents 列にはインデックス済みドキュメントの割合と数が表示されます。また、この列には、コレクション内の総ドキュメント数も表示されます。
次のセクションでは、 から Atlas Vector Search インデックスを作成および管理する方法について説明します。