Elasticsearch sorting buckets of dates with script Elasticsearch sorting buckets of dates with script elasticsearch elasticsearch

Elasticsearch sorting buckets of dates with script


I believe you are close, but have to return some value instead of 1 for the "future" dates.

Try substituting 1 for 1.0/doc['start_date'].value so it is inversely proportional to date (larger date values are further in the future).

Full example as above:

sort: [  {    _script: {      type: 'number',      script: {        lang: 'painless',        inline: "if (doc['item_date'].value > params.current_time) {return 1.0/doc['start_date'].value;} else {return 0}",        params: {          current_time,        },      },      order: 'desc',    },  },  { item_date: { order: 'desc' } },]