Skip to content
Merged
4 changes: 2 additions & 2 deletions lib/Doctrine/ODM/MongoDB/Aggregation/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@ public function sample(int $size): Stage\Sample
*/
public function search(): Stage\Search
{
$stage = new Stage\Search($this);
$stage = new Stage\Search($this, $this->getDocumentPersister());

return $this->addStage($stage);
}
Expand Down Expand Up @@ -660,7 +660,7 @@ public function sortByCount(string $expression): Stage\SortByCount
*/
public function vectorSearch(): Stage\VectorSearch
{
$stage = new Stage\VectorSearch($this);
$stage = new Stage\VectorSearch($this, $this->getDocumentPersister());

return $this->addStage($stage);
}
Expand Down
10 changes: 8 additions & 2 deletions lib/Doctrine/ODM/MongoDB/Aggregation/Stage/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Doctrine\ODM\MongoDB\Aggregation\Stage\Search\SearchOperator;
use Doctrine\ODM\MongoDB\Aggregation\Stage\Search\SupportsAllSearchOperators;
use Doctrine\ODM\MongoDB\Aggregation\Stage\Search\SupportsAllSearchOperatorsTrait;
use Doctrine\ODM\MongoDB\Persisters\DocumentPersister;

use function in_array;
use function is_array;
Expand Down Expand Up @@ -70,7 +71,7 @@ class Search extends Stage implements SupportsAllSearchOperators
/** @var array<string, -1|1|SortMeta> */
private array $sort = [];

public function __construct(Builder $builder)
public function __construct(Builder $builder, private DocumentPersister $persister)
{
parent::__construct($builder);
}
Expand Down Expand Up @@ -105,7 +106,7 @@ public function getExpression(): array
}

if ($this->sort) {
$params->sort = (object) $this->sort;
$params->sort = (object) $this->persister->prepareSort($this->sort);
}

if ($this->operator !== null) {
Expand Down Expand Up @@ -214,4 +215,9 @@ protected function getSearchStage(): static
{
return $this;
}

protected function getDocumentPersister(): DocumentPersister
{
return $this->persister;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
use Doctrine\ODM\MongoDB\Aggregation\Stage;
use Doctrine\ODM\MongoDB\Aggregation\Stage\Search;
use Doctrine\ODM\MongoDB\Aggregation\Stage\Sort;
use Doctrine\ODM\MongoDB\Persisters\DocumentPersister;

use function array_map;
use function is_array;

/**
* @internal
Expand All @@ -18,7 +22,7 @@
*/
abstract class AbstractSearchOperator extends Stage implements SearchOperator
{
public function __construct(private Search $search)
public function __construct(private Search $search, private DocumentPersister $persister)
{
parent::__construct($search->builder);
}
Expand Down Expand Up @@ -64,4 +68,25 @@ protected function getSearchStage(): Search
{
return $this->search;
}

/**
* @param T $field
*
* @return T
*
* @template T of string|string[]
*/
protected function prepareFieldPath(string|array $field): string|array
{
if (is_array($field)) {
return array_map($this->persister->prepareFieldName(...), $field);
}

return $this->persister->prepareFieldName($field);
}

protected function getDocumentPersister(): DocumentPersister
{
return $this->persister;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Doctrine\ODM\MongoDB\Aggregation\Stage\Search;

use Doctrine\ODM\MongoDB\Aggregation\Stage\Search;
use Doctrine\ODM\MongoDB\Persisters\DocumentPersister;

use function array_values;

Expand All @@ -23,9 +24,9 @@ class Autocomplete extends AbstractSearchOperator implements ScoredSearchOperato
private string $tokenOrder = '';
private ?object $fuzzy = null;

public function __construct(Search $search, string $path, string ...$query)
public function __construct(Search $search, DocumentPersister $persister, string $path, string ...$query)
{
parent::__construct($search);
parent::__construct($search, $persister);

$this->query(...$query);
$this->path($path);
Expand Down Expand Up @@ -79,7 +80,7 @@ public function getOperatorParams(): object
{
$params = (object) [
'query' => $this->query,
'path' => $this->path,
'path' => $this->prepareFieldPath($this->path),
];

if ($this->tokenOrder) {
Expand Down
12 changes: 12 additions & 0 deletions lib/Doctrine/ODM/MongoDB/Aggregation/Stage/Search/Compound.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace Doctrine\ODM\MongoDB\Aggregation\Stage\Search;

use Closure;
use Doctrine\ODM\MongoDB\Aggregation\Stage\Search;
use Doctrine\ODM\MongoDB\Persisters\DocumentPersister;

use function array_map;

Expand All @@ -29,6 +31,11 @@ class Compound extends AbstractSearchOperator implements CompoundSearchOperatorI
private string $currentClause = 'must';
private ?int $minimumShouldMatch = null;

public function __construct(Search $search, private DocumentPersister $persister)
{
parent::__construct($search, $this->persister);
}

/**
* @param T $operator
*
Expand Down Expand Up @@ -122,4 +129,9 @@ protected function getCompoundStage(): Compound
{
return $this;
}

protected function getDocumentPersister(): DocumentPersister
{
return $this->persister;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Doctrine\ODM\MongoDB\Aggregation\Stage\Search;

use Doctrine\ODM\MongoDB\Aggregation\Stage\Search;
use Doctrine\ODM\MongoDB\Persisters\DocumentPersister;

/**
* @internal
Expand All @@ -19,9 +20,9 @@ class EmbeddedDocument extends AbstractSearchOperator implements SupportsEmbedda
private string $path;
private ?SearchOperator $operator = null;

public function __construct(Search $search, string $path)
public function __construct(Search $search, DocumentPersister $persister, string $path)
{
parent::__construct($search);
parent::__construct($search, $persister);

$this->path($path);
}
Expand Down Expand Up @@ -52,7 +53,7 @@ public function getOperatorName(): string

public function getOperatorParams(): object
{
$params = (object) ['path' => $this->path];
$params = (object) ['path' => $this->prepareFieldPath($this->path)];

if ($this->operator) {
$params->operator = (object) $this->operator->getExpression();
Expand Down
7 changes: 4 additions & 3 deletions lib/Doctrine/ODM/MongoDB/Aggregation/Stage/Search/Equals.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Doctrine\ODM\MongoDB\Aggregation\Stage\Search;

use Doctrine\ODM\MongoDB\Aggregation\Stage\Search;
use Doctrine\ODM\MongoDB\Persisters\DocumentPersister;
use MongoDB\BSON\ObjectId;
use MongoDB\BSON\UTCDateTime;

Expand All @@ -22,9 +23,9 @@ class Equals extends AbstractSearchOperator implements ScoredSearchOperator
private mixed $value;

/** @param string|int|float|ObjectId|UTCDateTime|null $value */
public function __construct(Search $search, string $path = '', $value = null)
public function __construct(Search $search, DocumentPersister $persister, string $path = '', $value = null)
{
parent::__construct($search);
parent::__construct($search, $persister);

$this
->path($path)
Expand Down Expand Up @@ -54,7 +55,7 @@ public function getOperatorName(): string
public function getOperatorParams(): object
{
$params = (object) [
'path' => $this->path,
'path' => $this->prepareFieldPath($this->path),
'value' => $this->value,
];

Expand Down
7 changes: 4 additions & 3 deletions lib/Doctrine/ODM/MongoDB/Aggregation/Stage/Search/Exists.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Doctrine\ODM\MongoDB\Aggregation\Stage\Search;

use Doctrine\ODM\MongoDB\Aggregation\Stage\Search;
use Doctrine\ODM\MongoDB\Persisters\DocumentPersister;

/**
* @internal
Expand All @@ -13,9 +14,9 @@
*/
class Exists extends AbstractSearchOperator
{
public function __construct(Search $search, private string $path = '')
public function __construct(Search $search, DocumentPersister $persister, private string $path = '')
{
parent::__construct($search);
parent::__construct($search, $persister);
}

public function getOperatorName(): string
Expand All @@ -25,6 +26,6 @@ public function getOperatorName(): string

public function getOperatorParams(): object
{
return (object) ['path' => $this->path];
return (object) ['path' => $this->prepareFieldPath($this->path)];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Doctrine\ODM\MongoDB\Aggregation\Stage\Search;

use Doctrine\ODM\MongoDB\Aggregation\Stage\Search;
use Doctrine\ODM\MongoDB\Persisters\DocumentPersister;
use GeoJson\Geometry\Geometry;
use GeoJson\Geometry\LineString;
use GeoJson\Geometry\MultiPolygon;
Expand All @@ -27,9 +28,9 @@ class GeoShape extends AbstractSearchOperator implements ScoredSearchOperator
private LineString|Point|Polygon|MultiPolygon|array|null $geometry = null;

/** @param LineString|Point|Polygon|MultiPolygon|array|null $geometry */
public function __construct(Search $search, $geometry = null, string $relation = '', string ...$path)
public function __construct(Search $search, DocumentPersister $persister, $geometry = null, string $relation = '', string ...$path)
{
parent::__construct($search);
parent::__construct($search, $persister);

$this
->geometry($geometry)
Expand Down Expand Up @@ -67,7 +68,7 @@ public function getOperatorName(): string
public function getOperatorParams(): object
{
$params = (object) [
'path' => $this->path,
'path' => $this->prepareFieldPath($this->path),
'relation' => $this->relation,
'geometry' => $this->geometry instanceof Geometry
? $this->geometry->jsonSerialize()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Doctrine\ODM\MongoDB\Aggregation\Stage\Search;

use Doctrine\ODM\MongoDB\Aggregation\Stage\Search;
use Doctrine\ODM\MongoDB\Persisters\DocumentPersister;
use GeoJson\Geometry\Geometry;
use GeoJson\Geometry\MultiPolygon;
use GeoJson\Geometry\Point;
Expand All @@ -27,9 +28,9 @@ class GeoWithin extends AbstractSearchOperator implements ScoredSearchOperator

private array|MultiPolygon|Polygon|null $geometry = null;

public function __construct(Search $search, string ...$path)
public function __construct(Search $search, DocumentPersister $persister, string ...$path)
{
parent::__construct($search);
parent::__construct($search, $persister);

$this->path(...$path);
}
Expand Down Expand Up @@ -84,7 +85,7 @@ public function getOperatorName(): string

public function getOperatorParams(): object
{
$params = (object) ['path' => $this->path];
$params = (object) ['path' => $this->prepareFieldPath($this->path)];

if ($this->box) {
$params->box = $this->box;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
namespace Doctrine\ODM\MongoDB\Aggregation\Stage\Search;

use Doctrine\ODM\MongoDB\Aggregation\Stage\Search;
use Doctrine\ODM\MongoDB\Persisters\DocumentPersister;

use function array_map;
use function array_values;

/**
Expand All @@ -19,9 +21,9 @@ class MoreLikeThis extends AbstractSearchOperator
private array $like = [];

/** @param array<string, mixed>|object $documents */
public function __construct(Search $search, ...$documents)
public function __construct(Search $search, DocumentPersister $persister, ...$documents)
{
parent::__construct($search);
parent::__construct($search, $persister);

$this->like = array_values($documents);
}
Expand All @@ -33,6 +35,8 @@ public function getOperatorName(): string

public function getOperatorParams(): object
{
return (object) ['like' => $this->like];
return (object) [
'like' => array_map($this->getDocumentPersister()->prepareQueryOrNewObj(...), $this->like),
];
}
}
7 changes: 4 additions & 3 deletions lib/Doctrine/ODM/MongoDB/Aggregation/Stage/Search/Near.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Doctrine\ODM\MongoDB\Aggregation\Stage\Search;

use Doctrine\ODM\MongoDB\Aggregation\Stage\Search;
use Doctrine\ODM\MongoDB\Persisters\DocumentPersister;
use GeoJson\Geometry\Geometry;
use GeoJson\Geometry\Point;
use MongoDB\BSON\UTCDateTime;
Expand All @@ -29,9 +30,9 @@ class Near extends AbstractSearchOperator implements ScoredSearchOperator
* @param int|float|UTCDateTime|array|Point|null $origin
* @param int|float|null $pivot
*/
public function __construct(Search $search, $origin = null, $pivot = null, string ...$path)
public function __construct(Search $search, DocumentPersister $persister, $origin = null, $pivot = null, string ...$path)
{
parent::__construct($search);
parent::__construct($search, $persister);

$this
->origin($origin)
Expand Down Expand Up @@ -74,7 +75,7 @@ public function getOperatorParams(): object
? $this->origin->jsonSerialize()
: $this->origin,
'pivot' => $this->pivot,
'path' => $this->path,
'path' => $this->prepareFieldPath($this->path),
];

return $this->appendScore($params);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function getOperatorParams(): object
{
$params = (object) [
'query' => $this->query,
'path' => $this->path,
'path' => $this->prepareFieldPath($this->path),
];

if ($this->slop !== null) {
Expand Down
Loading