Skip to content

Conversation

@przemekwitek
Copy link
Contributor

@przemekwitek przemekwitek commented Sep 25, 2025

There are cases where we would like to answer queries like:
"give me list of employees sorted by their salaries"
"give me list of countries sorted by their area"
etc.
Currently, the TOP aggregation function only supports one field which is used as both sort field and the output field.
This PR enhances the TOP function by adding an optional parameter mapToField which is the field we want as the output. The sorting will still be performed on the field, just like today.

Example:
"give me salaries of 3 youngest employees by gender"

FROM employees
| STATS youngest_employees = TOP(birth_date, 3, "desc"),
        youngest_employees_salaries = TOP(birth_date, 3, "desc", salary)
  BY    gender
| SORT gender
| KEEP gender, youngest_employees, youngest_employees_salaries
;

gender:keyword | youngest_employees:datetime                                                    | youngest_employees_salaries:integer
F              | [1964-10-18T00:00:00.000Z, 1964-06-02T00:00:00.000Z, 1963-03-21T00:00:00.000Z] | [25976, 56371, 43602]
M              | [1965-01-03T00:00:00.000Z, 1964-06-11T00:00:00.000Z, 1964-04-18T00:00:00.000Z] | [37702, 45656, 46595]
null           | [1963-06-07T00:00:00.000Z, 1963-06-01T00:00:00.000Z, 1961-05-02T00:00:00.000Z] | [48735, 45797, 61358]
;

Fixes #128630

@github-actions
Copy link
Contributor

github-actions bot commented Oct 6, 2025

ℹ️ Important: Docs version tagging

👋 Thanks for updating the docs! Just a friendly reminder that our docs are now cumulative. This means all 9.x versions are documented on the same page and published off of the main branch, instead of creating separate pages for each minor version.

We use applies_to tags to mark version-specific features and changes.

Expand for a quick overview

When to use applies_to tags:

✅ At the page level to indicate which products/deployments the content applies to (mandatory)
✅ When features change state (e.g. preview, ga) in a specific version
✅ When availability differs across deployments and environments

What NOT to do:

❌ Don't remove or replace information that applies to an older version
❌ Don't add new information that applies to a specific version without an applies_to tag
❌ Don't forget that applies_to tags can be used at the page, section, and inline level

🤔 Need help?

@przemekwitek
Copy link
Contributor Author

I'm leaving the TopTests part, as I understood you're still working on that. I'll complete the review after it.

Done, PTAL

@przemekwitek przemekwitek requested review from a team as code owners October 21, 2025 06:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

:Analytics/ES|QL AKA ESQL >enhancement Team:Analytics Meta label for analytical engine team (ESQL/Aggs/Geo) v9.3.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[ES|QL] TOP Allow ordering by a different field

3 participants