pythonelasticsearch

How to one value come first during sort on that field Elastic Search


I use python and I sort data in alphabetical order,but only one data in there will not be sorted like that but put it at the top

I dont have solution for problem example: I want to value = "Other Countries" comes first, any other data have to sort by alphabet

This is my query:

{
    "query": {
        "bool": {
            "must": [
                {
                    "match": {
                        "type": "COUNTRY"
                    }
                },
                {
                    "wildcard": {
                        "value": "**"
                    }
                },
                {
                    "bool": {
                        "should": [
                            {
                                "bool": {
                                    "must": []
                                }
                            }
                        ]
                    }
                }
            ]
        }
    },
    "sort": [
        {
            "country": {
                "order": "asc",
                "missing": "_first"
            }
        }
    ],
    "size": 10,
    "track_total_hits": true
}

Solution

  • This is my solution and it work

        "sort": [
        {
            "_script": {
                "type": "number",
                "script": {
                    "lang": "painless",
                    "source": "if (params._source.country_code == 'OTHER_COUNTRY') { return 0; } else { return 1; }",
                    "params": {
                        "multiplier": "OTHER_COUNTRY"
                    }
                },
                "order": "asc"
            }
        },
        {
            "country": {
                "order": "asc",
                "missing": "_first"
            }
        }
    ],