# Elasticsearch sort by field

**URL:** <https://discuss.elastic.co/t/elasticsearch-sort-by-field/145793>\
**Category:** Elasticsearch\
**Created:** [August 23, 2018, 7:38pm UTC](https://discuss.elastic.co/t/elasticsearch-sort-by-field/145793 "2018-08-23T19:38:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vipra\_Tiwari](https://avatars.discourse-cdn.com/v4/letter/v/c4cdca/32.png) [@Vipra\_Tiwari](https://discuss.elastic.co/u/Vipra_Tiwari)\
**Post date:** [August 23, 2018, 7:38pm UTC](https://discuss.elastic.co/t/elasticsearch-sort-by-field/145793/1 "2018-08-23T19:38:47Z")

</div>

I need to perform a sort by “ **status**" (field in data set) operation on elastic search data where value of status is mapped.

Example : This is sample data in elastic search

{

```
"tickets": [    
    {
        "ticketId": "805",
        "accountId": "676",
        "subject": "changed the subject line, let's see how long it takes",
        "status": “New",          
    },
    {
        "ticketId": "207",
        "accountId": "324",
        "subject": "Set to Pending Vendor",
        "status": “Progress",          
    },
    {
        "ticketId": "208",
        "accountId": "714",
        "subject": "Set to Pending Maintenance",
        "status": “Process",         
    },
    {
        "ticketId": "005",
        "accountId": "376",
        "subject": "status test ticket",
        "status": “Progress",            
    },
    {
        "ticketId": "004",
        "accountId": "676",
        "subject": "status test ticket",
        "status": “Process",           
    },
    {
        "ticketId": "153",
        "accountId": "376",
        "subject": "the subject",
        "status": “Solved",          
    },
    {
        "ticketId": "379",
        "accountId": “676",
        "subject": "the subject",
        "status": “Progress",            
    },
    {
        "ticketId": "871",
        "accountId": “676",
        "subject": "the subject",
        "status": “Closed",            
    },
    {
       "ticketId": "153",
        "accountId": “876",
        "subject": "the subject",
        "status": “New",         
    },
    {
        "ticketId": "008",
        "accountId": “756",
        "subject": "Request to Enable AlertLogic ThreatManager for Internal Vulnerability Scans",
        "status": “Progress",          
    },
    {
        "ticketId": "184",
        "accountId": "236",
        "subject": "the subject",
        "status": “Solved",           
    },
    {
        "ticketId": "018",
       "accountId": "368",
        "subject": "the subject",
        "status": “Closed",        
    },
    {
        "ticketId": "708",
        "accountId": “456",
        "subject": "Ticket v2 Test",
        "status": “Solved",           
    },
    {
        "ticketId": "382",
        "accountId": “824",
        "subject": "the subject",
        "status": “Process",         
    },
    {
        "ticketId": "013",
        "accountId": “576",
        "subject": "the subject",
        "status": “Solved",            
    },
    {
        "ticketId": "002",
        "accountId": "676",
        "subject": "SDP QE Ticket",
        "status": “Progress",            
    },
    {
        "ticketId": "005",
        "accountId": "236",
        "subject": "SDP QE Ticket",
        "status": “New",            
    },
    {
       "ticketId": "298"
        "accountId": “986",
        "subject": "the subject",
        "status": “Closed",           
    }
]   

```

}

status has to be mapped in following manner

New, Progress, Process -\> Pending  
Solved -\> Resolved  
Closed -\> Removed

So, expected sequence of results from elastic search (as per mapping sequence )

{  
"tickets": [  
{  
"ticketId": "805",  
"accountId": "676",  
"subject": "changed the subject line, let's see how long it takes",  
"status": “New",  
},  
{  
"ticketId": "207",  
"accountId": "324",  
"subject": "Set to Pending Vendor",  
"status": “Progress",  
},  
{  
"ticketId": "208",  
"accountId": "714",  
"subject": "Set to Pending Maintenance",  
"status": “Process",  
},  
{  
"ticketId": "005",  
"accountId": "376",  
"subject": "status test ticket",  
"status": “Progress",  
},  
{  
"ticketId": "004",  
"accountId": "676",  
"subject": "status test ticket",  
"status": “Process",  
},  
{  
"ticketId": "379",  
"accountId": “676",  
"subject": "the subject",  
"status": “Progress",  
},  
{  
"ticketId": "153",  
"accountId": “876",  
"subject": "the subject",  
"status": “New",  
},  
{  
"ticketId": "008",  
"accountId": “756",  
"subject": "Request to Enable AlertLogic ThreatManager for Internal Vulnerability Scans",  
"status": “ Progress",  
},  
{  
"ticketId": "002",  
"accountId": "676",  
"subject": "SDP QE Ticket",  
"status": “Progress",  
},  
{  
"ticketId": "005",  
"accountId": "236",  
"subject": "SDP QE Ticket",  
"status": “New",  
},  
{  
"ticketId": "382",  
"accountId": “824",  
"subject": "the subject",  
"status": “Process",  
},  
{  
"ticketId": "871",  
"accountId": “676",  
"subject": "the subject",  
"status": “Closed",  
},  
{  
"ticketId": "018",  
"accountId": "368",  
"subject": "the subject",  
"status": “Closed",  
},  
{  
"ticketId": "298",  
"accountId": “986",  
"subject": "the subject",  
"status": “Closed",  
},  
{  
"ticketId": "184",  
"accountId": "236",  
"subject": "the subject",  
"status": “Solved",  
},  
{  
"ticketId": "708",  
"accountId": “456",  
"subject": "Ticket v2 Test",  
"status": “Solved",  
},  
{  
"ticketId": "013",  
"accountId": “576",  
"subject": "the subject",  
"status": “Solved",  
},  
{  
"ticketId": "153",  
"accountId": "376",  
"subject": "the subject",  
"status": “Solved",  
},  
]  
}

How this functionality can be implemented using elastic search query. Is it possible to perform by using function\_score ?

---

<div class="post-metadata">

**Author:** ![mayya](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mayya/32/83147_2.png) [@mayya](https://discuss.elastic.co/u/mayya)\
**Post date:** [August 31, 2018, 9:09pm UTC](https://discuss.elastic.co/t/elasticsearch-sort-by-field/145793/2 "2018-08-31T21:09:52Z")

</div>

Is you intention to return new values "pending", "resolved", "removed" in the search result? Or do you want to modify documents to have these new values?  
If it is for search, you can take advantage of [script fields](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-script-fields.html).

An example, how script fields can be used for your use-case:

```auto
GET /_search
{
  "query" : {
      "match_all": {}
  },
  "script_fields" : {
      "updated-status" : {
          "script" : {
              "lang": "painless",
              "source": "if (doc['status'].value == 'New') {'Pending';} else {'Resolved';}"
          }
      }
  }
}

```

---

<div class="post-metadata">

**Author:** ![Vipra\_Tiwari](https://avatars.discourse-cdn.com/v4/letter/v/c4cdca/32.png) [@Vipra\_Tiwari](https://discuss.elastic.co/u/Vipra_Tiwari)\
**Post date:** [September 7, 2018, 11:49am UTC](https://discuss.elastic.co/t/elasticsearch-sort-by-field/145793/3 "2018-09-07T11:49:59Z")

</div>

Thanks for help

---

<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 5, 2018, 11:50am UTC](https://discuss.elastic.co/t/elasticsearch-sort-by-field/145793/4 "2018-10-05T11:50:13Z")

</div>

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