# Order the Multiple exact values query by the order of the query list

**URL:** <https://discuss.elastic.co/t/order-the-multiple-exact-values-query-by-the-order-of-the-query-list/46125>\
**Category:** Elasticsearch\
**Created:** [April 2, 2016, 9:33am UTC](https://discuss.elastic.co/t/order-the-multiple-exact-values-query-by-the-order-of-the-query-list/46125 "2016-04-02T09:33:25Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Garito](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/garito/32/8854_2.png) [@Garito](https://discuss.elastic.co/u/Garito)\
**Post date:** [April 2, 2016, 9:33am UTC](https://discuss.elastic.co/t/order-the-multiple-exact-values-query-by-the-order-of-the-query-list/46125/1 "2016-04-02T09:33:25Z")

</div>

It is possible to sort the list of documents in the list order?  
So if I use this query:

```
{
  "query": {
    "bool": {
      "must": [
        {
          "match_phrase": {
            "path.raw": "/"
          }
        },
        {
          "terms": {
            "slug": [
              "users",
              "system",
              "projects"
            ]
          }
        }
      ]
    }
  }
}

```

I expect to retrieve the document with slug "users" 1st, "system" 2nd and "projects" 3

How can achieve this?

Thanks

---

<div class="post-metadata">

**Author:** ![Garito](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/garito/32/8854_2.png) [@Garito](https://discuss.elastic.co/u/Garito)\
**Post date:** [April 3, 2016, 10:11pm UTC](https://discuss.elastic.co/t/order-the-multiple-exact-values-query-by-the-order-of-the-query-list/46125/2 "2016-04-03T22:11:36Z")

</div>

Any other way besides this one:

```auto
{
  "query": {
    "bool": {
      "must": [
        {
          "match_phrase": {
            "path.raw": "/"
          }
        },
        {
          "terms": {
            "slug": [
              "system",
              "projects",
              "users"
            ]
          }
        }
      ]
    }
  },
  "sort": [
    {
      "_script": {
        "script": "list.indexOf(doc['slug'].value)",
        "type": "number",
        "order": "asc",
        "lang": "groovy",
        "params": {
          "list": ["system", "projects", "users"]
        }
      }
    }
  ]
}

```

---

<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:** [April 4, 2016, 9:25am UTC](https://discuss.elastic.co/t/order-the-multiple-exact-values-query-by-the-order-of-the-query-list/46125/3 "2016-04-04T09:25:58Z")

</div>

Your example would work. Another option that would have the benefit to not use scripts (that often slow down queries if many documents match) would be to do something like this:

```auto
{
  "query": {
    "bool": {
      "must": [
        {
          "constant_score": {
            "filter": {
              "match_phrase": {
                "path.raw": "/"
              }
            }
          }
        }
      ],
      "should": [
        {
          "constant_score": {
            "filter": {
              "term": {
                "slug": "system"
              }
            },
            "boost": 3
          }
        },
        {
          "constant_score": {
            "filter": {
              "term": {
                "slug": "projects"
              }
            },
            "boost": 2
          }
        },
        {
          "constant_score": {
            "filter": {
              "term": {
                "slug": "users"
              }
            },
            "boost": 1
          }
        }
      ]
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![Garito](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/garito/32/8854_2.png) [@Garito](https://discuss.elastic.co/u/Garito)\
**Post date:** [April 4, 2016, 10:30am UTC](https://discuss.elastic.co/t/order-the-multiple-exact-values-query-by-the-order-of-the-query-list/46125/4 "2016-04-04T10:30:15Z")

</div>

I see, thanks for your help

The problem with the 1st option is the performance only if the list is too long, isn't it?

I know there will not being more than 30 documents or something like that

Is there any other options? I would like a succint one if possible giving the fact that, in my opinion, that the multiple exact values query don't give back the response in order is a bug or at least is has no sense

---

<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:** [April 4, 2016, 12:21pm UTC](https://discuss.elastic.co/t/order-the-multiple-exact-values-query-by-the-order-of-the-query-list/46125/5 "2016-04-04T12:21:19Z")

</div>

There will indeed be performance issues if the list is long. But also if many documents match since the script will need to be called on every matching document, and Groovy scripts typically have some constant overhead. But if you know for sure there will never be more than 30 matching documents, this will be totally fine.

---

<div class="post-metadata">

**Author:** ![Garito](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/garito/32/8854_2.png) [@Garito](https://discuss.elastic.co/u/Garito)\
**Post date:** [April 4, 2016, 1:40pm UTC](https://discuss.elastic.co/t/order-the-multiple-exact-values-query-by-the-order-of-the-query-list/46125/6 "2016-04-04T13:40:08Z")

</div>

No, I think I will not have this issue since I'm asking for documents that can't share path and slug (so if the list is 3 elements long the response will be 3 documents long)

What about the issue that being list and array were the order matters the query is returning a non ordered response?  
What sense makes this? Remember that the query is called Multiple exact values (exact)

---

<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:** [April 4, 2016, 4:29pm UTC](https://discuss.elastic.co/t/order-the-multiple-exact-values-query-by-the-order-of-the-query-list/46125/7 "2016-04-04T16:29:26Z")

</div>

> [@Garito](#):
>
> Is there any other options? I would like a succint one if possible giving the fact that, in my opinion, that the multiple exact values query don't give back the response in order is a bug or at least is has no sense

This is no bug to me: the order in which results are returned is defined by the value of the `sort` parameter (which defaults to sorting by descending score). And the terms query gives a score equal to 1 to all matching documents, so documents will come in an order which is not specified. When dealing with multiple terms, the way to go if you want to give different weights to your terms is to use a `bool` query and then combine the scores using boosts for simple use-cases or `function_score` for more complicated use-cases.

---

<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, 11:02pm UTC](https://discuss.elastic.co/t/order-the-multiple-exact-values-query-by-the-order-of-the-query-list/46125/8 "2017-07-05T23:02:33Z")

</div>


