# Simple sort in painless (for MediaWiki)

**URL:** <https://discuss.elastic.co/t/simple-sort-in-painless-for-mediawiki/149569>\
**Category:** Elasticsearch\
**Created:** [September 22, 2018, 6:25pm UTC](https://discuss.elastic.co/t/simple-sort-in-painless-for-mediawiki/149569 "2018-09-22T18:25:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![freephile](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/freephile/32/35777_2.png) [@freephile](https://discuss.elastic.co/u/freephile)\
**Post date:** [September 22, 2018, 6:25pm UTC](https://discuss.elastic.co/t/simple-sort-in-painless-for-mediawiki/149569/1 "2018-09-22T18:25:13Z")

</div>

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:

```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

---

<div class="post-metadata">

**Author:** ![rjernst](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rjernst/32/6363_2.png) [@rjernst](https://discuss.elastic.co/u/rjernst)\
**Post date:** [September 26, 2018, 10:51pm UTC](https://discuss.elastic.co/t/simple-sort-in-painless-for-mediawiki/149569/2 "2018-09-26T22:51:34Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [October 24, 2018, 10:51pm UTC](https://discuss.elastic.co/t/simple-sort-in-painless-for-mediawiki/149569/3 "2018-10-24T22:51:45Z")

</div>

Closed on October 24, 2018, 10:51pm UTC

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