Atlas Vector Search
在此页面上
Overview
In this guide, you can learn how to perform searches on your documents by using the Atlas Vector Search feature. The PHP library allows you to perform Atlas Vector Search queries by using the 聚合构建器.
注意
部署兼容性
You can use the Atlas Vector Search feature only when you connect to MongoDB Atlas clusters. This feature is not available for self-managed deployments.
To learn more about Atlas Vector Search, see the Atlas Vector
Search Overview. The
Atlas Vector Search implementation for the PHP library internally
uses the $vectorSearch
aggregation operator to perform queries. To
learn more about this operator, see the $vectorSearch reference in the
Atlas documentation.
注意
Atlas Search
To perform advanced full-text search on your documents, you can use the Atlas Search API. To learn about this feature, see the Atlas Search guide.
Atlas Vector Search 索引
Before you can perform Atlas Vector Search queries, you must create an Atlas Vector Search index on your collection. To learn more about creating this index type, see the Atlas Search 索引 guide.
Vector Search Aggregation Stage
将以下类导入到应用程序中,以使用聚合构建器执行Atlas Search查询:
use MongoDB\Builder\Stage;
要在聚合管道中创建 $vectorSearch
阶段,请执行以下操作:
创建一个大量来存储管道阶段。
Call the
Stage::vectorSearch()
method to create the Atlas Vector Search stage.Within the body of the
vectorSearch()
method, specify the criteria for your vector query.
以下代码演示了用于构建基本Atlas Search查询的模板:
$pipeline = [ Stage::vectorSearch( /* Atlas Vector Search query specifications index: '<index name>', path: '<path to embeddings>', ...*/ ), ];
You must pass the following parameters to the vectorSearch()
method:
Parameter | 类型 | 说明 |
---|---|---|
|
| Name of the vector search index |
|
| Field that stores vector embeddings |
|
| Vector representation of your query |
|
| Number of results to return |
Atlas Search查询示例
In this section, you can learn how to perform Atlas Vector
Search queries by using the Aggregation Builder. The examples in this
section use sample data from the sample_mflix.embedded_movies
collection.
注意
Query Vector Length
For demonstrative purposes, the examples in this section use sample query vectors that contain very few elements, compared to the query vector you might use in a runnable application. To view an example that contains the full-length query vector, see the Atlas Vector Search Quick Start and select PHP from the Select your language dropdown in the upper-right corner of the page.
Basic Vector Search Query
The following code performs an Atlas Vector Search query on the
plot_embedding
vector field:
$pipeline = [ Stage::vectorSearch( index: 'vector', path: 'plot_embedding', queryVector: [-0.0016261312, -0.028070757, -0.011342932], numCandidates: 150, limit: 5, ), Stage::project( _id: 0, title: 1, ), ]; $cursor = $collection->aggregate($pipeline); foreach ($cursor as $doc) { echo json_encode($doc), PHP_EOL; }
{"title":"Thrill Seekers"} {"title":"About Time"} {"title":"Timecop"} // Results truncated
Vector Search Score
The following code performs the same query as in the preceding example,
but outputs only the title
field and vectorSearchScore
meta
field, which describes how well the document matches the query vector:
$pipeline = [ Stage::vectorSearch( index: 'vector', path: 'plot_embedding', queryVector: [-0.0016261312, -0.028070757, -0.011342932], numCandidates: 150, limit: 5, ), Stage::project( _id: 0, title: 1, score: ['$meta' => 'vectorSearchScore'], ), ]; $cursor = $collection->aggregate($pipeline); foreach ($cursor as $doc) { echo json_encode($doc), PHP_EOL; }
{"title":"Thrill Seekers","score":0.927734375} {"title":"About Time","score":0.925750732421875} {"title":"Timecop","score":0.9241180419921875} // Results truncated
Vector Search Options
You can use the vectorSearch()
method to perform many types of Atlas
Vector Search queries. Depending on your desired query, you can pass the
following optional parameters to vectorSearch()
:
可选参数 | 类型 | 说明 | 默认值 |
---|---|---|---|
|
| Specifies whether to run an Exact Nearest Neighbor ( |
|
|
| Specifies a pre-filter for documents to search on | no filtering |
|
| Specifies the number of nearest neighbors to use during the search |
|