# Maintain order of results based on search criteria

**URL:** https://discuss.elastic.co/t/maintain-order-of-results-based-on-search-criteria/58332
**Category:** Elasticsearch
**Created:** [August 18, 2016, 9:16am UTC](https://discuss.elastic.co/t/maintain-order-of-results-based-on-search-criteria/58332 "2016-08-18T09:16:34Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![hitesh-inbetween](https://avatars.discourse-cdn.com/v4/letter/h/51bf81/32.png) [@hitesh-inbetween](https://discuss.elastic.co/u/hitesh-inbetween)
#### Post date: [August 18, 2016, 9:16am UTC](https://discuss.elastic.co/t/maintain-order-of-results-based-on-search-criteria/58332/1 "2016-08-18T09:16:34Z")

</div>

Hello elasticsearch pioneers,

I would like the results to be in the same sequence as the query. To elaborate more about this, consider the following example. :

I have documents with ID field, which look somewhat like this :

```
{
  {
    "ID": 102,
    "Name": "Mark"
  },
  {
    "ID": 104,
    "Name": "Pete"
  },
  {
    "ID": 101,
    "Name": "Su"
  },
  {
    "ID": 107,
    "Name": "Kate"
  },
  {
    "ID": 106,
    "Name": "Roger"
  }
}

```

And my query is:

```
{
  "query": {
    "bool": {
      "should": [
        {
          "match": 
          {
            "ID": "101"
          }
        },
        {
          "match": 
          {
            "ID": "104"
          }
        },
        {
          "match": 
          {
            "ID": "107"
          }
        },
        {
          "match": 
          {
            "ID": "102"
          }
        },
        {
          "match": 
          {
            "ID": "106"
          }
        }
      ]
    }
  }
}

```

Now I'm expecting the results to be in the same order as the search criteria. i.e :

```
{
    "ID": 101,
    "Name": "Su"
},
{
    "ID": 104,
    "Name": "Pete"
},
{
    "ID": 107,
    "Name": "Kate"
},
{
    "ID": 102,
    "Name": "Mark"
},
{
    "ID": 106,
    "Name": "Roger"
}

```

Been trying to figure out a way to do this. Is it even possible? Any help appreciated.

---

<div class="post-metadata">

### Author: ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)
#### Post date: [August 19, 2016, 10:13am UTC](https://discuss.elastic.co/t/maintain-order-of-results-based-on-search-criteria/58332/2 "2016-08-19T10:13:09Z")

</div>

I think the most efficient way to do it would be to run one query for each id in a multi-search call [https://www.elastic.co/guide/en/elasticsearch/reference/current/search-multi-search.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-multi-search.html)

---

<div class="post-metadata">

### Author: ![hitesh-inbetween](https://avatars.discourse-cdn.com/v4/letter/h/51bf81/32.png) [@hitesh-inbetween](https://discuss.elastic.co/u/hitesh-inbetween)
#### Post date: [August 20, 2016, 7:57am UTC](https://discuss.elastic.co/t/maintain-order-of-results-based-on-search-criteria/58332/3 "2016-08-20T07:57:58Z")

</div>

Hey thanks...I need to try that still...but yes it's a simple and splendid solution...  
Thank you..  
I now just need to figure out a way to fire multi-search using elasticsearch-php...  
Thanks once again.!!

---

<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: [July 5, 2017, 10:26pm UTC](https://discuss.elastic.co/t/maintain-order-of-results-based-on-search-criteria/58332/4 "2017-07-05T22:26:28Z")

</div>


