Overview
In this guide, you can learn how to perform searches on your documents by using the MongoDB Search feature. Laravel MongoDB provides an API to perform MongoDB Search queries directly with your models. This guide describes how to create MongoDB Search indexes and provides examples of how to use the Laravel Integration to perform searches.
MongoDB Search API在内部使用 $search聚合操作符符来执行查询。要学习;了解有关此操作符的更多信息,请参阅Atlas文档中的 $搜索 参考。
重要
Atlas和 Community Edition 版本要求
$search聚合管道操作符仅适用于在运行MongoDB v. 或更高版本的MongoDB4 2Atlas集群上托管的集合,或者运行MongoDB v. 或更高版本的MongoDB Community8 2Edition集群上托管的集合。要学习;了解有关这些要求的更多信息,请参阅MongoDB搜索文档。
注意
您可能无法将本指南中描述的方法用于所有类型的 MongoDB Search 查询。对于更复杂的使用案例,请使用聚合操作来创建聚合管道。
要对 MongoDB 中的向量嵌入进行搜索,您可以使用 Laravel MongoDB MongoDB 向量搜索 API。要了解有关此功能的更多信息,请参阅运行 MongoDB 向量搜索查询指南。
创建MongoDB搜索索引
您可以通过以下任一方式创建 MongoDB 搜索索引:
调用
Schema外观上的create()方法,并传递带有索引创建详细信息的searchIndex()辅助程序方法。要了解有关此策略的更多信息,请参见 Schema Builder 指南的管理 MongoDB Search 和 MongoDB Vector Search 索引部分。Access a collection, then call the
createSearchIndex()method from the MongoDB PHP Library, as shown in the following code:$collection = DB::connection('mongodb')->getCollection('movies'); $collection->createSearchIndex( ['mappings' => ['dynamic' => true]], ['name' => 'search_index'] );
重要
异步搜索索引操作
创建和删除MongoDB搜索索引的操作异步运行。因此,在删除同一命名空间中具有相同名称的索引时,您无法创建新的MongoDB搜索索引。确保等到MongoDB删除冲突索引后再调用 createSearchIndex() 方法。
Additionally, you must wait until the index is queryable before using it. To see if your index is queryable, run the Collection::listSearchIndexes() method, find the document describing the new index in the method output, and verify the queryable field value. Documents added after index creation are not available immediately in the query results.
执行查询
在本节中,您可以学习如何在 Laravel 集成中使用 MongoDB 搜索 API。
一般查询
Laravel 集成提供 search() 方法作为查询构建者方法和 Eloquent 模型方法。您可以使用 search() 方法对集合中的文档运行MongoDB搜索查询。
您必须将 operator 参数传递给 search() 方法,该参数是 SearchOperatorInterface 的实例或包含操作符类型、字段名称和查询值的数组。您可以通过调用 Search::text() 方法并传递您要查询的字段以及您的搜索词或短语来创建 SearchOperatorInterface 的实例。
您必须在应用程序中包含以下导入语句才能创建 SearchOperatorInterface 实例:
use MongoDB\Builder\Search;
以下代码对 Movie 模型的 title 字段执行 MongoDB 搜索查询,以查找术语'dream':
$movies = Movie::search( sort: ['title' => 1], operator: Search::text('title', 'dream'), )->all();
您可以使用 search() 方法执行多种类型的MongoDB搜索查询。根据所需的查询,您可以将以下可选参数传递给 search():
可选参数 | 类型 | 说明 |
|---|---|---|
|
| 提供要使用的MongoDB搜索索引的名称 |
|
| 指定在原始上下文中显示搜索词的高亮选项 |
|
| 在专用搜索节点上并行化搜索查询各分段 |
|
| 指定用于检索结果计数的计数选项 |
|
| 指定一个参考点,用于返回紧接该点之后开始的文档 |
|
| 指定一个参考点,用于返回紧接该点之前开始的文档 |
|
| 指定是否检索结果得分的明细 |
|
| 指定用于对结果进行排序的字段 |
|
| 指定是对后端数据库执行完整文档查找,还是直接从MongoDB 搜索仅返回存储的源字段 |
To learn more about these parameters, see the Fields section of the $search operator reference in the Atlas documentation.
自动完成查询
Laravel Integration 提供了 autocomplete() 方法,既可用作查询构建器方法,也可用作 Eloquent 模型方法。您可以使用 autocomplete() 方法运行集合中文档的自动完成搜索。此方法仅返回您指定为查询路径的字段的值。
要学习有关此类MongoDB搜索查询的更多信息,请参阅Atlas文档中的自动完成参考。
注意
必须先在集合上创建具有自动完成配置的MongoDB搜索索引,然后才能执行自动完成搜索。请参阅本指南的创建MongoDB搜索索引部分,学习;了解有关创建搜索索引的更多信息。
以下代码对 title字段上的string "jak" 执行MongoDB搜索自动完成查询:
$movies = Movie::autocomplete('title', 'jak')->all();
您还可以将以下可选参数传递给 autocomplete() 方法来定制查询:
可选参数 | 类型 | 说明 | 默认值 |
|---|---|---|---|
|
| 启用模糊搜索和模糊搜索选项 |
|
|
| 指定搜索词元的顺序 |
|
To learn more about these parameters, see the Options section of the autocomplete operator reference in the Atlas documentation.