Skip to content

Commit 23a90ea

Browse files
committed
DOCSP-29802: explain text index field weights (#685)
* DOCSP-29802: explain text index field weights * CD PR fixes (cherry picked from commit 1bb2ae4) (cherry picked from commit c0f3135)
1 parent 92ae557 commit 23a90ea

File tree

3 files changed

+59
-32
lines changed

3 files changed

+59
-32
lines changed

source/code-snippets/indexes/text.js

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,26 @@ const client = new MongoClient(uri);
1010
async function run() {
1111
try {
1212
// begin-idx
13-
const database = client.db("sample_mflix");
14-
const movies = database.collection("movies");
13+
const myDB = client.db("testDB");
14+
const myColl = myDB.collection("blogPosts");
1515

16-
// Create a text index on the "fullplot" field in the
17-
// "movies" collection.
18-
const result = await movies.createIndex({ fullplot: "text" }, { default_language: "english" });
19-
console.log(`Index created: ${result}`);
16+
// Create a text index on the "title" and "body" fields
17+
const result = await myColl.createIndex(
18+
{ title: "text", body: "text" },
19+
{ default_language: "english" },
20+
{ weights: { body: 10, title: 3 } }
21+
);
2022
// end-idx
23+
console.log(`Index created: ${result}`);
2124

2225
// begin-query
23-
const query = { $text: { $search: "java coffee shop" } };
24-
const projection = { _id: 0, fullplot: 1 };
25-
const cursor = movies
26-
.find(query)
27-
.project(projection);
26+
const query = { $text: { $search: "life ahead" } };
27+
const projection = { _id: 0, title: 1 };
28+
const cursor = myColl.find(query).project(projection);
2829
// end-query
29-
30+
for await (const doc of cursor) {
31+
console.log(doc);
32+
}
3033
} finally {
3134
await client.close();
3235
}

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

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,26 +32,40 @@ code for creating a text index and using the ``$text`` query operator.
3232
Examples
3333
--------
3434

35-
The following examples use the ``movies`` collection in the ``sample_mflix``
36-
database. In order to enable text searches on the ``title`` field, create
37-
the **text index** using the following command:
35+
The following examples use sample data from the ``movies`` collection in
36+
the ``sample_mflix`` database. In order to enable text searches on the
37+
``title`` field, create a **text index** by using the following command:
3838

3939
.. code-block:: javascript
4040

4141
db.movies.createIndex({ title: "text" });
4242

43-
We use this text index for the examples, but you can create a compound
44-
text index that broadens your text queries to multiple fields as follows:
43+
We use a single field text index for the examples in this guide, but you can
44+
create a compound text index that broadens your text queries to multiple
45+
fields. The following command creates a text index on two fields in the
46+
``movies`` collection:
4547

4648
.. code-block:: javascript
4749

48-
db.movies.createIndex({ title: "text", fullplot: "text" });
50+
db.movies.createIndex({ title: "text", plot: "text" });
51+
52+
.. tip:: Specify Field Weights in a Text Index
53+
54+
When creating a compound text index, you can specify a weight option to
55+
prioritize certain text fields in your index. When you execute a text
56+
search, the field weights influence how MongoDB calculates the
57+
:ref:`text search score <node-text-search-score>` for each matching
58+
document.
59+
60+
To learn more about specifying field weights when creating a text
61+
index, see the :ref:`Text Indexes <node-fundamentals-text-indexes>`
62+
section in the Indexes guide.
4963

5064
You can only create *one* text index per collection. Every text search
5165
queries all the fields specified in that index for matches.
5266

53-
See the MongoDB server manual for more information on creating
54-
:manual:`text indexes </core/index-text/>`.
67+
To learn more about text indexes, see :manual:`Text Indexes
68+
</core/index-text>` in the Server manual.
5569

5670
Query for Words
5771
~~~~~~~~~~~~~~~
@@ -160,6 +174,8 @@ Querying with the negated term yields the following documents:
160174

161175
.. include:: /includes/access-cursor-note.rst
162176

177+
.. _node-text-search-score:
178+
163179
Sort by Relevance
164180
~~~~~~~~~~~~~~~~~
165181

source/fundamentals/indexes.txt

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -187,38 +187,46 @@ To learn more, see
187187
:v6.0:`Clustered Indexes </reference/method/db.createCollection/#std-label-db.createCollection.clusteredIndex>` and
188188
:v6.0:`Clustered Collections </core/clustered-collections>`.
189189

190+
.. _node-fundamentals-text-indexes:
191+
190192
Text Indexes
191193
~~~~~~~~~~~~
192194

193195
**Text indexes** support text search queries on string content. These indexes
194196
can include any field whose value is a string or an array of string elements.
195-
MongoDB supports text search for various languages. You can specify the
196-
default language as an option when creating the index. Read our guide on
197-
:doc:`text search queries </fundamentals/crud/read-operations/text>` for more
198-
information.
199197

200-
The following example uses the ``createIndex()`` method to create a
201-
``text`` index on the ``fullplot`` field in the ``movies`` collection in
202-
the ``sample_mflix`` database and specifies ``english`` as the default
203-
language.
198+
MongoDB supports text search for various languages, so you can specify the
199+
default language as an option when creating the index. You can also
200+
specify a weight option to prioritize certain text fields in your
201+
index. These weights denote the significance of fields relative to the
202+
other indexed fields.
203+
204+
To learn more about text searches, see our guide on :ref:`text search queries <node-fundamentals-text>`.
205+
206+
The following example uses the ``createIndex()`` method to perform the
207+
following actions:
208+
209+
- Create a ``text`` index on the ``title`` and ``body`` fields in the
210+
``blogPosts`` collection
211+
- Specify ``english`` as the default language
212+
- Set the field weight of ``body`` to ``10`` and ``title`` to ``3``
204213

205214
.. literalinclude:: /code-snippets/indexes/text.js
206215
:language: js
207216
:start-after: begin-idx
208217
:end-before: end-idx
209218
:dedent:
210219

211-
The following is an example of a query that would be covered by the index
212-
created above. Note that the ``sort`` is omitted because text indexes do not
213-
contain sort order.
220+
The following query uses the text index created in the preceding code:
214221

215222
.. literalinclude:: /code-snippets/indexes/text.js
216223
:language: js
217224
:start-after: begin-query
218225
:end-before: end-query
219226
:dedent:
220227

221-
To learn more, see :manual:`Text Indexes </core/index-text>`.
228+
To learn more about text indexes, see :manual:`Text Indexes
229+
</core/index-text>` in the Server manual.
222230

223231
Geospatial Indexes
224232
~~~~~~~~~~~~~~~~~~

0 commit comments

Comments
 (0)