Text Indexes
Overview
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.
Tip
MongoDB offers an improved full-text search solution, Atlas Search. To learn more about Atlas Search indexes and how to use them, see the Atlas Search and Vector Search Indexes guide.
Sample Data
The examples in this guide use the sample_mflix.movies
collection
from the Atlas sample datasets. To learn how to create a
free MongoDB Atlas cluster and load the sample datasets, see the
Get Started with PyMongo.
Text Index on a Single Field
The following example creates a text index on the plot
field:
movies.create_index( [( "plot", "text" )] )
The following is an example of a query that uses the index created in the preceding code example:
query = { "$text": { "$search": "a time-traveling DeLorean" } } cursor = movies.find(query)
Collation
When you create an index, you can specify a default collation for all operations you perform on fields that are included in the index.
A collation is a set of language-specific rules for string comparison, such as for letter case and accent marks.
To specify a collation, create an instance of the Collation
class or a Python dictionary.
For a list of options to pass to the Collation
constructor or include as keys in the
dictionary, see Collation in the MongoDB Server manual.
Tip
Import Collation
To create an instance of the Collation
class, you must import it from
pymongo.collation
.
To use an index with a specified collation, your operation must meet the following criteria:
The operation uses the same collation as the one specified in the index.
The operation is covered by the index that contains the collation.
The following example creates the same index as the previous example,
but with a default collation of fr_CA
:
from pymongo.collation import Collation movies.create_index( [( "plot", "text" )], collation=Collation(locale='fr_CA') )
Text Index on Multiple Fields
A collection can contain only one text index. If you want to create a text index for multiple text fields, create a compound index. A text search runs on all the text fields within the compound index.
The following example creates a compound text index for the title
and genre
fields:
from pymongo.collation import Collation result = myColl.create_index( [("title", "text"), ("genre", "text")], default_language="english", weights={ "title": 10, "genre": 3 }, collation=Collation(locale='fr_CA') )
For more information, see Compound Text Index Restrictions and Text Indexes in the MongoDB Server manual.