-
Type: Improvement
-
Resolution: Fixed
-
Priority: Major - P3
-
Affects Version/s: None
-
Component/s: Aggregation Framework, Querying
-
None
-
Minor Change
-
Query 2019-12-16, Query 2019-12-30
When a $sort stage is at the beginning of the pipeline, or close to the beginning, the aggregation subsystem attempts to execute it using the underlying PlanStage query execution tree. We are trying to push increasingly more execution into the PlanStage layer in order to move towards having a single, unified execution engine which will be easier to extend and maintain.
There is a special case where we don't push down $sort if it has "textScore" or "randVal" $meta expressions: see https://github.com/mongodb/mongo/blob/8ccaacce937ae51deef27c41e977aca0511fb338/src/mongo/db/pipeline/pipeline_d.cpp#L102-L120.
For "textScore", this restriction stems from the fact that the find and agg systems have different validity rules for $meta sort by text score. The agg subsystem permits $meta:textScore with no matching $meta projection:
> db.c.drop() > db.c.createIndex({f: "text"}) { "createdCollectionAutomatically" : true, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "commitQuorum" : 1, "ok" : 1 } > db.c.aggregate([{$match: {$text: {$search: "foo"}}}, {$sort: {f: {$meta: "textScore"}}}]) // Success!
The find subsystem, on the other hand, requires a matching "textScore" $meta projection:
> db.c.find({$text: {$search: "foo"}}, {}).sort({f: {$meta: "textScore"}}) Error: error: { "ok" : 0, "errmsg" : "must have $meta projection for all $meta sort keys", "code" : 2, "codeName" : "BadValue" } > db.c.find({$text: {$search: "foo"}}, {f: {$meta: "textScore"}}).sort({f: {$meta: "textScore"}}) // Success!
We should eliminate this restriction, which would allow us to always push down "textScore" $meta sort.
The situation for "randVal" needs some investigation, but note SERVER-43666. The ability to sort by "randVal" has existed internally for some time to support $sample, though I don't think we intend to support it as available to applications.
- is duplicated by
-
SERVER-40253 Pipelines with text search queries should be eligible for being optimized away
- Closed
- is related to
-
SERVER-7568 Aggregation framework favors non-blocking sorts
- Closed