Skip to content

Conversation

GromNaN
Copy link
Member

@GromNaN GromNaN commented Sep 8, 2025

Q A
Type feature
BC Break no
Fixed issues PHPORM-382

Summary

Add the $vectorSearch stage builder.

$dm->getRepository(Face::class)
   ->createAggregationBuilder()
   ->hydrate(VectorSearchResult::class)
   ->vectorSearch()
       ->index('descriptions')
       ->path('descriptionEmbeddings')
       ->filter(
           $builder->matchExpr()
               ->field('id')
               ->notEqual($face->id)
       )
       ->numCandidates($limit * 20)
       ->queryVector($face->descriptionEmbeddings)
       ->limit($limit)
   ->project()
       ->field('_id')->expression(0)
       ->field('face')->expression('$$ROOT')
       ->field('score')->meta('vectorSearchScore');

@GromNaN GromNaN added this to the 2.13.0 milestone Sep 8, 2025
@GromNaN GromNaN requested a review from Copilot September 8, 2025 18:58
@GromNaN GromNaN added the Feature label Sep 8, 2025
Copilot

This comment was marked as resolved.

Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds support for the MongoDB $vectorSearch aggregation stage to the Doctrine ODM aggregation builder. This enables vector similarity search functionality for MongoDB Atlas Vector Search.

  • Implements a new VectorSearch stage class with fluent API methods for all supported options
  • Adds comprehensive test coverage for all stage methods and option combinations
  • Supports all vector search parameters: exact matching, filtering, indexing, limits, candidates, path, and query vectors

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
lib/Doctrine/ODM/MongoDB/Aggregation/Stage/VectorSearch.php Main implementation of the VectorSearch stage with fluent API methods
tests/Doctrine/ODM/MongoDB/Tests/Aggregation/Stage/VectorSearchTest.php Comprehensive test suite covering all VectorSearch functionality

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@doctrine doctrine deleted a comment from Copilot AI Sep 8, 2025
Comment on lines +48 to +74
if ($this->exact !== null) {
$params['exact'] = $this->exact;
}

if ($this->filter !== null) {
$params['filter'] = $this->filter->getQuery();
}

if ($this->index !== null) {
$params['index'] = $this->index;
}

if ($this->limit !== null) {
$params['limit'] = $this->limit;
}

if ($this->numCandidates !== null) {
$params['numCandidates'] = $this->numCandidates;
}

if ($this->path !== null) {
$params['path'] = $this->path;
}

if ($this->queryVector !== null) {
$params['queryVector'] = $this->queryVector;
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

index, limit, path and queryVector are required. The server will return an error if they are not used.
Looking at the other stages, it seems that we don't check this before.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense to me. Maybe it's nice to document this as a standard in some sort of development decision documentation in the repo, as I think we've had conversations about this before.

Comment on lines +68 to +70
if ($this->path !== null) {
$params['path'] = $this->path;
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The path should be mapped to the field name using the class metadata.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a TODO item? If so, is there another ticket to track this?

This seems related to #2820 (comment) from the PR that introduced a VectorSearchIndex mapping.

Not added to the Stage class as this stage must be first
@GromNaN GromNaN merged commit ace0396 into doctrine:2.13.x Sep 10, 2025
22 checks passed
@GromNaN GromNaN deleted the PHPORM-382 branch September 10, 2025 10:45
*
* @see https://www.mongodb.com/docs/atlas/atlas-vector-search/vector-search-stage/#mongodb-pipeline-pipe.-vectorSearch
*/
public function vectorSearch(): Stage\VectorSearch
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIUC, this PR does not add a Stage::vectorSearch() method for the same reason that you deprecated Stage::search() in #2823, correct?

Comment on lines +68 to +70
if ($this->path !== null) {
$params['path'] = $this->path;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a TODO item? If so, is there another ticket to track this?

This seems related to #2820 (comment) from the PR that introduced a VectorSearchIndex mapping.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants