# Perform Union on Two Sorted Queries

**URL:** <https://discuss.elastic.co/t/perform-union-on-two-sorted-queries/494>\
**Category:** Elasticsearch\
**Created:** [May 11, 2015, 11:52am UTC](https://discuss.elastic.co/t/perform-union-on-two-sorted-queries/494 "2015-05-11T11:52:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![wajahat\_Javaid](https://avatars.discourse-cdn.com/v4/letter/w/9de053/32.png) [@wajahat\_Javaid](https://discuss.elastic.co/u/wajahat_Javaid)\
**Post date:** [May 11, 2015, 11:52am UTC](https://discuss.elastic.co/t/perform-union-on-two-sorted-queries/494/1 "2015-05-11T11:52:40Z")

</div>

Hi all,  
I have a use case where I have to run two elastic search Queries which are paginated, have a sorting criteria, but the number of results are same, I would like to know if there is a possibility to UNION both queries, besides doing it my self by iterating results of both the queries at application end.

Sample Type definition :

{  
"lineNo": 4,  
"x1": 1556,  
"x2": 1758,  
"snippetText": "This is my text",

}

x1 and x2 are numric values per line, there are multiple documents with same lineNo and multiple x1, x2 values  
what I want is a list of relative x1 sorted by x1 and line number  
list of relative x2 sorted by x2 and line number

My Current Queries are:  
{  
"size": 192,  
"query": {  
"filtered": {  
"filter": {  
"range": {  
"x1": {  
"gt": 0  
}  
}  
}  
}  
},  
"track\_scores": true,  
"sort": {  
"x1": {  
"order": "asc"  
},  
"lineNo": {  
"order": "asc"  
}  
}  
}

{  
"size": 192,  
"query": {  
"filtered": {  
"filter": {  
"range": {  
"x2": {  
"gt": 0  
}  
}  
}  
}  
},  
"track\_scores": true,  
"sort": {  
"x2": {  
"order": "asc"  
},  
"lineNo": {  
"order": "asc"  
}  
}  
}

Both Queries work fine separately, I just want to run them in single request, resulting in a single hits[] object.

---

<div class="post-metadata">

**Author:** ![polyfractal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/polyfractal/32/48162_2.png) [@polyfractal](https://discuss.elastic.co/u/polyfractal)\
**Post date:** [May 11, 2015, 12:56pm UTC](https://discuss.elastic.co/t/perform-union-on-two-sorted-queries/494/2 "2015-05-11T12:56:24Z")

</div>

You can combine them by using a `bool` filter with two `should` clauses. You'll have to combine the sort values as well, and decide if you want to sort x1 or x2 first when there are ties. If some of the docs don't have an x1 or x2 field, you may also have to specify a [`missing` value for sort](http://www.elastic.co/guide/en/elasticsearch/reference/1.5/search-request-sort.html#_missing_values) to handle them correctly.

```json
{
  "size": 400,
  "query": {
    "filtered": {
      "filter": {
        "bool": {
          "should": [
            {
              "range": {
                "x1": {
                  "gt": 0
                }
              }
            },
            {
              "range": {
                "x2": {
                  "gt": 0
                }
              }
            }
          ]
        }
      }
    }
  },
  "track_scores": true,
  "sort": {
    "x1": {
      "order": "asc"
    },
    "x2": {
      "order": "asc"
    },
    "lineNo": {
      "order": "asc"
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![wajahat\_Javaid](https://avatars.discourse-cdn.com/v4/letter/w/9de053/32.png) [@wajahat\_Javaid](https://discuss.elastic.co/u/wajahat_Javaid)\
**Post date:** [May 11, 2015, 1:29pm UTC](https://discuss.elastic.co/t/perform-union-on-two-sorted-queries/494/3 "2015-05-11T13:29:12Z")

</div>

Thank you for the quick reply, seems really helpful.  
x1 and x2 values would not be missing in any scenario, so missing value handling isn't going to be a problem.  
I'll try to use it in my scenario and see if it can serve the purpose.

Thank you again

---

<div class="post-metadata">

**Author:** ![saldan77](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/saldan77/32/595_2.png) [@saldan77](https://discuss.elastic.co/u/saldan77)\
**Post date:** [June 10, 2015, 11:11am UTC](https://discuss.elastic.co/t/perform-union-on-two-sorted-queries/494/4 "2015-06-10T11:11:42Z")

</div>

Hi guys, what if I need to limit in size the first resultset (x1\>0)?

I need to select maximum XX records if x1\>0 and unlimited records if x2\>0.

Is it possible without iterating at application end?

---

<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 6, 2017, 12:08am UTC](https://discuss.elastic.co/t/perform-union-on-two-sorted-queries/494/5 "2017-07-06T00:08:38Z")

</div>


