Mixing/Alternating Query Results

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

You can use Multi Search to perform 3 separate searches (one in each category) and then interlace the results on your application side before displaying them to the user.

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.

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