Docs Menu
Docs Home
/
MongoDB Atlas
/

ベクトルAtlas Searchのフィールドにインデックスを付ける方法

項目一覧

  • Considerations
  • 構文
  • vector型について
  • filter型について
  • Atlas Vector Search インデックス フィールド
  • Atlas Vector Search インデックスの作成
  • 前提条件
  • 必要なアクセス権
  • 無料クラスターと共有クラスターの制限
  • 手順
  • Atlas Vector Search インデックスの表示
  • 必要なアクセス権
  • 手順
  • Atlas Vector Search インデックスの編集
  • 必要なアクセス権
  • 手順
  • Atlas Vector Search インデックスの削除
  • 必要なアクセス権
  • 手順
  • インデックスのステータス

vectorSearchタイプを使用して、 $vectorSearchクエリを実行中のフィールドにインデックスを作成できます。 クエリするベクトル埋め込みと、データを事前フィルタリングするために使用するブール値、日付、ObjectId、数値、string、または UUID 値のインデックスを定義できます。 データをフィルタリングすると、セマンティック検索の範囲を絞り込み、マルチテナント環境など特定のベクトル埋め込みが比較の対象とならないようにするのに役立ちます。

Atlas Vector Search インデックスを 作成 するには、Atlas UI、Atlas Administration API、Atlas CLI、mongosh 、またはサポートされている MongoDB ドライバー を使用できます。

注意

非推奨のknnBeta演算子を使用して、 vectorSearch型のインデックス定義を使用してインデックス付けされたフィールドをクエリすることはできません。

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}

ベクトル$vectorSearch フィールドには、 パイプライン ステージを使用してクエリを実行するための BSON データ型の数値の配列が含まれている必要があります。doublefields配列内でベクトル フィールドを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}

オプションで、ブール値、日付、数値、 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 インデックス定義には次のフィールドがあります。

オプション
タイプ
必要性
目的
fields
ドキュメントの配列
必須
インデックスするベクトル フィールドとフィルター フィールド(ドキュメントごとに 1 つ)。 少なくとも 1 つのドキュメントには、ベクトル フィールドのフィールド定義が含まれている必要があります。 オプションで、ブール値、日付、数値、 ObjectId 、 string 、および UUID フィールドをドキュメントごとに 1 つずつインデックスして、データを事前にフィルタリングすることもできます。
fields.type
string
必須

$vectorSearchのフィールドをインデックス化するために使用するフィールドタイプ。 次のいずれかの値を指定できます。

  • vector - ベクトル埋め込みを含むフィールドの場合。

  • filter - ブール値、日付、ObjectId、数値、string、または UUID 値を含むフィールドの場合。

fields.path
string
必須

インデックスを作成するフィールドの名前。 ネストされたフィールドの場合は、埋め込みフィールドへのパスを指定するために ドット表記 を使用します。 2 つの連続したドット( . )とドットで終わるフィールド名( . )のインデックスは作成できません。 たとえば、Atlas Vector Search は次のフィールド名のインデックス作成をサポートしていません

foo..bar
フィールド名には 2 つの連続したドットがあります。
foo_bar.
フィールド名はドットで終わる。
fields.numDimensions
整数
必須
Atlas Vector Search がインデックス作成時およびクエリ時に強制されるベクトル次元の数。 4096以下の値を指定する必要があります。 このフィールドはvector型フィールドにのみ設定できます。
fields.similarity
string

必須

上位 K 近傍の検索に使用するベクトル類似度関数。 このフィールドはvector型フィールドにのみ設定できます。 値には次のものが含まれます。

  • euclidean - ベクトルの端点間の距離を測定します。 この値により、さまざまな次元に基づいて類似性を測定できます。 詳しくは、「 Euclidean 」を参照してください。

  • cosine - ベクトル間の角度に基づいて類似性を測定します。 この値により、大きさでスケーリングされない類似性を測定できます。 cosineでは絶対値ゼロのベクトルは使用できません。 コサイン類似度を測定するには、ベクトルを正規化し、代わりにdotProductを使用することをお勧めします。 詳しくは、「 コサイン 」を参照してください。

  • dotProduct - cosineと同様の類似性を測定しますが、ベクトルの大きさを考慮します。 大きさを正規化すると、類似性の測定においてcosinedotProductはほぼ同じになります。

    fields.similaritydotProductの値に設定すると、角度と大きさの両方に基づいて類似性を効率的に測定できます。 dotProductを使用するには、インデックス作成時とクエリ時にベクトルを単位の長さで正規化する必要があります。 詳しくは、「 ドット積 」を参照してください。

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.117.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リクエストを送信します。

1curl --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フィールドを タイプとしてインデックス化します。filterplot_embeddingフィールドには、OpenAI のtext-embedding-ada-002埋め込みモデルを使用して作成された埋め込みが含まれます。 インデックス定義では、 1536ベクトル次元を指定し、 euclidean距離を使用して類似性を測定します。

次のインデックス定義は、ベクトル検索を実行するためのベクトル埋め込みフィールドのみをインデックスします。

1curl --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 )。

1curl --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 インデックスを作成するには、次の手順を実行します。

1

インデックス定義は、次の形式のようになります。

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という名前のファイルを作成します。

2
<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フィールドを タイプとしてインデックス化します。filterplot_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}
3
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 クラスター検索インデックス作成コマンド を参照してください。

1
  1. まだ表示されていない場合は、希望するプロジェクトを含む組織を選択しますナビゲーション バーのOrganizationsメニュー

  2. まだ表示されていない場合は、ナビゲーション バーのProjectsメニューから目的のプロジェクトを選択します。

  3. Clusters ページがまだ表示されていない場合は、サイドバーの Database をクリックします。

    [ Clusters (クラスター) ] ページが表示されます。

2

GoAtlas Searchページには、サイドバー、Data Explorer 、またはクラスターの詳細ページから できます。

  1. サイドバーで、 Services見出しの下のAtlas Searchをクリックします。

  2. [ Select data sourceドロップダウンからクラスターを選択し、[ Go to Atlas Search ] をクリックします。

    Atlas Searchページが表示されます。

  1. クラスターの [Browse Collections] ボタンをクリックします。

  2. データベースを展開し、コレクションを選択します。

  3. コレクションのSearch Indexesタブをクリックします。

    Atlas Searchページが表示されます。

  1. クラスタの名前をクリックします。

  2. [Atlas Search] タブをクリックします。

    Atlas Searchページが表示されます。

3

作成するには、[ Create Search Index ] をクリックします。

4
Atlas Vector Search インデックスの作成画面のスクリーンショット
クリックして拡大します
5
  1. Index Name フィールドにインデックス名を入力します。

    インデックス名は、インデックスのタイプに関係なく、名前空間内で一意である必要があります。

    サンプル インデックスの名前としてvector_indexと入力します。 このコレクションにvector_indexという名前のインデックスがすでに存在する場合は、インデックスに別の名前を入力します。

  2. Database and Collectionセクションで、 データベースを検索し、 コレクションを選択します。

    Tip

    Data Explorerからこのページに移動した場合、Atlas はData Explorerで選択したデータベースとコレクションを事前選択するため、この手順をスキップできます。

    Database and Collectionセクションで、 sample_mflixデータベースを検索し、 embedded_moviesコレクションを選択します。

6

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フィールドを タイプとしてインデックス化します。filterplot_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}
7
8

Atlas は、インデックスが作成中であることを知らせるモーダル ウィンドウを表示します。

9
10

新規作成したインデックスは Atlas Search タブに表示されます。インデックスを作成する間、Status フィールドには Build in Progress と表示されます。インデックスの作成が完了すると、Status フィールドの表示は Active になります。

注意

コレクションが大きいほど、インデックスの作成に時間がかかります。インデックスの作成が完了すると、メール通知が届きます。

mongosh v 2.1.2以降を使用してコレクションに Atlas Vector Search インデックスを作成するには、次の手順を実行します。

1

詳しくは、「 mongosh経由で接続 」を参照してください。

2

use sample_mflix
switched to db sample_mflix
3

db.collection.createSearchIndex()メソッドの構文は次のとおりです。

