Skip to content

Allow to paginate $search aggregation with searchBefore and searchAfter markers #2764

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: 2.12.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions lib/Doctrine/ODM/MongoDB/Aggregation/Stage/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
* maxNumPassages?: int,
* },
* returnStoredSource?: bool,
* searchBefore?: string,
* searchAfter?: string,
* sort?: object,
* autocomplete?: object,
* compound?: object,
Expand Down Expand Up @@ -61,6 +63,8 @@ class Search extends Stage implements SupportsAllSearchOperators
private ?object $count = null;
private ?object $highlight = null;
private ?bool $returnStoredSource = null;
private ?string $searchBefore = null;
private ?string $searchAfter = null;
private ?SearchOperator $operator = null;

/** @var array<string, -1|1|SortMeta> */
Expand Down Expand Up @@ -92,6 +96,14 @@ public function getExpression(): array
$params->returnStoredSource = $this->returnStoredSource;
}

if ($this->searchBefore) {
$params->searchBefore = $this->searchBefore;
}

if ($this->searchAfter) {
$params->searchAfter = $this->searchAfter;
}

if ($this->sort) {
$params->sort = (object) $this->sort;
}
Expand Down Expand Up @@ -145,6 +157,20 @@ public function returnStoredSource(bool $returnStoredSource = true): static
return $this;
}

public function searchBefore(string $searchBefore): static
{
$this->searchBefore = $searchBefore;

return $this;
}

public function searchAfter(string $searchAfter): static
{
$this->searchAfter = $searchAfter;

return $this;
}

/**
* @param array<string, int|string>|string $fieldName Field name or array of field/order pairs
* @param int|string $order Field order (if one field is specified)
Expand Down
112 changes: 112 additions & 0 deletions tests/Doctrine/ODM/MongoDB/Tests/Aggregation/Stage/SearchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1313,4 +1313,116 @@
$searchStage->getExpression(),
);
}

#[DataProvider('provideAutocompleteBuilders')]

Check failure on line 1317 in tests/Doctrine/ODM/MongoDB/Tests/Aggregation/Stage/SearchTest.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.2)

Method Doctrine\ODM\MongoDB\Tests\Aggregation\Stage\SearchTest::testSearchOperatorsWithSearchBefore() has parameter $expectedOperator with no value type specified in iterable type array.
#[DataProvider('provideCompoundBuilders')]
#[DataProvider('provideEmbeddedDocumentBuilders')]
#[DataProvider('provideEmbeddedDocumentCompoundBuilders')]
#[DataProvider('provideEqualsBuilders')]
#[DataProvider('provideExistsBuilders')]
#[DataProvider('provideGeoShapeBuilders')]
#[DataProvider('provideGeoWithinBuilders')]
#[DataProvider('provideMoreLikeThisBuilders')]
#[DataProvider('provideNearBuilders')]
#[DataProvider('providePhraseBuilders')]
#[DataProvider('provideQueryStringBuilders')]
#[DataProvider('provideRangeBuilders')]
#[DataProvider('provideRegexBuilders')]
#[DataProvider('provideTextBuilders')]
#[DataProvider('provideWildcardBuilders')]
public function testSearchOperatorsWithSearchBefore(array $expectedOperator, Closure $createOperator): void
Copy link
Member

Choose a reason for hiding this comment

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

I think a single case is sufficient to test this feature. As it is, you're simply repeating the same test many times over for no purpose.

Copy link
Author

Choose a reason for hiding this comment

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

Thanks for the advice. I leave only one data provider on each method

{
$baseExpected = [
'index' => 'my_search_index',
'highlight' => (object) [
'path' => 'content',
'maxCharsToExamine' => 2,
'maxNumPassages' => 3,
],
'count' => (object) [
'type' => 'lowerBound',
'threshold' => 1000,
],
'returnStoredSource' => true,
'searchBefore' => 'marker',
];

$searchStage = new Search($this->getTestAggregationBuilder());
$searchStage
->index('my_search_index')
->searchBefore('marker');

$result = $createOperator($searchStage);

self::logicalOr(
new IsInstanceOf(AbstractSearchOperator::class),
new IsInstanceOf(Search::class),
);

$result
->highlight('content', 2, 3)
->countDocuments('lowerBound', 1000)
->returnStoredSource();

self::assertEquals(
['$search' => (object) array_merge($baseExpected, $expectedOperator)],
$searchStage->getExpression(),
);
}

#[DataProvider('provideAutocompleteBuilders')]

Check failure on line 1373 in tests/Doctrine/ODM/MongoDB/Tests/Aggregation/Stage/SearchTest.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.2)

Method Doctrine\ODM\MongoDB\Tests\Aggregation\Stage\SearchTest::testSearchOperatorsWithSearchAfter() has parameter $expectedOperator with no value type specified in iterable type array.
#[DataProvider('provideCompoundBuilders')]
#[DataProvider('provideEmbeddedDocumentBuilders')]
#[DataProvider('provideEmbeddedDocumentCompoundBuilders')]
#[DataProvider('provideEqualsBuilders')]
#[DataProvider('provideExistsBuilders')]
#[DataProvider('provideGeoShapeBuilders')]
#[DataProvider('provideGeoWithinBuilders')]
#[DataProvider('provideMoreLikeThisBuilders')]
#[DataProvider('provideNearBuilders')]
#[DataProvider('providePhraseBuilders')]
#[DataProvider('provideQueryStringBuilders')]
#[DataProvider('provideRangeBuilders')]
#[DataProvider('provideRegexBuilders')]
#[DataProvider('provideTextBuilders')]
#[DataProvider('provideWildcardBuilders')]
public function testSearchOperatorsWithSearchAfter(array $expectedOperator, Closure $createOperator): void
{
$baseExpected = [
'index' => 'my_search_index',
'highlight' => (object) [
'path' => 'content',
'maxCharsToExamine' => 2,
'maxNumPassages' => 3,
],
'count' => (object) [
'type' => 'lowerBound',
'threshold' => 1000,
],
'returnStoredSource' => true,
'searchAfter' => 'marker',
];

$searchStage = new Search($this->getTestAggregationBuilder());
$searchStage
->index('my_search_index')
->searchAfter('marker');

$result = $createOperator($searchStage);

self::logicalOr(
new IsInstanceOf(AbstractSearchOperator::class),
new IsInstanceOf(Search::class),
);

$result
->highlight('content', 2, 3)
->countDocuments('lowerBound', 1000)
->returnStoredSource();

self::assertEquals(
['$search' => (object) array_merge($baseExpected, $expectedOperator)],
$searchStage->getExpression(),
);
}
}
Loading