Overview
このガイドでは、Rubyドライバーを使用してMongoDB 検索インデックスおよびMongoDB ベクトル検索インデックスをプログラムで管理する方法を学ぶことができます。
MongoDB Search 機能を使用すると、MongoDB Atlasでホストされているコレクションに対して全文検索を実行できます。MongoDB 検索する の詳細については、「MongoDB 検索する 概要。」を参照してください。
MongoDB ベクトル検索を使用すると、MongoDB Atlasに保存されているベクトル埋め込みに対してセマンティック検索を実行できます。MongoDB ベクトル検索の詳細については、MongoDB ベクトル検索の概要を参照してください。
MongoDB 検索およびMongoDB ベクトル検索の検索インデックスを管理するには、次のメソッドを呼び出します。
search_indexes#create_onesearch_indexes#create_manysearch_indexes#update_onesearch_indexes#drop_one
注意
MongoDB 検索する と MongoDB ベクトル検索 のインデックスマネジメントは非同期です
RubyドライバーはMongoDB検索インデックスとMongoDBベクトル検索インデックスを非同期に管理します。次のセクションで説明されているメソッドはサーバー応答をすぐに返しますが、検索インデックスへの変更はバックグラウンドで行われ、しばらくが完了しないと完了しない可能性があります。
次のセクションでは、前述の各コマンドの使用方法を示すコード例を示します。
検索インデックスを作成
単一のMongoDB Search またはMongoDB ベクトル検索インデックスを作成するには、search_indexes#create_one メソッドを使用します。複数のインデックスを作成するには、search_indexes#create_many メソッドを使用します。どちらのメソッドもすぐに返されますが、インデックスはバックグラウンドで非同期に作成されます。
次のコード例は、インデックス定義とインデックスの任意の名前を指定してMongoDB検索インデックスを作成する方法を示しています。
# 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検索インデックスを作成します。MongoDB ベクトル検索インデックスを作成するには、create_one を呼び出すときに type パラメータを 'vectorSearch' に設定する必要があります。
search_indexes#create_many を使用して、インデックス仕様の配列を提供することで、複数の MongoDB 検索インデックス または MongoDB ベクトル検索インデックスを作成できます。各インデックス仕様には、次のコンポーネントを含める必要があります。
definitionパラメータ: インデックスを定義nameパラメータ: インデックス名を指定typeパラメーター:インデックスの種類('search'または'vectorSearch')を指定します
次のコード例は、1 回の呼び出しで MongoDB 検索インデックス と 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 検索インデックス 構文の検討」または「MongoDB ベクトル検索のフィールドにインデックスを作成する方法」ガイドを参照してください。
検索インデックスをアップデートする
MongoDB Search または MongoDB ベクトル検索インデックスを更新するには、search_indexes#update_one メソッドを使用します。
インデックスを更新するには、新しいインデックス定義を指定する必要があります。インデックスの name または id を使用して、更新するインデックスを指定する必要があります。次のコードは、MongoDB 検索インデックスを更新する方法を示しています。
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 検索する または 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 検索するインデックスおよび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 ドキュメントを参照してください。