1db.<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フィールドを タイプとしてインデックス化します。filterplot_embeddingフィールドには、OpenAI のtext-embedding-ada-002埋め込みモデルを使用して作成された埋め込みが含まれます。 インデックス定義では、 1536ベクトル次元を指定し、 euclidean距離を使用して類似性を測定します。

次のインデックス定義は、ベクトル検索を実行するためのベクトル埋め込みフィールドのみをインデックスします。

1db.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 )。

1db.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 ベクトル検索インデックスを作成するには、次の手順を実行します。

1
1package main
2
3import (
4 "context"
5 "fmt"
6 "log"
7
8 "go.mongodb.org/mongo-driver/mongo"
9 "go.mongodb.org/mongo-driver/mongo/options"
10)
11
12func 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という名前のファイルを作成します。

2
<connectionString>
<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 )のみをインデックスします。

1package main
2
3import (
4 "context"
5 "fmt"
6 "log"
7
8 "go.mongodb.org/mongo-driver/mongo"
9 "go.mongodb.org/mongo-driver/mongo/options"
10)
11
12func 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 )。

1package main
2
3import (
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
13func 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}
3
go run <file-name>.go

go run vector-index.go

MongoDB Node ドライバーv 6.6.0以降を使用してコレクション用に Atlas Vector Search インデックスを作成するには、次の手順を実行します。

1
1const { MongoClient } = require("mongodb");
2
3// connect to your Atlas deployment
4const uri = "<connectionString>";
5
6const client = new MongoClient(uri);
7
8async 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}
41run().catch(console.dir);

vector-index.jsという名前のファイルを作成します。

2
<connectionString>
<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 )のみをインデックスします。

1const { MongoClient } = require("mongodb");
2
3// connect to your Atlas deployment
4const uri = "<connectionString>";
5
6const client = new MongoClient(uri);
7
8async 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}
36run().catch(console.dir);

このインデックス定義は、次のフィールドをインデックスします。

  • データを事前にフィルタリングするためのstringフィールド(genres)と数値フィールド(year)。

  • 事前フィルタリングされたデータに対してベクトル検索を実行するためのベクトル埋め込みフィールド( plot_embedding )。

1const { MongoClient } = require("mongodb");
2
3// connect to your Atlas deployment
4const uri = "<connectionString>";
5
6const client = new MongoClient(uri);
7
8async 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}
44run().catch(console.dir);
3
node <file-name>.js

node vector_index.js

PyMongoドライバー v 4.7以降を使用してコレクション用に Atlas Vector Search インデックスを作成するには、次の手順を実行します。

1
1import pymongo
2from pymongo.mongo_client import MongoClient
3from pymongo.operations import SearchIndexModel
4
5# Connect to your Atlas deployment
6uri = "<connectionString>"
7client = MongoClient(uri)
8
9# Access your database and collection
10database = client["<databaseName>"]
11collection = database["<collectionName>"]
12
13# Create your index model, then create the search index
14search_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
34result = collection.create_search_index(model=search_index_model)
35print(result)

詳細については、「 create_search_index() 」を参照してください。 使用して複数のドキュメントを挿入できます。

1from pymongo.mongo_client import MongoClient
2from pymongo.operations import SearchIndexModel
3
4def 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という名前のファイルを作成します。

2
<connectionString>
<databaseName>
インデックスを作成するコレクションを含むデータベース。
<collectionName>
インデックスを作成するコレクション。
<indexName>
インデックスの名前。 インデックス名を省略すると、Atlas Search はインデックスにvector_indexという名前を付けます。
<numberOfDimensions>
Atlas Vector Search がインデックス作成時およびクエリ時に強制されるベクトル次元の数。
<fieldToIndex>
インデックスするベクトル フィールドとフィルター フィールド。

以下をコピーしてvector-index.pyに貼り付け、 <connectionString>プレースホルダー値を置き換えます。 次のインデックス定義は、Atlas Vector Search インデックスで、 フィールドを タイプとしてインデックス化し、 フィールドとplot_embeddingvector genresyearフィールドを タイプとしてインデックス化します。filterplot_embeddingフィールドには、OpenAI のtext-embedding-ada-002埋め込みモデルを使用して作成された埋め込みが含まれます。 インデックス定義では、 1536ベクトル次元を指定し、 euclidean距離を使用して類似性を測定します。

次のインデックス定義は、ベクトル検索を実行するためにベクトル埋め込みフィールド( plot_embedding )のみをインデックスします。

1from pymongo.mongo_client import MongoClient
2from pymongo.operations import SearchIndexModel
3
4# Connect to your Atlas deployment
5uri = "<connectionString>"
6client = MongoClient(uri)
7
8# Access your database and collection
9database = client["sample_mflix"]
10collection = database["embedded_movies"]
11
12# Create your index model, then create the search index
13search_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
28result = collection.create_search_index(model=search_index_model)
29print(result)

このインデックス定義は、次のフィールドをインデックスします。

  • データを事前にフィルタリングするためのstringフィールド(genres)と数値フィールド(year)。

  • 事前フィルタリングされたデータに対してベクトル検索を実行するためのベクトル埋め込みフィールド( plot_embedding )。

1from pymongo.mongo_client import MongoClient
2from pymongo.operations import SearchIndexModel
3
4# Connect to your Atlas deployment
5uri = "<connectionString>"
6client = MongoClient(uri)
7
8# Access your database and collection
9database = client["sample_mflix"]
10collection = database["embedded_movies"]
11
12# Create your index model, then create the search index
13search_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
36result = collection.create_search_index(model=search_index_model)
37print(result)
3
python <file-name>.py

python vector-index.py

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リクエストを送信します。

1curl --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 行)を指定して エンドポイントに リクエストを送信します。

1curl --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 インデックスを返すには、次の手順を実行します。

1
clusterName
Atlas クラスターの名前。
db
インデックス付きコレクションを含む Atlas クラスター上のデータベースの名前。
collection
データベース内のインデックス作成されたコレクションの名前。
projectId
プロジェクトの一意の識別子。
2
atlas clusters search indexes list --clusterName [cluster_name] --db <db-name> --collection <collection-name>

コマンドで、次のプレースホルダー値を置き換えます。

  • cluster-name - インデックス作成されたコレクションを含む Atlas クラスターの名前。

  • db-name - インデックスを検索するコレクションを含むデータベースの名前。

  • collection-name - インデックスを検索するコレクションの名前。

コマンド構文規則とパラメーターについて詳しくは、Atlas CLI ドキュメントのAtlas クラスター検索インデックス リストコマンド を参照してください。

1
  1. まだ表示されていない場合は、希望するプロジェクトを含む組織を選択しますナビゲーション バーのOrganizationsメニュー

  2. まだ表示されていない場合は、ナビゲーション バーのProjectsメニューから目的のプロジェクトを選択します。

  3. Clusters ページがまだ表示されていない場合は、サイドバーの Database をクリックします。

    [ Clusters (クラスター) ] ページが表示されます。

2

GoAtlas Searchページには、サイドバー、Data Explorer 、またはクラスターの詳細ページから できます。

  1. サイドバーで、 Services見出しの下のAtlas Searchをクリックします。

  2. [ Select data sourceドロップダウンからクラスターを選択し、[ Go to Atlas Search ] をクリックします。

    Atlas Searchページが表示されます。

  1. クラスターの [Browse Collections] ボタンをクリックします。

  2. データベースを展開し、コレクションを選択します。

  3. コレクションのSearch Indexesタブをクリックします。

    Atlas Searchページが表示されます。

  1. クラスタの名前をクリックします。

  2. [Atlas Search] タブをクリックします。

    Atlas Searchページが表示されます。

ページには、ページのインデックスに関する次の詳細が表示されます。

名前
インデックスを識別するラベル。
インデックス タイプ

Atlas Search または Atlas Vector Search インデックスを示すラベル。 値には、次のものが含まれます。

  • search Atlas Search 。

  • vectorSearch は、Atlas Vector Search インデックスの場合

インデックス フィールド
このインデックスがインデックスを作成するフィールドを含むリスト。
ステータス
クラスターのプライマリ ノード上のインデックスの現在の状態。 有効な値については、「インデックス ステータス 」を参照してください。
サイズ
プライマリ ノードのインデックスのサイズ。
ドキュメント
コレクション内のドキュメントの総数のうち、インデックスが作成されたドキュメントの数。
アクション

インデックスに対して実行できるアクション。 ここでは、次の作業が可能です。

Search Tester UI では、 vectorSearchタイプのインデックスに対してクエリを実行できません。 Queryボタンをクリックすると、Atlas Vector Search にサンプル$vectorSearchが表示されます。このツールはコピー、変更、実行することができ、 Data Explorerおよびサポートされている他のクライアントを使用してコピー、変更、実行できます。

mongoshを使用してコレクションの Atlas Vector Search インデックスを表示するには、次の手順を実行します。

1

詳しくは、「 mongosh経由で接続 」を参照してください。

2
3

db.collection.getSearchIndexes()メソッドの構文は次のとおりです。

1db.<collectionName>.getSearchIndexes( "<index-name>" );

MongoDB Goドライバーv 1.16.0以降を使用してコレクションの Atlas ベクトル検索インデックスを表示するには、次の手順を実行します。

1
1package main
2
3import (
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
14func 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}
2
<connectionString>
Atlas 接続文字列。詳しくは、「ドライバーによる接続」を参照してください。
<databaseName>
コレクションを含むデータベース。
<collectionName>
インデックスを検索するコレクション。
<indexName>
特定のインデックスを検索する場合は、インデックスの名前。 コレクションのすべてのインデックスを返すには、この値を省略します。
3
go run <file-name>.go

MongoDB Node ドライバーv 6.6.0以降を使用してコレクションの Atlas Vector Search インデックスを表示するには、次の手順を実行します。

1
1const { MongoClient } = require("mongodb");
2
3// connect to your Atlas deployment
4const uri = "<connectionString>";
5
6const client = new MongoClient(uri);
7
8async 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}
20run().catch(console.dir);
2
<connectionString>
Atlas 接続文字列。詳しくは、「ドライバーによる接続」を参照してください。
<databaseName>
コレクションを含むデータベース。
<collectionName>
インデックスを検索するコレクション。
<indexName>
特定のインデックスを検索する場合は、インデックスの名前。 コレクションのすべてのインデックスを返すには、この値を省略します。
3
node <file-name>.js

PyMongoドライバー v 4.7以降を使用してコレクションの Atlas Vector Search インデックスを表示するには、次の手順を実行します。

1
1from pymongo.mongo_client import MongoClient
2
3# Connect to your Atlas deployment
4uri = "<connectionString>"
5client = MongoClient(uri)
6
7# Access your database and collection
8database = client["<databaseName>"]
9collection = database["<collectionName>"]
10
11# Get a list of the collection's search indexes and print them
12cursor = collection.list_search_indexes()
13for index in cursor:
14 print(index)

詳しくは、 list_search_indexs() を参照してください 使用して複数のドキュメントを挿入できます。

2
<connectionString>
Atlas 接続文字列。詳しくは、「ドライバーによる接続」を参照してください。
<databaseName>
コレクションを含むデータベースの名前。
<collectionName>
コレクションの名前。
3
python <file-name>.py

既存の Atlas Vector Search インデックスのインデックス定義は、Atlas UI、Atlas Administration API、Atlas CLI、 mongosh 、またはサポートされているMongoDB ドライバーから変更できます。 インデックスの名前を変更したり、インデックス タイプを変更したりすることはできません。 インデックスの名前またはタイプを変更する必要がある場合は、新しいインデックスを作成し、古いインデックスを削除する必要があります。

重要

インデックスを編集すると、Atlas Vector Search はそのインデックスを再構築します。 インデックスの再ビルド中も、古いインデックス定義を使用してベクトル検索クエリを引き続き実行できます。 インデックスの再構築が完了すると、古いインデックスは自動的に置き換えられます。 このプロセスは、標準の Atlas 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リクエストを送信します。

1curl --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 インデックスを編集するには、次の手順を実行します。

1

インデックス定義は、次の形式のようになります。

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}
2
<name-of-database>
インデックスを作成するコレクションを含むデータベース。
<name-of-collection>
インデックスを作成するコレクション。
<index-name>
インデックスの名前。 インデックス名を省略すると、Atlas Vector Search はインデックスにvector_indexという名前を付けます。
<number-of-dimensions>
Atlas Vector Search がインデックス作成時およびクエリ時に強制されるベクトル次元の数。
<field-to-index>
インデックスするベクトル フィールドとフィルター フィールド。
3
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 クラスター検索インデックス アップデートコマンド を参照してください。

1
  1. まだ表示されていない場合は、希望するプロジェクトを含む組織を選択しますナビゲーション バーのOrganizationsメニュー

  2. まだ表示されていない場合は、ナビゲーション バーのProjectsメニューから目的のプロジェクトを選択します。

  3. Clusters ページがまだ表示されていない場合は、サイドバーの Database をクリックします。

    [ Clusters (クラスター) ] ページが表示されます。

2

GoAtlas Searchページには、サイドバー、Data Explorer 、またはクラスターの詳細ページから できます。

  1. サイドバーで、 Services見出しの下のAtlas Searchをクリックします。

  2. [ Select data sourceドロップダウンからクラスターを選択し、[ Go to Atlas Search ] をクリックします。

    Atlas Searchページが表示されます。

  1. クラスターの [Browse Collections] ボタンをクリックします。

  2. データベースを展開し、コレクションを選択します。

  3. コレクションのSearch Indexesタブをクリックします。

    Atlas Searchページが表示されます。

  1. クラスタの名前をクリックします。

  2. [Atlas Search] タブをクリックします。

    Atlas Searchページが表示されます。

3
  1. 編集するvectorSearch型インデックスを見つけます。

  2. そのインデックスのActionsドロップダウンから [ Edit Indexをクリックします。

  3. 現在の構成設定を確認し、必要に応じて編集します。

    Atlas Vector Search インデックスのフィールドの詳細については、 「ベクトル検索のフィールドにインデックスを作成する方法」 を参照してください。

  4. 変更を適用するには、 Saveをクリックします。

インデックスのステータスがActiveからBuildingに変わります。 この状態では、アップデートされたインデックスが使用可能になるまで、Atlas Vector Search が古いインデックスを削除しないため、古いインデックスを使用し続けることができます。 ステータスがActiveに戻ったら、変更されたインデックスは使用可能になります。

mongoshを使用してコレクションの Atlas Vector Search インデックスを編集するには、次の手順を実行します。

1

詳しくは、「 mongosh経由で接続 」を参照してください。

2
3

db.collection.updateSearchIndex()メソッドの構文は次のとおりです。

1db.<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 ベクトル検索インデックスを更新するには、次の手順を実行します。

1
1package main
2
3import (
4 "context"
5 "fmt"
6 "log"
7
8 "go.mongodb.org/mongo-driver/mongo"
9 "go.mongodb.org/mongo-driver/mongo/options"
10)
11
12func 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}
2
<connectionString>
<databaseName>
インデックスを作成するコレクションを含むデータベース。
<collectionName>
インデックスを作成するコレクション。
<indexName>
インデックスの名前。 インデックス名を省略すると、Atlas Search はインデックスにvector_indexという名前を付けます。
<numberOfDimensions>
Atlas Vector Search がインデックス作成時およびクエリ時に強制されるベクトル次元の数。
<fieldToIndex>
インデックスするベクトル フィールドとフィルター フィールド。
3
go run <file-name>.go

MongoDB Node ドライバーv 6.6.0以降を使用してコレクションの Atlas Vector Search インデックスを更新するには、次の手順を実行します。

1
1const { MongoClient } = require("mongodb");
2
3// connect to your Atlas deployment
4const uri = "<connection-string>";
5
6const client = new MongoClient(uri);
7
8async 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}
41run().catch(console.dir);
2
<connectionString>
<databaseName>
インデックスを作成するコレクションを含むデータベース。
<collectionName>
インデックスを作成するコレクション。
<indexName>
インデックスの名前。 インデックス名を省略すると、Atlas Search はインデックスにvector_indexという名前を付けます。
<numberOfDimensions>
Atlas Vector Search がインデックス作成時およびクエリ時に強制されるベクトル次元の数。
<fieldToIndex>
インデックスするベクトル フィールドとフィルター フィールド。
3
node <file-name>.js

PyMongoドライバー v 4.7以降を使用してコレクションの Atlas Vector Search インデックスを更新するには、次の手順を実行します。

1
1from pymongo.mongo_client import MongoClient
2
3# Connect to your Atlas deployment
4uri = "<connectionString>"
5client = MongoClient(uri)
6
7# Access your database and collection
8database = client["<databaseName>"]
9collection = database["<collectionName>"]
10
11definition = {
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
28collection.update_search_index("<indexName>", definition)

詳しくは、 update_search_index() 使用して複数のドキュメントを挿入できます。

2
<connectionString>
<databaseName>
インデックスを作成するコレクションを含むデータベース。
<collectionName>
インデックスを作成するコレクション。
<indexName>
インデックスの構成。 インデックス名を省略すると、Atlas Search はインデックスにvector_indexという名前を付けます。
<numberOfDimensions>
Atlas Vector Search がインデックス作成時およびクエリ時に強制されるベクトル次元の数。
<fieldToIndex>
インデックスするベクトル フィールドとフィルター フィールド。
3
python <file-name>.py

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リクエストを送信します。

1curl --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 インデックスを削除するには、次の手順を実行します。

1
<indexId>
削除するインデックスの一意の識別子。
<clusterName>
Atlas クラスターの名前。
<projectId>
プロジェクトの一意の識別子。
2
atlas clusters search indexes delete <indexId> [options]

コマンドで、 indexIdプレースホルダー値を削除するインデックスの一意の識別子に置き換えます。

コマンド構文規則とパラメーターについて詳しくは、Atlas CLI ドキュメントのAtlas クラスター検索インデックスの削除コマンドを参照してください。

1
  1. まだ表示されていない場合は、希望するプロジェクトを含む組織を選択しますナビゲーション バーのOrganizationsメニュー

  2. まだ表示されていない場合は、ナビゲーション バーのProjectsメニューから目的のプロジェクトを選択します。

  3. Clusters ページがまだ表示されていない場合は、サイドバーの Database をクリックします。

    [ Clusters (クラスター) ] ページが表示されます。

2

GoAtlas Searchページには、サイドバー、Data Explorer 、またはクラスターの詳細ページから できます。

  1. サイドバーで、 Services見出しの下のAtlas Searchをクリックします。

  2. [ Select data sourceドロップダウンからクラスターを選択し、[ Go to Atlas Search ] をクリックします。

    Atlas Searchページが表示されます。

  1. クラスターの [Browse Collections] ボタンをクリックします。

  2. データベースを展開し、コレクションを選択します。

  3. コレクションのSearch Indexesタブをクリックします。

    Atlas Searchページが表示されます。

  1. クラスタの名前をクリックします。

  2. [Atlas Search] タブをクリックします。

    Atlas Searchページが表示されます。

3
  1. 削除するvectorSearchタイプ インデックスを見つけます。

  2. そのインデックスのActionsドロップダウンから [ Delete Indexをクリックします。

  3. 確認ウィンドウで [ Drop Indexをクリックします。

mongoshを使用してコレクションの Atlas Vector Search インデックスを削除するには、次の手順を実行します。

1

詳しくは、「 mongosh経由で接続 」を参照してください。

2
3

db.collection.dropSearchIndex()メソッドの構文は次のとおりです。

1db.<collectionName>.dropSearchIndex( "<index-name>" );

MongoDB Goドライバーv 1.16.0以降を使用してコレクションの Atlas ベクトル検索インデックスを削除するには、次の手順を実行します。

1
1package main
2
3import (
4 "context"
5 "fmt"
6 "log"
7
8 "go.mongodb.org/mongo-driver/mongo"
9 "go.mongodb.org/mongo-driver/mongo/options"
10)
11
12func 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}
2
<connectionString>
Atlas 接続文字列。詳しくは、「ドライバーによる接続」を参照してください。
<databaseName>
インデックスを作成するコレクションを含むデータベース。
<collectionName>
インデックスを作成するコレクション。
<indexName>
インデックスの名前。インデックス名を省略すると、Atlas Search はインデックスに vector_index という名前を付けます。
3
go run <file-name>.go

MongoDB Node ドライバーv 6.6.0以降を使用してコレクションの Atlas Vector Search インデックスを削除するには、次の手順を実行します。

1
1const { MongoClient } = require("mongodb");
2
3// connect to your Atlas deployment
4const uri = "<connectionString>";
5
6const client = new MongoClient(uri);
7
8async 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}
20run().catch(console.dir);
2
<connectionString>
Atlas 接続文字列。詳しくは、「ドライバーによる接続」を参照してください。
<databaseName>
インデックスを作成するコレクションを含むデータベース。
<collectionName>
インデックスを作成するコレクション。
<indexName>
インデックスの名前。インデックス名を省略すると、Atlas Search はインデックスに vector_index という名前を付けます。
3
node <file-name>.js

PyMongoドライバー v 4.7以降を使用してコレクションの Atlas Vector Search インデックスを削除するには、次の手順を実行します。

1
1from pymongo.mongo_client import MongoClient
2
3# Connect to your Atlas deployment
4uri = "<connectionString>"
5client = MongoClient(uri)
6
7# Access your database and collection
8database = client["<databaseName>"]
9collection = database["<collectionName>"]
10
11# Delete your search index
12collection.drop_search_index("<indexName>")

詳細については、 drop_search_index() 使用して複数のドキュメントを挿入できます。

2
<connectionString>
Atlas 接続文字列。詳しくは、「ドライバーによる接続」を参照してください。
<databaseName>
コレクションを含むデータベースの名前。
<collectionName>
コレクションの名前。
<indexName>
削除するインデックスの名前。
3
python <file-name>.py

Atlas Vector Search インデックスを作成すると、 Status列にはクラスターのプライマリ ノードにおけるインデックスの現在の状態が表示されます。 クラスタ内のすべてのノードにおけるインデックスの状態を表示するには、ステータスの下にあるView status detailsリンクをクリックします。

Status 列が Active の場合、インデックスは使用可能です。そのほかの状態の場合は、インデックスに対するクエリに不完全な結果が返される場合があります。

ステータス
説明
開始前
Atlas によるインデックスの構築は開始されていません。
最初の同期

Atlas はインデックスを構築中、または編集したインデックスを再構築中です。インデックスがこの状態のとき、次の状況が発生します。

  • 新しいインデックスの場合、Atlas Vector Search はインデックスのビルドが完了するまでクエリを処理しません。

  • 既存のインデックスの場合、インデックスの再構築が完了するまで、ユーザーは古いインデックスを引き続き使用して既存のクエリと新しいクエリを処理できます。

アクティブ
インデックスをいつでも使用できます。
リカバリ中
レプリケーションでエラーが発生しました。この状態は通常、現在のレプリケーション ポイントが mongod oplog で使用できなくなった場合に発生します。既存のインデックスがアップデートされ、ステータスが Active に変わるまで、引き続き既存のインデックスをクエリできます。View status detailsモーダル ウィンドウのエラーを使用して、問題のトラブルシューティングを行います。詳しくは、「Atlas Search の問題の修正」を参照してください。
失敗
Atlas によるインデックスの構築は失敗しました。View status details モーダル ウィンドウのエラーを使用して、問題のトラブルシューティングを行います。詳しくは、「Atlas Search の問題の修正」を参照してください。
削除中
Atlas はクラスター ノードからインデックスを削除しています。

Atlas によるインデックスの構築中および構築の完了後、Documents 列にはインデックス済みドキュメントの割合と数が表示されます。また、この列には、コレクション内の総ドキュメント数も表示されます。

次のセクションでは、 から Atlas Vector Search インデックスを作成および管理する方法について説明します。

戻る

ベクトル埋め込みの作成方法