-
Notifications
You must be signed in to change notification settings - Fork 51
DOCSP-29802: explain text index field weights #685
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
rustagir
merged 2 commits into
mongodb:master
from
rustagir:DOCSP-29802-text-index-weights
May 16, 2023
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -32,26 +32,40 @@ code for creating a text index and using the ``$text`` query operator. | |||||||
Examples | ||||||||
-------- | ||||||||
|
||||||||
The following examples use the ``movies`` collection in the ``sample_mflix`` | ||||||||
database. In order to enable text searches on the ``title`` field, create | ||||||||
the **text index** using the following command: | ||||||||
The following examples use sample data from the ``movies`` collection in | ||||||||
the ``sample_mflix`` database. In order to enable text searches on the | ||||||||
``title`` field, create a **text index** by using the following command: | ||||||||
|
||||||||
.. code-block:: javascript | ||||||||
|
||||||||
db.movies.createIndex({ title: "text" }); | ||||||||
|
||||||||
We use this text index for the examples, but you can create a compound | ||||||||
text index that broadens your text queries to multiple fields as follows: | ||||||||
We use a single field text index for the examples in this guide, but you can | ||||||||
create a compound text index that broadens your text queries to multiple | ||||||||
fields. The following command creates a text index on two fields in the | ||||||||
``movies`` collection: | ||||||||
|
||||||||
.. code-block:: javascript | ||||||||
|
||||||||
db.movies.createIndex({ title: "text", fullplot: "text" }); | ||||||||
db.movies.createIndex({ title: "text", plot: "text" }); | ||||||||
|
||||||||
.. tip:: Specify Field Weights in a Text Index | ||||||||
|
||||||||
When creating a compound text index, you can specify a weight option to | ||||||||
prioritize certain text fields in your index relative to the other | ||||||||
indexed fields. When you execute a text search, the field weights | ||||||||
influence how MongoDB calculates the :ref:`text search score | ||||||||
<node-text-search-score>` for each matching document. | ||||||||
|
||||||||
To learn more about specifying field weights when creating a text | ||||||||
index, see the :ref:`Text Indexes <node-fundamentals-text-indexes>` | ||||||||
section in the Indexes guide. | ||||||||
|
||||||||
You can only create *one* text index per collection. Every text search | ||||||||
queries all the fields specified in that index for matches. | ||||||||
|
||||||||
See the MongoDB server manual for more information on creating | ||||||||
:manual:`text indexes </core/index-text/>`. | ||||||||
See the MongoDB server manual to learn more about :manual:`text indexes | ||||||||
</core/index-text/>`. | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggestion: Put the reason for the link first. Sharing the link to the style guide here.
Suggested change
|
||||||||
|
||||||||
Query for Words | ||||||||
~~~~~~~~~~~~~~~ | ||||||||
|
@@ -160,6 +174,8 @@ Querying with the negated term yields the following documents: | |||||||
|
||||||||
.. include:: /includes/access-cursor-note.rst | ||||||||
|
||||||||
.. _node-text-search-score: | ||||||||
|
||||||||
Sort by Relevance | ||||||||
~~~~~~~~~~~~~~~~~ | ||||||||
|
||||||||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -187,38 +187,47 @@ To learn more, see | |||||||||
:v6.0:`Clustered Indexes </reference/method/db.createCollection/#std-label-db.createCollection.clusteredIndex>` and | ||||||||||
:v6.0:`Clustered Collections </core/clustered-collections>`. | ||||||||||
|
||||||||||
.. _node-fundamentals-text-indexes: | ||||||||||
|
||||||||||
Text Indexes | ||||||||||
~~~~~~~~~~~~ | ||||||||||
|
||||||||||
**Text indexes** support text search queries on string content. These indexes | ||||||||||
can include any field whose value is a string or an array of string elements. | ||||||||||
MongoDB supports text search for various languages. You can specify the | ||||||||||
default language as an option when creating the index. Read our guide on | ||||||||||
:doc:`text search queries </fundamentals/crud/read-operations/text>` for more | ||||||||||
information. | ||||||||||
|
||||||||||
The following example uses the ``createIndex()`` method to create a | ||||||||||
``text`` index on the ``fullplot`` field in the ``movies`` collection in | ||||||||||
the ``sample_mflix`` database and specifies ``english`` as the default | ||||||||||
language. | ||||||||||
MongoDB supports text search for various languages, so you can specify the | ||||||||||
default language as an option when creating the index. You can also | ||||||||||
specify a weight option to prioritize certain text fields in your | ||||||||||
index relative to the other indexed fields. | ||||||||||
|
||||||||||
Read our guide on :ref:`text search queries <node-fundamentals-text>` for | ||||||||||
more information. | ||||||||||
|
||||||||||
The following example uses the ``createIndex()`` method to perform the | ||||||||||
following actions: | ||||||||||
|
||||||||||
- Create a ``text`` index on the ``title`` and ``body`` fields in the | ||||||||||
``blogPosts`` collection | ||||||||||
- Specify ``english`` as the default language | ||||||||||
- Set the field weight of ``body`` to ``10`` and ``title`` to ``3`` | ||||||||||
|
||||||||||
.. literalinclude:: /code-snippets/indexes/text.js | ||||||||||
:language: js | ||||||||||
:start-after: begin-idx | ||||||||||
:end-before: end-idx | ||||||||||
:dedent: | ||||||||||
|
||||||||||
The following is an example of a query that would be covered by the index | ||||||||||
created above. Note that the ``sort`` is omitted because text indexes do not | ||||||||||
contain sort order. | ||||||||||
The following is an example of a query that uses the text index created | ||||||||||
in the previous code: | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggestion: Simplify
Suggested change
|
||||||||||
|
||||||||||
.. literalinclude:: /code-snippets/indexes/text.js | ||||||||||
:language: js | ||||||||||
:start-after: begin-query | ||||||||||
:end-before: end-query | ||||||||||
:dedent: | ||||||||||
|
||||||||||
To learn more, see :manual:`Text Indexes </core/index-text>`. | ||||||||||
To learn more about text indexes, see :manual:`Text Indexes | ||||||||||
</core/index-text>` in the Server manual. | ||||||||||
|
||||||||||
Geospatial Indexes | ||||||||||
~~~~~~~~~~~~~~~~~~ | ||||||||||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggestion: Can you break up this sentence into smaller sentences for readability? Based on the style guide, this is on the cusp of the suggested word length.