Skip to content

ES|QL: Optimize TopNOperator in final plans #131221

Open
@ioanatia

Description

@ioanatia

Description

For queries that are using a SORT we have a TopNOperator in the final plan which is executed on the coordinator.
The TopNOperator uses a PriorityQueue to keep the rows it needs to output.
For each input page and for each row, we check whether the current row can be added to this PriorityQueue (if it's part of the top N).

The pages that are fed into this TopNOperator can come directly from the data nodes and can already be sorted.
In this case we do not need to process the whole input page.
We just need to process the rows until we can detect that the current row was not added to priority queue.
This means that the rest of the input page does not contain any candidate rows that can make it in the Top N.
In this case we can stop processing the current input page and move to the next.

This optimization is useful in the context of search, where we sort by score FROM my-index METADATA _score | WHERE my_field:"foo" | SORT _score DESC.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions