Skip to content

CSHARP-5828: Add Rerank stage builder#1936

Merged
adelinowona merged 8 commits intomongodb:mainfrom
adelinowona:csharp5828
Apr 17, 2026
Merged

CSHARP-5828: Add Rerank stage builder#1936
adelinowona merged 8 commits intomongodb:mainfrom
adelinowona:csharp5828

Conversation

@adelinowona
Copy link
Copy Markdown
Contributor

No description provided.

@adelinowona adelinowona added the feature Adds new user-facing functionality. label Apr 2, 2026
@adelinowona adelinowona requested a review from a team as a code owner April 2, 2026 15:59
@adelinowona adelinowona requested review from Copilot, kyra-rk and sanych-sun and removed request for kyra-rk April 2, 2026 15:59
Copy link
Copy Markdown
Contributor

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

Adds first-class support for building/appending a MongoDB aggregation $rerank stage across the stage builder, pipeline builder extensions, and fluent aggregate API.

Changes:

  • Added $rerank stage builders to PipelineStageDefinitionBuilder.
  • Added pipeline extension methods to append $rerank stages via PipelineDefinitionBuilder.
  • Added fluent aggregate API surface for $rerank plus new unit tests covering rendering and parameter validation.

Reviewed changes

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

Show a summary per file
File Description
tests/MongoDB.Driver.Tests/PipelineStageDefinitionBuilderTests.cs Adds argument-validation tests for the new stage builder APIs.
tests/MongoDB.Driver.Tests/PipelineDefinitionBuilderTests.cs Adds rendering tests for $rerank pipeline extension methods and updates test model.
src/MongoDB.Driver/PipelineStageDefinitionBuilder.cs Introduces $rerank stage construction and rendering logic.
src/MongoDB.Driver/PipelineDefinitionBuilder.cs Adds pipeline extension methods to append $rerank stages.
src/MongoDB.Driver/IAggregateFluentExtensions.cs Adds expression-based fluent extension overload for $rerank.
src/MongoDB.Driver/IAggregateFluent.cs Extends the fluent aggregate interface with a $rerank stage method.
src/MongoDB.Driver/AggregateFluentBase.cs Adds base virtual method stub for $rerank.
src/MongoDB.Driver/AggregateFluent.cs Implements $rerank by delegating to the underlying pipeline.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/MongoDB.Driver/PipelineStageDefinitionBuilder.cs
Comment thread src/MongoDB.Driver/PipelineStageDefinitionBuilder.cs
Comment thread src/MongoDB.Driver/PipelineStageDefinitionBuilder.cs
@adelinowona adelinowona force-pushed the csharp5828 branch 2 times, most recently from fc62d55 to 07167c7 Compare April 7, 2026 23:58
Comment thread src/MongoDB.Driver/PipelineDefinitionBuilder.cs
public static PipelineDefinition<TInput, TOutput> Rerank<TInput, TOutput>(
this PipelineDefinition<TInput, TOutput> pipeline,
RerankQuery query,
IEnumerable<FieldDefinition<TOutput>> paths,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Let's either use IEnumerable parameter or params in both cases: here and on the line 1126.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The two multi-path overloads serve different use cases: the params Expression[] one is for inline type-safe expressions (x => x.Title, x => x.Plot), and the IEnumerable<FieldDefinition> one is for pre-built or dynamic collections of string-based paths. Using params for FieldDefinition wouldn't add much since there's no type inference benefit, and using IEnumerable<Expression> would lose the params convenience. I think the inconsistency is justified by the different use cases here.
Thoughts?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I see this as a different way to provide the same parameters. Having one as an array and another as a params looks confusing a little. About different use cases: both expressions and FieldDefinitions could be used as fixed list of values or dynamic collections I believe. I think I would prefer consistency between overloads if it's possible. @BorisDog @papafe @ajcvickers WDYT?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Agree with @adelinowona.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Changing my mind, as I already commented. I don't agree with, "Having one as an array and another as a params looks confusing a little. " but I do like keeping the parameter order the same between the overloads.

Comment thread src/MongoDB.Driver/PipelineDefinitionBuilder.cs
Comment thread src/MongoDB.Driver/IAggregateFluentExtensions.cs
Comment thread src/MongoDB.Driver/RerankQuery.cs Outdated
Comment thread src/MongoDB.Driver/RerankQuery.cs Outdated
Comment thread tests/MongoDB.Driver.Tests/PipelineStageDefinitionBuilderTests.cs Outdated
@adelinowona adelinowona requested a review from sanych-sun April 9, 2026 15:14
Copy link
Copy Markdown
Member

@sanych-sun sanych-sun left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Copy Markdown
Contributor

@BorisDog BorisDog left a comment

Choose a reason for hiding this comment

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

Minor comments

string model)
=> Rerank(
query,
new ExpressionFieldDefinition<TInput>(path),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

minor: probably need a null check for path here, for consistent exception with other overload.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit: You could keep the expression body and have

  return Rerank(
                query,
                new ExpressionFieldDefinition<TInput>( Ensure.IsNotNull(path, nameof(path)))

Comment thread src/MongoDB.Driver/RerankQuery.cs Outdated
Comment thread src/MongoDB.Driver/RerankQuery.cs Outdated
Comment thread src/MongoDB.Driver/PipelineStageDefinitionBuilder.cs
Comment thread src/MongoDB.Driver/RerankQuery.cs Outdated
Comment thread tests/MongoDB.Driver.Tests/PipelineStageDefinitionBuilderTests.cs
RerankQuery query,
int numDocsToRerank,
string model,
params Expression<Func<TOutput, TField>>[] paths)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Yeah as you mentioned the inconsistency in path and paths arguments position is not ideal. But no strong opinion.
Let's see what @ajcvickers thinks.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I didn't notice the change in argument ordering before. I prefer the ordering where the paths are immediately after the query--this feels like what we commonly do with paths, and feels natural here like Adelin said. So, given that the params doesn't add a lot in modern C#, I think I would drop it and keep the order the same for overloads. So, yeah, I'm changing my mind. :-) But I don't feel strongly.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I agree with Arthur here (and also don't feel strongly).

Copy link
Copy Markdown
Contributor

@ajcvickers ajcvickers left a comment

Choose a reason for hiding this comment

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

LGTM, except I marginally prefer to drop the params array and keep the overloads having the same parameter order.

Copy link
Copy Markdown
Contributor

@BorisDog BorisDog left a comment

Choose a reason for hiding this comment

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

LGTM + 2 minor suggestions.

string model)
=> Rerank(
query,
new ExpressionFieldDefinition<TInput>(path),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit: You could keep the expression body and have

  return Rerank(
                query,
                new ExpressionFieldDefinition<TInput>( Ensure.IsNotNull(path, nameof(path)))

@adelinowona adelinowona merged commit 10f345d into mongodb:main Apr 17, 2026
34 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature Adds new user-facing functionality.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants