ehsan shirzadi
ehsan shirzadi

Reputation: 4859

Elastic Search Union Queries

I want to union 2 Elastic search queries. since my queries are sorted separately, it's not possible to do it with and/or and methods like these.
I have news documents, I want to have a result containing:
- Unread news (read = false) sorted by date
- Read News (read = true) sorted by date
Unread news mus be at first and I want to get news with page size of 20, so we may have 3 pages of unread news, 1 page of read+unread news and then 5 pages of read news.
How can I create this query?

Upvotes: 2

Views: 972

Answers (2)

pkhlop
pkhlop

Reputation: 1844

If all your fields you need for sorting are within same type you can use several sorting criteria:

POST index/article/_search
{
  "size": 20,
  "sort": [
    {
      "read": {
        "order": "asc"
      }
    },
    {
      "date": {
        "order": "desc"
      }
    }
  ]
}

Upvotes: 1

Pandiyan Cool
Pandiyan Cool

Reputation: 6565

use multi search API to run your various queries in single search request.

_msearch

After receiving results, build result set using user defined algorithm.

Upvotes: 1

Related Questions