# Mixing/Alternating Query Results

**URL:** https://discuss.elastic.co/t/mixing-alternating-query-results/80748
**Category:** Elasticsearch
**Created:** [March 30, 2017, 9:00pm UTC](https://discuss.elastic.co/t/mixing-alternating-query-results/80748 "2017-03-30T21:00:10Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![David\_Klebanoff](https://avatars.discourse-cdn.com/v4/letter/d/ba8739/32.png) [@David\_Klebanoff](https://discuss.elastic.co/u/David_Klebanoff)
#### Post date: [March 30, 2017, 9:00pm UTC](https://discuss.elastic.co/t/mixing-alternating-query-results/80748/1 "2017-03-30T21:00:10Z")

</div>

Suppose you had documents that fall into distinct categories: A, B, C, D, E, and F. Is it possible to make a query, such that the results are sorted in such a way that they are mixed/alternated evenly across these categories. For example, suppose your query had a boolean filter selecting a subset of the categories:

```
{
    "size" : 10,
    "query" {
        "bool" : {
            "must" : [
              { "term" : {"title" : "foo"}}
            ]
            "should" : [
              { "term" : {"category" : "A"}},
              { "term" : {"category" : "B"}},
              { "term" : {"category" : "C"}}
            ]
        }
    }
}

```

Is it possible to generate results that look like:

A,A,B,B,C,C,A,A,B,B

---

<div class="post-metadata">

### Author: ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)
#### Post date: [March 30, 2017, 11:53pm UTC](https://discuss.elastic.co/t/mixing-alternating-query-results/80748/2 "2017-03-30T23:53:04Z")

</div>

You can use [Multi Search](https://www.elastic.co/guide/en/elasticsearch/reference/5.3/search-multi-search.html) to perform 3 separate searches (one in each category) and then interlace the results on your application side before displaying them to the user.

---

<div class="post-metadata">

### Author: ![David\_Klebanoff](https://avatars.discourse-cdn.com/v4/letter/d/ba8739/32.png) [@David\_Klebanoff](https://discuss.elastic.co/u/David_Klebanoff)
#### Post date: [March 31, 2017, 12:20am UTC](https://discuss.elastic.co/t/mixing-alternating-query-results/80748/3 "2017-03-31T00:20:42Z")

</div>

Thanks for the suggestion Igor. It certainly beats having the network overhead of X individual searches, which is nice, but it still isn't quite what I'm looking for. Scaling issues aside, the multi search results wouldn't paginate properly as well. A deal breaker for my intended use case.

Still looking for a solution should one exist.

---

<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: [April 28, 2017, 12:20am UTC](https://discuss.elastic.co/t/mixing-alternating-query-results/80748/4 "2017-04-28T00:20:57Z")

</div>

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