Skip to content

Commit 9cfa055

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) (cherry picked from commit cc24b83)
1 parent 2731410 commit 9cfa055

File tree

3 files changed

+57
-33
lines changed

3 files changed

+57
-33
lines changed

source/code-snippets/indexes/text.js

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,26 @@ async function run() {
1212
await client.connect();
1313

1414
// begin-idx
15-
const database = client.db("sample_mflix");
16-
const movies = database.collection("movies");
15+
const myDB = client.db("testDB");
16+
const myColl = myDB.collection("blogPosts");
1717

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

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

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: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -155,32 +155,37 @@ Text Indexes
155155

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

163-
The following example uses the ``createIndex()`` method to create a
164-
``text`` index on the ``fullplot`` field in the ``movies`` collection in
165-
the ``sample_mflix`` database and specifies ``english`` as the default
166-
language.
159+
MongoDB supports text search for various languages, so you can specify the
160+
default language as an option when creating the index. You can also
161+
specify a weight option to prioritize certain text fields in your
162+
index. These weights denote the significance of fields relative to the
163+
other indexed fields.
164+
165+
To learn more about text searches, see our guide on :ref:`text search queries <node-fundamentals-text>`.
166+
167+
The following example uses the ``createIndex()`` method to perform the
168+
following actions:
169+
170+
- Create a ``text`` index on the ``title`` and ``body`` fields in the
171+
``blogPosts`` collection
172+
- Specify ``english`` as the default language
173+
- Set the field weight of ``body`` to ``10`` and ``title`` to ``3``
167174

168175
.. literalinclude:: /code-snippets/indexes/text.js
169176
:language: js
170177
:start-after: begin-idx
171178
:end-before: end-idx
172179

173-
The following is an example of a query that would be covered by the index
174-
created above. Note that the ``sort`` is omitted because text indexes do not
175-
contain sort order.
180+
The following query uses the text index created in the preceding code:
176181

177182
.. literalinclude:: /code-snippets/indexes/text.js
178183
:language: js
179184
:start-after: begin-query
180185
:end-before: end-query
181186

182-
For a full explanation of text search with MongoDB, refer to
183-
:manual:`Text Indexes </core/index-text>` in the MongoDB manual.
187+
To learn more about text indexes, see :manual:`Text Indexes
188+
</core/index-text>` in the Server manual.
184189

185190
Geospatial Indexes
186191
~~~~~~~~~~~~~~~~~~

0 commit comments

Comments
 (0)