개요
In this guide, you can learn how to perform searches on your documents by using the MongoDB Vector Search feature. The PHP library allows you to perform MongoDB Vector Search queries by using the Aggregation Builder.
To learn more about MongoDB Vector Search, see the MongoDB Vector Search Overview. The MongoDB 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.
참고
MongoDB Search
To perform advanced full-text search on your documents, you can use the MongoDB Search API. To learn about this feature, see the Run a MongoDB Search Query guide.
MongoDB Vector Search 인덱스
Before you can perform MongoDB Vector Search queries, you must create a MongoDB Vector Search index on your collection. To learn more about creating this index type, see the MongoDB Search Indexes guide.
벡터 검색 애그리게이션 단계
Import the following classes into your application to perform MongoDB Vector Search queries by using the Aggregation Builder:
use MongoDB\Builder\Pipeline; use MongoDB\Builder\Stage;
집계 파이프라인 에서 $vectorSearch 단계를 만들려면 다음 작업을 수행합니다.
파이프라인 단계를 저장
Pipeline인스턴스 를 만듭니다.Call the
Stage::vectorSearch()method to create the MongoDB Vector Search stage.vectorSearch()메서드 본문 내에서 벡터 쿼리 에 대한 기준을 지정합니다.
The following code demonstrates the template for constructing basic MongoDB Vector Search queries:
$pipeline = new Pipeline( Stage::vectorSearch( /* MongoDB Vector Search query specifications index: '<index name>', path: '<path to embeddings>', ...*/ ), );
다음 매개변수를 vectorSearch() 메서드에 전달해야 합니다.
Parameter | 유형 | 설명 |
|---|---|---|
|
| 벡터 검색 인덱스 의 이름 |
|
| 벡터 임베딩을 저장하는 필드 |
|
| 쿼리 의 벡터 표현 |
|
| 반환할 결과 수 |
Vector Search Query Examples
In this section, you can learn how to perform MongoDB Vector Search queries by using the Aggregation Builder. The examples in this section use sample data from the sample_mflix.embedded_movies collection.
참고
쿼리 벡터 길이
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 MongoDB Vector Search Quick Start and select PHP from the Select your language dropdown in the upper-right corner of the page.
기본 벡터 검색 쿼리
The following code performs a MongoDB Vector Search query on the plot_embedding vector field:
$pipeline = new 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
벡터 검색 점수
다음 코드는 앞의 예시 와 동일한 쿼리 수행하지만 문서 쿼리 벡터와 얼마나 잘 일치하는지 설명하는 title 필드 와 vectorSearchScore 메타 필드 만 출력합니다.
$pipeline = new 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
벡터 검색 옵션
You can use the vectorSearch() method to perform many types of MongoDB Vector Search queries. Depending on your desired query, you can pass the following optional parameters to vectorSearch():
선택적 매개변수 | 유형 | 설명 | 기본값 |
|---|---|---|---|
|
| 정확한 인접 이웃( |
|
|
| 검색 할 문서에 대한 사전 필터를 지정합니다. | 필터링 없음 |
|
| 검색 중에 사용할 가장 가까운 이웃의 수를 지정합니다. |
|
이러한 매개변수에 대해 자세히 $vectorSearch 학습 Atlas 문서에서 연산자 참조의 필드 섹션을 참조하세요.