- Indexes >
- Indexing Tutorials >
- Text Search Tutorials >
- Create text Index to Satisfy the filter Component of Text Search
Create text Index to Satisfy the filter Component of Text Search¶
To perform a text search that also includes additional query conditions, the text command provides a filter option. A compound index can support the text search and the filter condition, such that the operation does not need to scan any documents to satisfy the filter condition; i.e. the returned text.stats contain 0 for the nscannedObjects field.
Procedure¶
To create a text index that can fulfill the filter condition of a text search:
Append scalar index fields to a text index, as in the following example which specifies an ascending index key on cited:
db.quotes.ensureIndex( { comments: "text", cited: 1 } )
Use the filter option in the text to specify an condition on the cited field, as in the following:
db.quotes.runCommand( "text", { search: "tomorrow", filter: { cited: { $gt: 10 } } } )
Considerations¶
When creating a compound index with that includes the text index, you cannot include multi-key index field or geospatial index field.
With a compound index that includes the text index and an ascending/descending key or keys, sort operations do not use the ascending/descending key from this index; only the text score determines the sort order.