Skip to content
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

Add sort operator to $search stage #2554

Merged
merged 5 commits into from
Nov 23, 2023
Merged

Conversation

alcaeus
Copy link
Member

@alcaeus alcaeus commented Oct 18, 2023

Q A
Type improvement
BC Break no
Fixed issues

Summary

This adds the missing sort operator to the builder for the $search aggregation pipeline stage.

@alcaeus alcaeus changed the title Add sort operator to stage Add sort operator to $search stage Oct 18, 2023
@alcaeus alcaeus marked this pull request as ready for review November 6, 2023 09:41
@alcaeus alcaeus requested a review from malarzm November 6, 2023 09:41
@alcaeus alcaeus self-assigned this Nov 6, 2023
@alcaeus alcaeus added this to the 2.6.0 milestone Nov 6, 2023
Copy link
Member

@malarzm malarzm left a comment

Choose a reason for hiding this comment

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

I don't really like the dynamic nature of sort's arguments. Or maybe there's already a precedence in our API that I don't recall :)

@@ -434,8 +434,10 @@ public function sortByCount(string $expression): Stage\SortByCount
*
* @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)
*
* @return Stage\Sort
Copy link
Member

Choose a reason for hiding this comment

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

What's wrong with native return type?

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 sort method for a Search stage does not return a sort stage, but handles this internally. We've had this with other stages (e.g. GeoNear::limit), but I believe I can still use a native type of Stage and refine it using the @return annotation.

/**
* @psalm-type CountType = 'lowerBound'|'total'
* @psalm-type SortMetaKeywords = 'searchScore'
Copy link
Member

Choose a reason for hiding this comment

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

This looks odd, name suggests there can be multiple keywords yet this is a string. Is this coming from MongoDB itself?

Copy link
Member Author

Choose a reason for hiding this comment

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

I used the same strategy we use in the Sort stage itself, where there are multiple meta sort options. In the $search aggregation pipeline stage, the only allowed meta sort is on the search score, hence the single keyword here. I can also change the naming to SortMetaKeywordType or something similar.

@@ -128,6 +143,27 @@ public function returnStoredSource(bool $returnStoredSource = true): static
return $this;
}

public function sort($fieldName, $order = null): static
Copy link
Member

@malarzm malarzm Nov 6, 2023

Choose a reason for hiding this comment

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

Please add PHPDoc, especially given multiple meanings of $fieldName. Or maybe there's a way to get rid of this overloading? Maybe we should encourage multiple calls to sort instead?

Copy link
Member Author

Choose a reason for hiding this comment

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

This all grew out of the query builder, which allows for precisely that:

$builder
    ->sort('foo', 1)
    ->sort('bar', -1);

// Equivalent to the above
$builder
    ->sort(['foo' => 1, 'bar' => -1]);

I've kept this strategy when creating the egg builder, and now it continues in the $search stage.

In the new aggregation pipeline builder we're creating we're instead leveraging named arguments for this:

Stage::sort(foo: 1, bar: -1)

Long story short, this is going to change once the MongoDB-supported builder is stable enough for us to use it underneath and deprecate ODM's builder :)

Copy link
Member

Choose a reason for hiding this comment

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

Stage::sort(foo: 1, bar: -1)

this looks nice :) looking forward to it!

@@ -35,6 +35,11 @@ public function returnStoredSource(bool $returnStoredSource): Search
return $this->search->returnStoredSource($returnStoredSource);
}

public function sort($fieldName, $order = null): Search
Copy link
Member

Choose a reason for hiding this comment

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

PHPDoc is missing here too

@alcaeus alcaeus requested a review from malarzm November 8, 2023 08:42
@@ -128,6 +143,27 @@ public function returnStoredSource(bool $returnStoredSource = true): static
return $this;
}

public function sort($fieldName, $order = null): static
Copy link
Member

Choose a reason for hiding this comment

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

Stage::sort(foo: 1, bar: -1)

this looks nice :) looking forward to it!

@malarzm malarzm merged commit 3cec5e3 into doctrine:2.6.x Nov 23, 2023
16 checks passed
@malarzm
Copy link
Member

malarzm commented Nov 23, 2023

Thanks @alcaeus!

@alcaeus alcaeus deleted the add-search-sort branch November 24, 2023 08:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants