Overview
このガイドでは、JavaドライバーでMongoDB ベクトル検索機能を使用する方法を学ぶことができます。Aggregatesビルダクラスは、$vectorSearchパイプラインステージを作成するために使用できる vectorSearch()ヘルパーメソッドを提供します。このパイプラインステージでは、ドキュメントに対してセマンティック検索を実行できます。セマンティック検索とは、指定した検索期間やフレーズと意味が似ているが、必ずしも同一ではない情報を検索するタイプの検索です。
ベクトル検索の実行
この機能を使用するには、ベクトル検索インデックスを作成し、ベクトル埋め込みにインデックスを付ける必要があります。ベクトル検索インデックスをプログラムで作成する方法については、インデックスガイドの「MongoDB Search および MongoDB Vector Search インデックス」セクションを参照してください。ベクトル埋め込みの詳細については、Atlas ドキュメントの「ベクトル検索のためのベクトル埋め込みのインデックス化方法」をご覧ください。
ベクトル埋め込みにベクトル検索インデックスを作成したら、次のセクションに示すように、パイプラインステージでこのインデックスを参照できます。
ベクトル検索の例
次の例は、 メソッドと メソッドを使用してベクトル検索スコアを計算する集計パイプラインを構築する方法を示しています。vectorSearch()project()
// Create an instance of the BinaryVector class as the query vector BinaryVector queryVector = BinaryVector.floatVector( new float[]{0.0001f, 1.12345f, 2.23456f, 3.34567f, 4.45678f}); // Specify the index name for the vector embedding index String indexName = "mflix_movies_embedding_index"; // Specify the path of the field to search on FieldSearchPath fieldSearchPath = fieldPath("plot_embedding"); // Limit the number of matches to 1 int limit = 1; // Create a pre-filter to only search within a subset of documents VectorSearchOptions options = exactVectorSearchOptions() .filter(gte("year", 2016)); // Create the vectorSearch pipeline stage List<Bson> pipeline = asList( vectorSearch( fieldSearchPath, queryVector, indexName, limit, options), project( metaVectorSearchScore("vectorSearchScore")));
Tip
クエリ ベクトル型
上記の例では、クエリベクトルとして機能するために BinaryVector のインスタンスを作成していますが、Double インスタンスの List を作成することもできます。 ただし、ストレージ効率を向上させるために、 BinaryVector タイプを使用することをお勧めします。
次の例は、集計を実行し、前述の集計パイプラインの結果からベクトル検索メタスコアを出力する方法を示しています。
Document found = collection.aggregate(pipeline).first(); double score = found.getDouble("vectorSearchScore").doubleValue(); System.out.println("vectorSearch score: " + score);
Tip
Javaドライバーベクトル検索の例
Javaドライバーを使用して Atlas Vector Search を実行する方法に関する詳細なチュートリアルについては、 Atlas のドキュメント を参照してください。
自動埋め込みインデックスをクエリする
自動埋め込みMongoDB ベクトル検索インデックスをクエリすることで、テキスト検索のベクトル生成を自動化できます。自動埋め込みインデックス の作成方法については、 「 MongoDB自動埋め込み検索インデックス モデル 」を参照してください。
次の例では、plotフィールド内のフレーズ time travel へのセマンティック類似性を検索するMongoDB ベクトル検索クエリを作成します。クエリは、auto_embedding_index という名前の plotフィールドに自動埋め込みMongoDB ベクトル検索インデックスを使用します。
List<Bson> pipeline = asList( vectorSearch( fieldPath("plot"), textQuery("time travel"), "auto_embedding_index", 10L, approximateVectorSearchOptions(150L) ), project( fields(include("title", "plot"), excludeId()) ) ); List<Document> results = collection.aggregate(pipeline).into(new ArrayList<>()); for (Document doc : results) { System.out.println("Title: " + doc.getString("title")); System.out.println("Plot: " + doc.getString("plot")); System.out.println("---"); }
Title: Manuel on the Island of Wonders Plot: Manuel's fantasy travel through Time goes from Long Ago (Episode 1 - O jardim proibido / Le Jardin interdit) through Now (Episode 2 - O pique-nique dos sonhos / Le Pique-nique des rèves), ... --- Title: 11 Minutes Ago Plot: Traveling in 11-minute increments, a time-tumbler from 48-years in the future spends two years of his life weaving through a two-hour wedding reception. --- Title: Time Freak Plot: A neurotic inventor creates a time machine and gets lost traveling around yesterday. --- Title: Timecrimes Plot: A man accidentally gets into a time machine and travels back in time nearly an hour. Finding himself will be the first of a series of disasters of unforeseeable consequences. --- Title: The Little Girl Who Conquered Time Plot: A high-school girl acquires the ability to time travel. --- Title: Time Traveller Plot: A high-school girl acquires the ability to time travel. --- Title: Je t'aime je t'aime Plot: Recovering from an attempted suicide, a man is selected to participate in a time travel experiment that has only been tested on mice. A malfunction in the experiment causes the man to ... --- Title: A.P.E.X. Plot: A time-travel experiment in which a robot probe is sent from the year 2073 to the year 1973 goes terribly wrong thrusting one of the project scientists, a man named Nicholas Sinclair into a... --- Title: The Ah of Life Plot: Theoretical mathematician, Nigel Kline finds himself the subject of his own vertical time study. Entering into Einstein's relativity, three versions of Nigel face off with each other, weaving time and space in a world of fluid moments. --- Title: About Time Plot: At the age of 21, Tim discovers he can travel in time and change what happens and has happened in his own life. His decision to make his world a better place by getting a girlfriend turns out not to be as easy as you might think. ---
注意
自動埋め込みインデックスを使用する場合は、そのテキストのベクトル表現ではなく、検索するテキストを直接指定します。
MongoDB ベクトル検索インデックスの自動埋め込みの詳細については、「 インデックスガイド 」の「 MongoDB自動埋め込み検索インデックス モデル 」セクションを参照してください。
API ドキュメント
このガイドで言及されているメソッドとタイプの詳細については、次のAPIドキュメントを参照してください。