Overview
In this guide, you can learn how to run a text query in the MongoDB Kotlin driver.
テキストクエリを使用して、指定されたフィールドにタームまたはフレーズを含むドキュメントを取得できます。タームは、空白文字を除外する文字のシーケンスです。フレーズは、任意の数の空白文字を含むタームのシーケンスです。
次のセクションでは、次のタイプのテキストクエリを実行する方法を説明します。
タームによるクエリ テキスト
フレーズで検索テキストを取得
タームが除外されたクエリ テキスト
テキスト クエリの結果を並べ替える場合は、 結果の並べ替えガイドの テキスト クエリのセクションを参照してください。
サンプル ドキュメント
次のセクションでは、fast_and_furious_moviesコレクションのテキスト クエリの例を紹介します。各セクションでは、collection という名前の変数を使用して、fast_and_furious_moviesコレクションの MongoCollectionインスタンスを参照します。
fast_and_furious_moviesコレクションには、映画「F形式」の構成要素であるいくつかの映画の 1 つを説明するドキュメントが含まれています。 各ドキュメントには、タイトル フィールドとタグ フィールドが含まれています。
{ "_id": 1, "title": "2 Fast 2 Furious ", "tags": ["undercover", "drug dealer"] } { "_id": 2, "title": "Fast 5", "tags": ["bank robbery", "full team"] } { "_id": 3, "title": "Furious 7", "tags": ["emotional"] } { "_id": 4, "title": "The Fate of the Furious", "tags": ["betrayal"] }
このデータは、次の Kotlin データ クラスでモデル化されます。
data class Movies( val id: Int, val title: String, val tags: List<String> )
Text Index
You must create a text index before running a text query. A text index specifies the string or string array field on which to run a text query.
In the following examples, you run text queries on the title field in the fast_and_furious_movies collection. To enable text queries on the title field, create a text index using the Indexes builder with the following snippet:
collection.createIndex(Indexes.text("title"))
詳細については、次のリソースを参照してください。
インデックス ガイドの「テキスト インデックス」セクション
テキストインデックスのサーバー マニュアル エントリ
テキストクエリ
テキストクエリを指定するには、Filters.text() メソッドを使用します。
The Filters.text() method uses the Filters builder to define a query filter specifying what to search for during the text query. The query filter is represented by a BSON instance. Pass the query filter to the find() method to run a text query.
When you execute the find() method, MongoDB runs a text query on all the fields indexed with the text index on the collection. MongoDB returns documents that contain one or more of the query terms and a relevance score for each result. For more information on relevance scores, see the Text Query section in our Sort Results guide.
オプションの指定
You can include TextSearchOptions as the second parameter of the Filters.text() method to specify text query options such as case sensitivity. By default, text queries run without case sensitivity which means the query matches lowercase and uppercase values.
To specify a case sensitive query, use the following snippet:
val options: TextSearchOptions = TextSearchOptions().caseSensitive(true) val filter = Filters.text("SomeText", options)
このセクションで説明されるメソッドとクラスの詳細については、次の API ドキュメントを参照してください。
タームによるクエリ テキスト
タームを string として Filters.text() メソッドに渡し、テキスト クエリでタームを指定します。
例
次の例では、「fast」タームを含むタイトルの fast_and_furious_moviesコレクション内のドキュメントに対してテキスト クエリを実行します。
val filter = Filters.text("fast") val findFlow = collection.find(filter) findFlow.collect { println(it) }
Movies(id=1, title=2 Fast 2 Furious, tags=[undercover, drug dealer]) Movies(id=2, title=Fast 5, tags=[bank robbery, full team])
テキスト クエリで複数のタームを検索するには、Filters.text() ビルダー メソッドで各タームをスペースで区切ります。ビルダ メソッドはテキストクエリを Bsonインスタンスとして返します。これを find() メソッドに渡すと、タームのいずれかに一致するドキュメントが返されます。
例
次の例では、"fate" または "7" というタームを含むタイトルの fast_and_furious_moviesコレクション内のドキュメントに対してテキスト クエリを実行します。
val filter = Filters.text("fate 7") val findFlow = collection.find(filter) findFlow.collect { println(it) }
Movies(id=3, title=Furious 7, tags=[emotional]) Movies(id=4, title=The Fate of the Furious, tags=[betrayal])
フレーズで検索テキストを取得
エスケープされた引用符を含むフレーズを Filters.text()メソッドに渡して、テキスト クエリでフレーズを指定します。エスケープされた引用符は、バックスラッシュ文字が前に付いた二重引用符文字です。 フレーズの前後にエスケープされた引用符を付けない場合、find() メソッドはターム検索を実行します。
例
次の例では、「フェイルオーバー」というフレーズを含むタイトルの fast_and_furious_moviesコレクション内のドキュメントに対してテキスト クエリを実行します。
val filter = Filters.text("\"fate of the furious\"") val findFlow = collection.find(filter) findFlow.collect { println(it) }
Movies(id=4, title=The Fate of the Furious, tags=[betrayal])
タームが除外されたクエリ テキスト
テキスト クエリから除外する各タームについて、Filters.text() ビルダー メソッドに渡す string のタームの前にマイナス記号を付けます。
None of the documents returned from the query contain the excluded term in your text index field.
重要
You must have at least one text query term if you want to exclude terms from your query.
例
次の例では、「fourial」タームが含まれるタイトルの fast_and_furious_moviesコレクション内のドキュメントに対してテキスト クエリを実行しますが、「fast」タームは 含まれていません 。
val filter = Filters.text("furious -fast") val findFlow = collection.find(filter) findFlow.collect { println(it) }
Movies(id=3, title=Furious 7, tags=[emotional]) Movies(id=4, title=The Fate of the Furious, tags=[betrayal])