管道阶段
MongoDB Search queries take the form of an aggregation pipeline stage. MongoDB Search provides $search and $searchMeta stages, both of which must be the first stage in any query pipeline, including the $lookup and $unionWith sub-pipelines. These stages can be used in conjunction with other aggregation pipeline stages in your query pipeline.
根据所选的管道阶段,查询返回全文搜索的搜索结果或关于搜索结果的元数据:
聚合管道阶段 | 用途 |
|---|---|
返回全文搜索的搜索结果。 | |
返回关于搜索结果的元数据。 |
运算符和收集器
MongoDB Search 还提供可在 $search和$searchMeta 聚合管道阶段中使用的查询运算符和收集器。MongoDB Search 操作符允许您从集群上的集合中查找和检索相关数据。收集器返回表示搜索元数据结果的文档。
您可以使用MongoDB Search 操作符来查询词、短语、地理形状和点、数值、类似文档、同义词等。
You can also search using regex and wildcard expressions. The MongoDB Search compound operator allows you to combine multiple operators inside your $search stage to perform a complex search and filter of data based on what must, must not, or should be present in the documents returned by MongoDB Search. You can use the compound operator to also match or filter documents in the $search stage itself. Running $match after $search is less performant than running $search with the compound operator.
如需了解有关操作符和收集器的更多信息,请参阅操作符和收集器。
查询处理
mongod和mongot在同一节点上When you run a query, MongoDB Search uses the configured read preference to identify the node on which to run the query. The query first goes to the MongoDB process, which is
mongodfor a replica set cluster ormongosfor a sharded cluster.For a replica set cluster, the MongoDB process routes the query to the
mongoton the same node. For sharded clusters, your cluster data is partitioned acrossmongodinstances and eachmongotknows about the data on themongodon the same node only. Therefore, you can't run MongoDB Search queries that target a particular shard.mongosdirects the queries to all shards, making these scatter gather queries. If you use zones to distribute a sharded collection over a subset of the shards in the cluster, MongoDB Search routes the query to the zone that contains the shards for the collection that you are querying and runs your$searchqueries on just the shards where the collection is located.MongoDB Search 执行搜索和评分,并将匹配结果的文档ID 和其他搜索元数据返回
mongod。然后,mongod对匹配结果隐式执行完整文档查找,并将结果返回客户端。如果在查询中使用
$search并发选项,则 MongoDB Search 将启用查询内并行机制。要了解更多信息,请参阅并行查询各分段。mongod和mongot在不同节点上When you run a query, the query first goes to the
mongodbased on the configured read preference. By default, themongodprocess routes the search query through a load balancer on the same node, which distributes the requests across all of themongotprocesses. Alternatively, if you set thesearchNodePreferenceparameter for your$searchstage, MongoDB Search runs your query against the same search node each time.MongoDB Search
mongot进程执行搜索和评分,并将匹配结果的文档ID 和元数据返回给mongod。然后,mongod对匹配结果执行完整文档查找,并将结果返回给客户端。如果在查询中使用$search并发 选项, MongoDB Search 将启用查询内并行机制。要学习;了解更多信息,请参阅跨分段并行查询执行。
评分
MongoDB Search 将基于相关性的分数与结果设立的每个文档相关联。基于相关性的评分允许MongoDB Search 按从最高分到最低分的顺序返回文档。如果查询术语频繁出现在文档中, MongoDB搜索对文档的评分较高;如果查询术语出现在集合中的许多文档中,MongoDB 搜索的评分较低。MongoDB Search 还支持通过提升、衰减或其他修改选项来自定义基于相关性的默认分数。要学习;了解有关自定义结果分数的更多信息,请参阅对结果中的文档进行评分。
支持的客户端
您可以使用以下客户端创建并运行MongoDB搜索查询:
排查查询问题
空结果集
mongot 不返回任何错误,但在以下情况下返回空结果集: $search查询:
引用了不存在的索引。如果您不在查询中按名称指定索引, MongoDB Search 默认使用名为
default的索引。如果您没有名为default的索引,或者您指定的索引不存在,则MongoDB Search 不会返回错误并返回空结果设立。您可以使用index选项按名称指定有效索引。
PlanExecutor 错误
mongot 如果$search查询出现以下情况,则会返回PlanExecutor错误:
指定作为不正确数据类型进行索引的字段。在这种情况下,如果您运行查询, MongoDB Search 将返回一条错误消息,标识索引不正确的字段及其正确的数据类型。示例:
PlanExecutor error during aggregation :: caused by :: Cannot facet on field "genres" because it was not indexed as a "stringFacet" field. 示例,要对
string、number或date字段运行facet(MongoDB搜索操作符)查询,请使用相应的MongoDB搜索字段类型为字段创建索引,例如stringFacet、number和date。要学习;了解详情,请参阅支持和不支持的数据类型。