How to create custom sort in Open Search on a single parameter

21 Views Asked by At

I use Open Search in my project and I need to sort the values based on a parameter with a custom setup. I need the values to be sorted as follows:

  1. inProgress
  2. approvalRequired
  3. dataMissing
  4. completed

As you can see I can't sort them alphabetically. Based on the documentations, I thought the sort values should look like:

[
    "status": {
        "order": [
            { "inProgress": "asc" },
            { "approvalRequired": "asc" },
            { "dataMissing": "asc" },
            { "completed": "asc" }
        ]
    },
    // I need this one too, but it's not the point of the question
    "lastName": {
        "order": "desc"
    }

]

or this:

[
    {
      "_script": {
        "type": "number",
        "script": {
          "lang": "painless",
          "source": "def statusOrder = ['inProgress': 1, 'approvalRequired': 2, 'dataMissing': 3, 'completed': 4]; return statusOrder.containsKey(doc['status'].value) ? statusOrder[doc['status'].value] : 5;"
        },
        "order": "asc"
      }
    },
    {
      "lastName": {
        "order": "desc"
      }
    }
]

But none of them worked. I can't find in the documentations how to define a custom sort, not just "desc" or "asc".

0

There are 0 best solutions below