@@ -16,7 +16,7 @@ Overview
16
16
--------
17
17
18
18
In this guide, you can learn how to run a **text search** in the MongoDB
19
- Java driver.
19
+ Kotlin driver.
20
20
21
21
You can use a text search to retrieve documents that contain a **term**
22
22
or a **phrase** in a specified field. A term is a sequence of characters
@@ -52,6 +52,11 @@ movie franchise. Each document contains a title field and a tags field.
52
52
{ "_id": 3, "title": "Furious 7", "tags": ["emotional"] }
53
53
{ "_id": 4, "title": "The Fate of the Furious", "tags": ["betrayal"] }
54
54
55
+ This data is modeled with the following Kotlin data class:
56
+
57
+ .. literalinclude:: /examples/generated/SearchTextTest.snippet.search-data-model.kt
58
+ :language: kotlin
59
+
55
60
Text Index
56
61
~~~~~~~~~~
57
62
@@ -65,11 +70,8 @@ searches on the ``title`` field, create a text index using the
65
70
:ref:`Indexes <index-text-indexes>` builder with the following
66
71
snippet:
67
72
68
- .. literalinclude:: /includes/fundamentals/code-snippets/SearchText.java
69
- :language: java
70
- :dedent:
71
- :start-after: begin textIndex
72
- :end-before: end textIndex
73
+ .. literalinclude:: /examples/generated/SearchTextTest.snippet.text-index.kt
74
+ :language: kotlin
73
75
74
76
For more information, see the following resources:
75
77
@@ -105,16 +107,14 @@ which means the search matches lowercase and uppercase values.
105
107
106
108
To specify a case sensitive search, use the following snippet:
107
109
108
- .. code-block:: java
109
-
110
- TextSearchOptions options = new TextSearchOptions().caseSensitive(true);
111
- Bson filter = Filters.text("SomeText", options);
110
+ .. literalinclude:: /examples/generated/SearchTextTest.snippet.specify-options.kt
111
+ :language: kotlin
112
112
113
113
For more information about the methods and classes mentioned in this section,
114
114
see the following API Documentation:
115
115
116
- - `Filters.text() <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/Filters.html#text(java.lang.String )>`__
117
- - `TextSearchOptions <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/TextSearchOptions.html >`__
116
+ - `Filters.text() <TODO:(DOCSP-29169 )>`__
117
+ - `TextSearchOptions <TODO:(DOCSP-29169) >`__
118
118
119
119
.. _term_search:
120
120
@@ -131,19 +131,16 @@ The following example runs a text search on the documents in the
131
131
``fast_and_furious_movies`` collection for titles that contain the
132
132
term "fast":
133
133
134
- .. literalinclude:: /includes/fundamentals/code-snippets/SearchText.java
135
- :language: java
136
- :dedent:
137
- :start-after: begin termExample
138
- :end-before: end termExample
134
+ .. io-code-block::
139
135
140
- The following shows the output of the preceding code:
136
+ .. input:: /examples/generated/SearchTextTest.snippet.search-term.kt
137
+ :language: kotlin
141
138
142
- .. code-block :: json
143
- :copyable: false
139
+ .. output ::
140
+ :language: console
144
141
145
- { "_id": 1, " title": " 2 Fast 2 Furious ", " tags": [" undercover", " drug dealer"] }
146
- { "_id": 2, " title": " Fast 5", " tags": [" bank robbery", " full team"] }
142
+ Movies(id= 1, title= 2 Fast 2 Furious, tags=[ undercover, drug dealer])
143
+ Movies(id= 2, title= Fast 5, tags=[ bank robbery, full team])
147
144
148
145
To match multiple terms in your text search, separate each term
149
146
with spaces in the ``Filters.text()`` builder method. The builder method
@@ -158,19 +155,16 @@ The following example runs a text search on the documents in the
158
155
``fast_and_furious_movies`` collection for titles that contain the
159
156
terms "fate" or "7":
160
157
161
- .. literalinclude:: /includes/fundamentals/code-snippets/SearchText.java
162
- :language: java
163
- :dedent:
164
- :start-after: begin multipleTermExample
165
- :end-before: end multipleTermExample
158
+ .. io-code-block::
166
159
167
- The following shows the output of the preceding code:
160
+ .. input:: /examples/generated/SearchTextTest.snippet.search-multiple-terms.kt
161
+ :language: kotlin
168
162
169
- .. code-block :: json
170
- :copyable: false
163
+ .. output ::
164
+ :language: console
171
165
172
- { "_id": 3, " title": " Furious 7", " tags": [" emotional"] }
173
- { "_id": 4, " title": " The Fate of the Furious", " tags": [" betrayal"] }
166
+ Movies(id= 3, title= Furious 7, tags=[ emotional])
167
+ Movies(id= 4, title= The Fate of the Furious, tags=[ betrayal])
174
168
175
169
Search Text by a Phrase
176
170
~~~~~~~~~~~~~~~~~~~~~~~
@@ -188,18 +182,15 @@ The following example runs a text search on the documents in the
188
182
``fast_and_furious_movies`` collection for titles that contain the
189
183
phrase "fate of the furious":
190
184
191
- .. literalinclude:: /includes/fundamentals/code-snippets/SearchText.java
192
- :language: java
193
- :dedent:
194
- :start-after: begin phraseExample
195
- :end-before: end phraseExample
185
+ .. io-code-block::
196
186
197
- The following shows the output of the preceding code:
187
+ .. input:: /examples/generated/SearchTextTest.snippet.search-phrase.kt
188
+ :language: kotlin
198
189
199
- .. code-block :: json
200
- :copyable: false
190
+ .. output ::
191
+ :language: console
201
192
202
- { "_id": 4, " title": " The Fate of the Furious", " tags": [" betrayal"] }
193
+ Movies(id= 4, title= The Fate of the Furious, tags=[ betrayal])
203
194
204
195
Search Text with Terms Excluded
205
196
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -223,16 +214,13 @@ The following example runs a text search on the documents in the
223
214
``fast_and_furious_movies`` collection for titles that contain the
224
215
term "furious", but do not contain the term "fast":
225
216
226
- .. literalinclude:: /includes/fundamentals/code-snippets/SearchText.java
227
- :language: java
228
- :dedent:
229
- :start-after: begin negateExample
230
- :end-before: end negateExample
217
+ .. io-code-block::
231
218
232
- The following shows the output of the preceding code:
219
+ .. input:: /examples/generated/SearchTextTest.snippet.exclude-term.kt
220
+ :language: kotlin
233
221
234
- .. code-block :: json
235
- :copyable: false
222
+ .. output ::
223
+ :language: console
236
224
237
- { "_id": 3, " title": " Furious 7", " tags": [" emotional"] }
238
- { "_id": 4, " title": " The Fate of the Furious", " tags": [" betrayal"] }
225
+ Movies(id= 3, title= Furious 7, tags=[ emotional])
226
+ Movies(id= 4, title= The Fate of the Furious, tags=[ betrayal])
0 commit comments