Skip to content

Commit 8682718

Browse files
DOCSP-9563 crud search text (#85)
* added CRUD Search Text page
1 parent b836511 commit 8682718

File tree

4 files changed

+454
-15
lines changed

4 files changed

+454
-15
lines changed

source/fundamentals/crud/read-operations.txt

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,9 @@ Read Operations
99
- :doc:`/fundamentals/crud/read-operations/sort`
1010
- :doc:`/fundamentals/crud/read-operations/skip`
1111
- :doc:`/fundamentals/crud/read-operations/limit`
12-
- :doc:`/fundamentals/crud/read-operations/geo`
1312
- :doc:`/fundamentals/crud/read-operations/project`
14-
15-
..
16-
- :doc:`/fundamentals/crud/read-operations/text`
13+
- :doc:`/fundamentals/crud/read-operations/geo`
14+
- :doc:`/fundamentals/crud/read-operations/text`
1715

1816
.. toctree::
1917
:caption: Read Operations
@@ -23,9 +21,6 @@ Read Operations
2321
/fundamentals/crud/read-operations/sort
2422
/fundamentals/crud/read-operations/skip
2523
/fundamentals/crud/read-operations/limit
26-
/fundamentals/crud/read-operations/geo
2724
/fundamentals/crud/read-operations/project
28-
29-
..
30-
/fundamentals/crud/read-operations/text
31-
25+
/fundamentals/crud/read-operations/geo
26+
/fundamentals/crud/read-operations/text
Lines changed: 235 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,235 @@
1+
===========
2+
Search Text
3+
===========
4+
5+
.. default-domain:: mongodb
6+
7+
.. contents:: On this page
8+
:local:
9+
:backlinks: none
10+
:depth: 2
11+
:class: singlecol
12+
13+
Overview
14+
--------
15+
16+
In this guide, you can learn how to run a **text search** in the MongoDB
17+
Java driver.
18+
19+
You can use a text search to retrieve documents that contain a **term**
20+
or a **phrase** in a specified field. A term is a sequence of characters
21+
that excludes whitespace characters. A phrase is a sequence of terms
22+
with any number of whitespace characters.
23+
24+
The following sections show you how to perform the following types of
25+
text searches:
26+
27+
- Search Text by a Term
28+
- Search Text by a Phrase
29+
- Search Text with Terms Excluded
30+
31+
If you want to sort your text search results, see the :ref:`Text Search
32+
<sorts-crud-text-search>` section of our Sort Results guide.
33+
34+
Sample Documents
35+
~~~~~~~~~~~~~~~~
36+
37+
The following sections feature examples of text searches on the
38+
``fast_and_furious_movies`` collection. Each section uses a variable
39+
named ``collection`` to refer to the ``MongoCollection`` instance of the
40+
``fast_and_furious_movies`` collection.
41+
42+
The ``fast_and_furious_movies`` collection contains documents that
43+
describe one of the several movies that are part of the Fast and Furious
44+
movie franchise. Each document contains a title field and a tags field.
45+
46+
.. code-block:: json
47+
48+
{ "_id": 1, "title": "2 Fast 2 Furious ", "tags": ["undercover", "drug dealer"] }
49+
{ "_id": 2, "title": "Fast 5", "tags": ["bank robbery", "full team"] }
50+
{ "_id": 3, "title": "Furious 7", "tags": ["emotional"] }
51+
{ "_id": 4, "title": "The Fate of the Furious", "tags": ["betrayal"] }
52+
53+
Text Index
54+
~~~~~~~~~~
55+
56+
You must create a **text index** before running a text search. A text
57+
index specifies the string or string array field on which to run a text
58+
search.
59+
60+
In the following examples, you run text searches on the ``title``
61+
field in the ``fast_and_furious_movies`` collection. To enable text
62+
searches on the ``title`` field, create a text index using the
63+
:ref:`Indexes <index-text-indexes>` builder with the following
64+
snippet:
65+
66+
.. literalinclude:: /includes/fundamentals/code-snippets/SearchText.java
67+
:language: java
68+
:dedent:
69+
:start-after: begin textIndex
70+
:end-before: end textIndex
71+
72+
For more information, see the following resources:
73+
74+
- :ref:`Text Indexes <text-indexes>` section of our Indexes guide
75+
- :manual:`Text Indexes </core/index-text/>` Server Manual Entry
76+
77+
78+
Text Search
79+
-----------
80+
81+
Use the ``Filters.text()`` method to specify a text search.
82+
83+
The ``Filters.text()`` method uses the :doc:`Filters builder
84+
</fundamentals/builders/filters>` to define a query filter specifying
85+
what to search for during the text search. The query filter is
86+
represented by a :ref:`Bson <bson>` instance. Pass the query filter to the
87+
``find()`` method to run a text search.
88+
89+
When you execute the ``find()`` method, MongoDB runs a text search on
90+
all the fields indexed with the text index on the collection. MongoDB
91+
returns documents that contain one or more of the search terms and a
92+
relevance score for each result. For more information on relevance
93+
scores, see the :ref:`Text Search <sorts-crud-text-search>` section in
94+
our Sort Results guide.
95+
96+
Specify Options
97+
~~~~~~~~~~~~~~~
98+
99+
You can include ``TextSearchOptions`` as the second parameter of the
100+
``Filters.text()`` method to specify text search options such as case
101+
sensitivity. By default, text searches run without case sensitivity
102+
which means the search matches lowercase and uppercase values.
103+
104+
To specify a case sensitive search, use the following snippet:
105+
106+
.. code-block:: java
107+
108+
TextSearchOptions options = new TextSearchOptions().caseSensitive(true);
109+
Bson filter = Filters.text("SomeText", options);
110+
111+
See the following API documentation for more information:
112+
113+
- :java-core-api:`Filters.text() <com/mongodb/client/model/Filters.html#text(java.lang.String)>`
114+
- :java-core-api:`TextSearchOptions <com/mongodb/client/model/TextSearchOptions.html>`
115+
116+
.. _term_search:
117+
118+
Search Text by a Term
119+
~~~~~~~~~~~~~~~~~~~~~
120+
121+
Pass a term as a string to the ``Filters.text()`` method to specify the
122+
term in your text search.
123+
124+
Example
125+
```````
126+
127+
The following example runs a text search on the documents in the
128+
``fast_and_furious_movies`` collection for titles that contain the
129+
term "fast":
130+
131+
.. literalinclude:: /includes/fundamentals/code-snippets/SearchText.java
132+
:language: java
133+
:dedent:
134+
:start-after: begin termExample
135+
:end-before: end termExample
136+
137+
The following shows the output of the preceding code:
138+
139+
.. code-block:: json
140+
:copyable: false
141+
142+
{ "_id": 1, "title": "2 Fast 2 Furious ", "tags": ["undercover", "drug dealer"] }
143+
{ "_id": 2, "title": "Fast 5", "tags": ["bank robbery", "full team"] }
144+
145+
To match multiple terms in your text search, separate each term
146+
with spaces in the ``Filters.text()`` builder method. The builder method
147+
returns the text search query as a ``Bson`` instance. When you pass
148+
this to the ``find()`` method, it returns documents that match any of
149+
the terms.
150+
151+
Example
152+
```````
153+
154+
The following example runs a text search on the documents in the
155+
``fast_and_furious_movies`` collection for titles that contain the
156+
terms "fate" or "7":
157+
158+
.. literalinclude:: /includes/fundamentals/code-snippets/SearchText.java
159+
:language: java
160+
:dedent:
161+
:start-after: begin multipleTermExample
162+
:end-before: end multipleTermExample
163+
164+
The following shows the output of the preceding code:
165+
166+
.. code-block:: json
167+
:copyable: false
168+
169+
{ "_id": 3, "title": "Furious 7", "tags": ["emotional"] }
170+
{ "_id": 4, "title": "The Fate of the Furious", "tags": ["betrayal"] }
171+
172+
Search Text by a Phrase
173+
~~~~~~~~~~~~~~~~~~~~~~~
174+
175+
Pass a phrase with **escaped quotes** to the ``Filters.text()`` method to
176+
specify the phrase in your text search. Escaped quotes are double quote
177+
characters preceded by a backslash character. If you don't add escaped
178+
quotes around the phrase, the ``find()`` method runs a :ref:`term search
179+
<term_search>`.
180+
181+
Example
182+
```````
183+
184+
The following example runs a text search on the documents in the
185+
``fast_and_furious_movies`` collection for titles that contain the
186+
phrase "fate of the furious":
187+
188+
.. literalinclude:: /includes/fundamentals/code-snippets/SearchText.java
189+
:language: java
190+
:dedent:
191+
:start-after: begin phraseExample
192+
:end-before: end phraseExample
193+
194+
The following shows the output of the preceding code:
195+
196+
.. code-block:: json
197+
:copyable: false
198+
199+
{ "_id": 4, "title": "The Fate of the Furious", "tags": ["betrayal"] }
200+
201+
Search Text with Terms Excluded
202+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
203+
204+
For each term you want to exclude from your text search, prefix the term
205+
with a minus sign in the string that you pass to the ``Filters.text()``
206+
builder method.
207+
208+
None of the documents returned from the search contain the excluded term
209+
in your text index field.
210+
211+
.. note::
212+
213+
You must have at least one text search term if you want to
214+
exclude terms from your search.
215+
216+
Example
217+
```````
218+
219+
The following example runs a text search on the documents in the
220+
``fast_and_furious_movies`` collection for titles that contain the
221+
term "furious", but do not contain the term "fast":
222+
223+
.. literalinclude:: /includes/fundamentals/code-snippets/SearchText.java
224+
:language: java
225+
:dedent:
226+
:start-after: begin negateExample
227+
:end-before: end negateExample
228+
229+
The following shows the output of the preceding code:
230+
231+
.. code-block:: json
232+
:copyable: false
233+
234+
{ "_id": 3, "title": "Furious 7", "tags": ["emotional"] }
235+
{ "_id": 4, "title": "The Fate of the Furious", "tags": ["betrayal"] }

0 commit comments

Comments
 (0)