Text Search in the Aggregation Pipeline

New in version 2.6.

In the aggregation pipeline, text search is available via the use ofthe $text query operator in the $match stage.

Restrictions

For general $text operator restrictions, see operatorrestrictions.

In addition, text search in the aggregation pipeline has the followingrestrictions:

  • The $match stage that includes a $text must bethe first stage in the pipeline.
  • A text operator can only occur once in the stage.
  • The text operator expression cannot appear in$or or $not expressions.
  • The text search, by default, does not return the matching documentsin order of matching scores. Use the $meta aggregationexpression in the $sort stage.

Text Score

The $text operator assigns a score to each document thatcontains the search term in the indexed fields. The score representsthe relevance of a document to a given text search query. The score canbe part of a $sort pipeline specification as well as part of theprojection expression. The { $meta: "textScore" } expressionprovides information on the processing of the $text operation.See $meta aggregation for details on accessing the score forprojection or sort.

The metadata is only available after the $match stage thatincludes the $text operation.

Examples

The following examples assume a collection articles that has a textindex on the field subject:

  1. db.articles.createIndex( { subject: "text" } )

Calculate the Total Views for Articles that Contains a Word

The following aggregation searches for the term cake in the$match stage and calculates the total views for thematching documents in the $group stage.

  1. db.articles.aggregate(
  2. [
  3. { $match: { $text: { $search: "cake" } } },
  4. { $group: { _id: null, views: { $sum: "$views" } } }
  5. ]
  6. )

Return Results Sorted by Text Search Score

To sort by the text search score, include a $metaexpression in the $sort stage. The following examplematches on either the term cake or tea, sorts by thetextScore in descending order, and returns only the title fieldin the results set.

  1. db.articles.aggregate(
  2. [
  3. { $match: { $text: { $search: "cake tea" } } },
  4. { $sort: { score: { $meta: "textScore" } } },
  5. { $project: { title: 1, _id: 0 } }
  6. ]
  7. )

The specified metadata determines the sort order. For example, the"textScore" metadata sorts in descending order. See$meta for more information on metadata as well as anexample of overriding the default sort order of the metadata.

Match on Text Score

The "textScore" metadata is available for projections, sorts, andconditions subsequent the $match stage that includes the$text operation.

The following example matches on either the term cake or tea,projects the title and the score fields, and then returns onlythose documents with a score greater than 1.0.

  1. db.articles.aggregate(
  2. [
  3. { $match: { $text: { $search: "cake tea" } } },
  4. { $project: { title: 1, _id: 0, score: { $meta: "textScore" } } },
  5. { $match: { score: { $gt: 1.0 } } }
  6. ]
  7. )

The following aggregation searches in spanish for documents thatcontain the term saber but not the term claro in the$match stage and calculates the total views for thematching documents in the $group stage.

  1. db.articles.aggregate(
  2. [
  3. { $match: { $text: { $search: "saber -claro", $language: "es" } } },
  4. { $group: { _id: null, views: { $sum: "$views" } } }
  5. ]
  6. )