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
查询运算符,可以进行文本搜索。
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
上具有文本索引:
db.articles.createIndex( { subject: "text" } )
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" } } } ] )
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
。
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 } } } ] )
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 Search$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
聚合阶段,对您的集合执行全文搜索。