I'm using ElasticSearch for MediaWiki and have the following script which seems to be working (this is in PHP, as the interface to ElasticSearch for MediaWiki is in PHP:
// Contsruct the script array $this->sort = [ '_script' => [ 'type' => 'string', 'script' => [ 'lang' => 'painless', 'source' => "def s = params._source.sortKey; if(s) return s; else return params.nsSort[doc['namespace'].value.toString()] + '2' + doc['title.keyword'].value", 'params' => [ 'nsSort' => DcsNamespaces::$sortedNS ] ] ] ];
I say 'seems to work' because I'm not seeing any script compilation errors in the Elasticsearch log. But I just converted that from groovy, and the search system itself isn't returning values yet. (That's most likely a separate problem I believe.)
Anyway, I'm just looking for confirmation that this script is correct, and that there aren't improvements that could be made or pitfalls that I'll encounter momentarily.