On this page本页内容
In the aggregation pipeline, text search is available via the use of the 在聚合管道中,可以通过在$text query operator in the $match stage.$match阶段使用$text查询运算符进行文本搜索。
For general 有关常规$text operator restrictions, see operator restrictions.$text运算符限制,请参阅运算符限制。
In addition, text search in the aggregation pipeline has the following restrictions:此外,聚合管道中的文本搜索具有以下限制:
$match stage that includes a $text must be the first stage in the pipeline.$text的$match阶段必须是管道中的第一个阶段。text operator can only occur once in the stage.$text运算符只能在阶段中出现一次。text operator expression cannot appear in $or or $not expressions.text运算符表达式不能出现在$or或$not表达式中。$meta aggregation expression in the $sort stage.$sort阶段使用$meta聚合表达式。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阶段之后可用。
The following examples assume a collection 以下示例假定某个集合articles that has a text index on the field subject:articles在字段subject上有文本索引:
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阶段中匹配文档的总视图。
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.cake或tea,按textScore降序排序,并仅返回结果集中的title字段。
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。
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.cake或tea,投影title和score字段,然后仅返回score大于1.0的文档。
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阶段中匹配文档的总视图。