Text Search in the Aggregation Pipeline聚合管道中的文本搜索
On this page本页内容
This page describes text search capabilities for self-managed (non-Atlas) deployments. 本页介绍了用于自我管理(非Atlas)部署的文本搜索功能。For data hosted on MongoDB Atlas, MongoDB offers an improved full-text search solution, Atlas Search.对于托管在MongoDB Atlas上的数据,MongoDB提供了一个改进的全文搜索解决方案Atlas search。
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
阶段计算匹配文档的总views
。
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. "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"
元数据可用于$match
阶段(包括$text
操作)之后的预测、排序和条件。
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
阶段中匹配文档的总views
。
db.articles.aggregate(
[
{ $match: { $text: { $search: "saber -claro", $language: "es" } } },
{ $group: { _id: null, views: { $sum: "$views" } } }
]
)
$search
Stage in Atlas SearchAtlas搜索中的阶段
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
聚合阶段,用于对您的集合执行全文搜索。