Text search supports the search of string content in documents of a
collection. Text search introduces a new text index type and a new text command.
The text search process:
- tokenizes and stems the search term(s) during both the index creation
and the text command execution.
- assigns a score to each document that contains the search term in the
indexed fields. The score determines the relevance of a document to a
given search query.
By default, text command returns at most the top 100
matching documents as determined by the scores.
Before you can create a text index or run the text command, you need to manually enable the text
search. See Enable Text Search for information on
how to enable the text search feature.
Storage Requirements and Performance Costs
text indexes have the following storage requirements and
- text indexes change the space allocation method for all future
record allocations in a collection to usePowerOf2Sizes.
- text indexes can be large. They contain one index entry for each
unique post-stemmed word in each indexed field for each document
- Building a text index is very similar to building a large
multi-key index and will take longer than building a simple ordered
(scalar) index on the same data.
- When building a large text index on an existing collection,
ensure that you have a sufficiently high limit on open file
descriptors. See the recommended settings.
- text indexes will impact insertion throughput because MongoDB
must add an index entry for each unique post-stemmed word in each
indexed field of each new source document.
- Additionally, text indexes do not store phrases or information
about the proximity of words in the documents. As a result, phrase
queries will run much more effectively when the entire collection
fits in RAM.
Create a text Index
To perform text search, create a text index on the field or fields
whose value is a string or an array of string elements. To create a
text index, use the db.collection.ensureIndex() method
with a document that contains field and value pairs where the value is
the string literal text.
The following tutorials offer examples on text index creation
The text command can search for words and phrases. The
command matches on the complete stemmed words. For example, if a
document field contains the word blueberry, a search on the term
blue will not match the document. However, a search on either
blueberry or blueberries will match.
You cannot combine the text command, which requires a
special text index, with a query operator
that requires a different type of special index. For example you
cannot combine text with the $near operator.
For information and examples on various text search patterns, see
Search String Content for Text.
Text Search Output
The text command returns a document that contains the
See Output for information on the output.