Complex sorting with Painless based on nested fields

We are developping an opensource portal for education based on DjangoCMS and Elasticsearch (

The catalog is composed of courses which each have course runs with dates, enrollment dates and languages.

The course is the main document and course runs are a nested field.

While searching through courses, I need to sort them by the status of their course runs (taking into account the applied filters, which can also filter on nested fields like enrollment dates or languages... tricky). Best is if a course has course runs open for enrollment, second best is if a course has course runs that are coming soon, etc. worse is if a course is past.

The situation changes everyday with time so we wrote a sorting script in painless. This sorting script accesses course runs via the _source field.

Everything works fine but I read today that accessing the _source field is very slow and should only be used in field scripts.

The code is opensource so if you know the intricacies of Elasticsearch, I would like to hear what you think and how it could be optimized!?

The Painless script is still in a PR as of today and visible here:

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.