Overview
このガイドでは、 Rubyドライバーを使用してMongoDB Search およびMongoDB ベクトル検索インデックスをプログラムで管理する方法を学習できます。
MongoDB Search 機能を使用すると、 MongoDB Atlasでホストされているコレクションに対して全文検索を実行できます。 MongoDB Search の詳細については、「 MongoDB Search の概要 」を参照してください。
MongoDB ベクトル検索 を使用すると、 MongoDB Atlasに保存されているベクトル埋め込みに対してセマンティック検索を実行できます。 MongoDB ベクトル検索の詳細については、「 MongoDB ベクトル検索 の概要 」を参照してください。
MongoDB Search およびMongoDB ベクトル検索インデックスを管理するには、次のメソッドを呼び出します。
search_indexes#create_one
search_indexes#create_many
search_indexes#update_one
search_indexes#drop_one
注意
MongoDB Search とMongoDB ベクトル検索 のインデックス管理は非同期
RubyドライバーはMongoDB Search インデックスとMongoDB ベクトル検索インデックスを非同期に管理します。次のセクションで説明されているメソッドはサーバー応答をすぐに返しますが、検索インデックスへの変更はバックグラウンドで行われ、しばらくが完了しないと完了しない可能性があります。
次のセクションでは、前述の各コマンドの使用方法を示すコード例を示します。
検索インデックスを作成
単一のMongoDB Search またはMongoDB ベクトル検索インデックスを作成するには、search_indexes#create_one
メソッドを使用します。複数のインデックスを作成するには、search_indexes#create_many
メソッドを使用します。どちらのメソッドもすぐに返されますが、インデックスはバックグラウンドで非同期に作成されます。
次のコード例は、インデックス定義とインデックスの任意の名前を指定してMongoDB Searchインデックスを作成する方法を示しています。
# Creates indexes on all dynamically indexable fields with a default index name collection.search_indexes.create_one( { mappings: { dynamic: true } } ) # Creates an index on the specified field with the specified index name index_definition = { mappings: { dynamic: false, fields: { fullplot: { type: 'string' } } } } collection.search_indexes.create_one(index_definition, name: 'mySearchIndex')
注意
デフォルトでは 、type
パラメータを渡さない場合、ドライバーはMongoDB Searchインデックスを作成します。 MongoDB ベクトル検索インデックスを作成するには、create_one
を呼び出すときに type
パラメータを 'vectorSearch'
に設定する必要があります。
search_indexes#create_many
を使用して、インデックス仕様の配列を提供することで、複数のMongoDB Search またはMongoDB ベクトル検索インデックスを作成できます。各インデックス仕様には、次のコンポーネントを含める必要があります。
definition
パラメータ: インデックスを定義name
パラメータ: インデックス名を指定type
パラメーター:インデックスの種類('search'
または'vectorSearch'
)を指定します
次のコード例は、1 回の呼び出しでMongoDB Search とMongoDB ベクトル検索インデックスの両方を作成する方法を示しています。
index_spec_1 = { name: 'searchIndex_plot', type: 'search', definition: { mappings: { dynamic: false, fields: { plot: { type: 'string' } } } } } index_spec_2 = { name: 'vsIndex_plot_embedding', type: 'vectorSearch', definition: { fields: [ { type: "vector", path: "plot_embedding", numDimensions: 1536, similarity: "dotProduct" } ] } } collection.search_indexes.create_many([index_spec_1, index_spec_2])
より長いインデックス定義の場合、 メソッド呼び出しの外部でインデックス定義を定義するのが便利です。インデックス定義の構文の詳細については、Atlas マニュアルの「MongoDB Search インデックスシンタックスの確認」 または「MongoDB ベクトル検索 のフィールドにインデックスを作成する方法」 ガイドを参照してください。
検索インデックスをアップデートする
MongoDB Search またはMongoDB ベクトル検索インデックスを更新するには、search_indexes#update_one
メソッドを使用します。
インデックスを更新するには、新しいインデックス定義を指定する必要があります。インデックスの name
または id
を使用して、更新するインデックスを指定する必要があります。次のコードは、 MongoDB Searchインデックスを更新する方法を示しています。
updated_definition = { mappings: { dynamic: false, fields: { fullplot: { type: 'string' } } } } # Specifies the index to update by using the index name collection.search_indexes.update_one(updated_definition, name: 'searchIndex_plot') # Specifies the index to update by using the index id collection.search_indexes.update_one(updated_definition, id: <index id>)
検索インデックスを削除する
MongoDB Search またはMongoDB ベクトル検索インデックスを削除するには、search_indexes#drop_one
メソッドを使用します。
インデックスを削除するには、インデックスの id
または name
を指定する必要があります。次のコードは、コレクションから検索インデックスを削除する方法を示しています。
# Specifies the index to delete by using the index name collection.search_indexes.drop_one(name: 'searchIndex_plot') # Specifies the index to delete by using the index id collection.search_indexes.drop_one(id: <index id>)
検索インデックスをリストする
search_indexes
オブジェクトを使用して、コレクションの各MongoDB Search およびMongoDB ベクトル検索インデックスのインデックス仕様全体を一覧表示できます。
puts collection.search_indexes.collect(&:to_json)
各インデックスのインデックス仕様における個々のフィールドを一覧表示するには、search_indexes
オブジェクトを反復処理します。
collection.search_indexes.each do |index_spec| p index_spec['id'] p index_spec['name'] p index_spec['status'] p index_spec['queryable'] p index_spec['latestDefinition'] end
詳細情報
MongoDB Search の詳細については、 MongoDB Search のドキュメント を参照してください。
MongoDB ベクトル検索の詳細については、 Rubyドライバーの 「 MongoDB ベクトル検索クエリの実行 」ガイドまたはMongoDB ベクトル検索 のドキュメントを参照してください。
API ドキュメント
このガイドで説明されているメソッドの詳細については、次の API ドキュメントを参照してください。