Simple sort in painless (for MediaWiki)

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.

Thanks

It is hard to evaluate this without seeing what nsSort is, but syntactically it looks ok. However, it looks like you are building a string sort key, and I wonder why you wouldn't have separate sort specifications for each field you want to sort on. With your current script, you will always load title.keyword, even if there are no documents with the same value for namespace. Instead, you can add a secondary sort on the title.keyword field (which does not need to be a script).

1 Like

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