Elasticsearch-dsl-py: Sorting using an installed painless script

Created on 15 Feb 2018  路  1Comment  路  Source: elastic/elasticsearch-dsl-py

Hi,

I'm having a bit of trouble figuring out how to sort using an installed painless script. I installed the script using:
self.connection.put_script( lang="painless", id="partialNumericSort", body={"script": self.script_partial_numeric_sort} )
where self.script_partial_numeric_sort is a property containing the Painless code to be used for sorting.

Putting the script seems to work fine, no compile errors while creating the index.

Though, now I am not sure how to format the self.search().sort(_sort_params_here).execute().
where self.search() returns a elasticsearch_dsl Search object. to utilize this sort script...

In pure JSON format, it would look like:

{
    "query": _my_query_goes_here_,
    "sort": [{
        "_script": {
            "lang": "painless",
            "order": "asc",
            "params": {
                "delim": "._",
                "fieldName": "seq",
                "prefix": "sq"
            },
            "script_id": "partialNumericSort",
            "type": "text"
    }]
}

Any help would greatly be appreciated.

Most helpful comment

If you have complex search params like this you can always just pass them to the sort method:

s = s.sort({
        "_script": {
            "lang": "painless",
            "order": "asc",
            "params": {
                "delim": "._",
                "fieldName": "seq",
                "prefix": "sq"
            },
            "script_id": "partialNumericSort",
            "type": "text"
    })

Hope this helps!

>All comments

If you have complex search params like this you can always just pass them to the sort method:

s = s.sort({
        "_script": {
            "lang": "painless",
            "order": "asc",
            "params": {
                "delim": "._",
                "fieldName": "seq",
                "prefix": "sq"
            },
            "script_id": "partialNumericSort",
            "type": "text"
    })

Hope this helps!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rokcarl picture rokcarl  路  4Comments

vmogilev picture vmogilev  路  4Comments

ypkkhatri picture ypkkhatri  路  4Comments

quasiben picture quasiben  路  4Comments

berinhard picture berinhard  路  3Comments