Note
This page describes text query capabilities for self-managed (non-Atlas) deployments. For data hosted on MongoDB, MongoDB also offers an improved full-text query solution, MongoDB Search and a vector search solution, Vector Search.本页介绍自我管理(非Atlas)部署的文本查询功能。对于托管在MongoDB上的数据,MongoDB还提供了改进的全文查询解决方案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包含$matchstage that includes a$textmust be the first stage in the pipeline.$text的$match阶段必须是管道中的第一阶段。A$textoperator can only occur once in the stage.$text运算符在阶段中只能出现一次。The$textoperator expression cannot appear in$oror$notexpressions.$text运算符表达式不能出现在$or或$not表达式中。默认情况下,$text, by default, does not return the matching documents in order of matching scores.$text不会按匹配分数的顺序返回匹配的文档。To sort by descending score, use the要按得分降序排序,请在$metaaggregation expression in the$sortstage.$sort阶段使用$meta聚合表达式。
Text Score文本得分
The $text operator assigns a score to each result document. The score represents the relevance of a document to a given query. $text运算符为每个结果文档分配一个分数。分数表示文档与给定查询的相关性。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.cake或tea这两个术语,按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. See $meta for more information on metadata as well as an example of overriding the default sort order of the metadata."textScore"元数据按降序排序。有关元数据的更多信息以及覆盖元数据默认排序顺序的示例,请参阅$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.cake或tea这两个术语,投影title和score字段,然后仅返回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 MongoDB SearchMongoDB搜索中的$search阶段
$search Stage in MongoDB SearchFor data hosted on MongoDB, MongoDB Search provides the $search aggregation stage to perform full-text search on your collections.对于托管在MongoDB上的数据,MongoDB搜索提供了$Search聚合阶段,可以对集合执行全文搜索。