Text Search in the Aggregation Pipeline聚合管道中的文本搜索

On this page本页内容

MongoDB offers a full-text search solution, MongoDB Atlas Search, for data hosted on MongoDB Atlas. MongoDB为MongoDB Atlas上托管的数据提供了全文搜索解决方案MongoDB Atlas搜索A legacy text search capability is available for users self-managing MongoDB deployments.传统文本搜索功能可用于用户自我管理MongoDB部署。

In the aggregation pipeline, text search is available via the use of the $text query operator in the $match stage.在聚合管道中,通过在$match阶段中使用$text查询运算符,可以进行文本搜索。

Restrictions限制

For general $text operator restrictions, see operator restrictions.有关常规$text运算符限制,请参阅运算符限制

In addition, text search in the aggregation pipeline has the following restrictions:此外,聚合管道中的文本搜索具有以下限制:

  • The $match stage that includes a $text must be the first stage in the pipeline.包含$text$match阶段必须是管道中的第一个阶段。
  • A $text operator can only occur once in the stage.$text运算符在阶段中只能出现一次。
  • The $text operator expression cannot appear in $or or $not expressions.$text运算符表达式不能出现在表达式$or$not中。
  • The text search, by default, does not return the matching documents in order of matching scores. 默认情况下,文本搜索不会按匹配分数的顺序返回匹配文档。To sort by descending score, use the $meta aggregation expression in the $sort stage.要按递减分数排序,请在$sort阶段使用$meta聚合表达式。

Text Score文本分数

The $text operator assigns a score to each document that contains the search term in the indexed fields. $text运算符为索引字段中包含搜索项的每个文档分配分数。The score represents the relevance of a document to a given text search query. 分数表示文档与给定文本搜索查询的相关性。The score can be part of a $sort pipeline specification as well as part of the projection expression. 分数可以是$sort管道规范的一部分,也可以是投影表达式的一部分。The { $meta: "textScore" } expression provides information on the processing of the $text operation. { $meta: "textScore" }表达式提供有关$text操作处理的信息。See $meta for details on accessing the score for projection or sort.有关访问分数进行投影或排序的详细信息,请参阅$meta

The metadata is only available after the $match stage that includes the $text operation.元数据仅在包含$text操作的$match阶段之后可用。

Examples示例

The following examples assume a collection articles that has a text index on the field subject:以下示例假设一个集合articles在字段subject上具有文本索引:

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 the matching documents in the $group stage.以下聚合在$match阶段中搜索术语cake,并计算$group阶段中匹配文档的总视图。

db.articles.aggregate(
   [
     { $match: { $text: { $search: "cake" } } },
     { $group: { _id: null, views: { $sum: "$views" } } }
   ]
)

Return Results Sorted by Text Search Score返回按文本搜索分数排序的结果

To sort by the text search score, include a {$meta: "textScore"} expression in the $sort stage. 要按文本搜索分数排序,请在$sort阶段包含{$meta: "textScore"}表达式。The following example matches on either the term cake or tea, sorts by the textScore in descending order, and returns only the title field in the results set.下面的示例匹配术语caketea,按textScore降序排序,并仅返回结果集中的title字段。

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

The specified metadata determines the sort order. 指定的元数据确定排序顺序。For example, the "textScore" metadata sorts in descending order. 例如,"textScore"元数据按降序排序。See $meta for more information on metadata as well as an example of overriding the default sort order of the metadata.有关元数据的更多信息以及重写元数据的默认排序顺序的示例,请参阅$meta

Match on Text Score文本分数匹配

The "textScore" metadata is available for projections, sorts, and conditions subsequent the $match stage that includes the $text operation."textScore"元数据可用于包括$text操作的$match阶段之后的预测、排序和条件。

The following example matches on either the term cake or tea, projects the title and the score fields, and then returns only those documents with a score greater than 1.0.下面的示例匹配术语caketea,投影titlescore字段,然后仅返回score大于1.0的文档。

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

Specify a Language for Text Search指定用于文本搜索的语言

The following aggregation searches in spanish for documents that contain the term saber but not the term claro in the $match stage and calculates the total views for the matching documents in the $group stage.以下聚合以西班牙语搜索$match阶段中包含术语saber但不包含术语claro的文档,并计算$group阶段中匹配文档的总视图。

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

$search Stage in Atlas SearchAtlas搜索中的$search阶段

For data hosted on MongoDB Atlas, Atlas Search provides the $search aggregation stage to perform full-text search on your collections.对于托管在MongoDB Atlas上的数据,Atlas Search提供$search聚合阶段,对您的集合执行全文搜索。

←  Text Search Operators (Legacy)Text Search Languages →