Skip to content

Commit fb12d9a

Browse files
author
Chris Cho
authored
DOCSP-10237: address feedback on text search (#96)
* DOCSP-10237: address feedback on text search
1 parent 772147d commit fb12d9a

File tree

2 files changed

+39
-44
lines changed

2 files changed

+39
-44
lines changed

source/code-snippets/crud/startrek.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ async function word(movies) {
2929

3030
async function phrase(movies) {
3131
// start phrase text example
32-
const query = { $text: { $search: '"star trek"' } };
32+
const query = { $text: { $search: "\"star trek\"" } };
3333

3434
// Return only the `title` of each matched document
3535
const projection = {
@@ -50,7 +50,7 @@ async function phrase(movies) {
5050

5151
async function negation(movies) {
5252
// start negation text example
53-
const query = { $text: { $search: '"star trek" -"into darkness"' } };
53+
const query = { $text: { $search: "\"star trek\" -\"into darkness\"" } };
5454

5555
// Include only the `title` field of each matched document
5656
const projection = {
@@ -71,7 +71,7 @@ async function negation(movies) {
7171

7272
async function relevance(movies) {
7373
// start relevance text example
74-
const query = { $text: { $search: '"star trek" -"into darkness"' } };
74+
const query = { $text: { $search: "\"star trek\" -\"into darkness\"" } };
7575

7676
// sort returned documents by descending text relevance score
7777
const sort = { score: { $meta: "textScore" } };

source/fundamentals/crud/read-operations/text.txt

Lines changed: 36 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4,48 +4,45 @@ Search Text
44

55
.. default-domain:: mongodb
66

7-
Use the :manual:`$text
8-
</reference/operator/query/text/>`
9-
operator to perform text searches on fields which have a
10-
:manual:`text index </core/index-text/>`.
11-
127
Overview
138
--------
149

15-
Text searches let you search a collection of string-valued fields for
16-
words or phrases, instead of matching substrings like
17-
:mdn:`regular expressions
18-
<Web/JavaScript/Guide/Regular_Expressions>`,
19-
also known as RegEx. While regex is very powerful, and MongoDB even
20-
offers a ``$regex`` operator, text searches work particularly well when
21-
applied to unstructured text, like transcripts, essays, or even web
22-
pages. Because MongoDB's text indexes are aware of concepts like
23-
plurality, case sensitivity, stop words, whitespace, and punctuation in
24-
multiple languages, all you have to do is query with a search term to
25-
find documents that contain your term.
26-
10+
Text search, using the ``$text`` query operator, lets you search string
11+
type fields in your collection for words or phrases. This operator
12+
performs a logical ``OR`` on each term separated by a space in the search
13+
string. You can also specify additional options to the operator to
14+
handle case sensitivity, word stemming (e.g. plural forms, tense) and stop
15+
words for a supported language. This is particularly useful for
16+
unstructured text such as transcripts, essays, or web pages.
2717

28-
.. admonition::
18+
The ``$text`` query operator requires that you specify the search field in
19+
a **text index** on your collection. See the examples below for sample
20+
code for creating a text index and using the ``$text`` query operator.
2921

30-
You can only create *one* text index per collection, so every text
31-
search queries all fields used by the index for term matches.
3222

3323
Examples
3424
--------
3525

36-
The following examples use the ``movies`` collection in the
37-
``sample_mflix`` sample database and assume that you have created a text
38-
index on the ``title`` field.
26+
The following examples use the ``movies`` collection in the ``sample_mflix``
27+
database. In order to enable text searches on the ``title`` field, create
28+
the **text index** using the following command:
29+
30+
.. code-block:: javascript
3931

40-
.. note::
32+
db.movies.createIndex({ title: "text" });
33+
34+
We use this text index for the examples, but you can create a compound
35+
text index that broadens your text queries to multiple fields as follows:
36+
37+
.. code-block:: javascript
4138

42-
Text searches require a text index. To create a text index on the
43-
``title`` field of the ``movies`` collection of the ``sample_mflix``
44-
database, use the following command on the sample_mflix database:
39+
db.movies.createIndex({ title: "text", fullplot: "text" });
4540

46-
.. code-block:: javascript
41+
You can only create *one* text index per collection. Every text search
42+
queries all the fields specified in that index for matches.
4743

48-
db.movies.createIndex({title: "text"})
44+
See the MongoDB server manual for more information on creating
45+
:manual:`text indexes </core/index-text/>`.
4946

5047
Query for Words
5148
~~~~~~~~~~~~~~~
@@ -80,10 +77,10 @@ This operation returns the following documents:
8077
{ title: 'Star Trek II: The Wrath of Khan' }
8178

8279
Success! The query found every document in the ``movies`` collection
83-
with a title including the word "trek". Unfortunately, it looks like the
84-
search included one unintended item: "Trek Nation," which is a movie
85-
about Star Trek and not a Star Trek movie. To solve this, we can query
86-
with a more specific **phrase**.
80+
with a title including the word "trek". Unfortunately, the search included
81+
one unintended item: "Trek Nation," which is a movie about Star Trek and not
82+
part of the Star Trek movie series. To solve this, we can query with a more
83+
specific **phrase**.
8784

8885
Query By Phrase
8986
~~~~~~~~~~~~~~~
@@ -119,18 +116,16 @@ matches the following documents:
119116
These results include all movies in the database that contain the phrase
120117
"star trek", which in this case results in only fictional Star Trek
121118
movies. Unfortunately, though, this query returned "Star Trek Into
122-
Darkness", a movie that nobody liked much. To solve this, we can omit
123-
that document with a **negation**.
119+
Darkness", a movie that was not part of the original series of movies. To
120+
resolve this issue, we can omit that document with a **negation**.
124121

125122
Query with Negations
126123
~~~~~~~~~~~~~~~~~~~~
127124

128-
Because you probably don't want to be associated with such an unpopular
129-
movie, you can choose to exclude it from the results set using a negated
130-
term. To use a negated term, place a negative sign (``-``) in front of
131-
the term you'd like to omit from the result set. Any documents that
132-
contain this term will not show up in your results. Since this query
133-
includes two terms, separate the terms with spaces.
125+
To use a negated term, place a negative sign (``-``) in front of the term
126+
you would like to omit from the result set. The query operation omits any
127+
documents that contain this term from the search result. Since this query
128+
includes two distinct terms, separate them with a space.
134129

135130
.. literalinclude:: /code-snippets/crud/startrek.js
136131
:language: javascript

0 commit comments

Comments
 (0)