# Perform Union on Two Sorted Queries

**URL:** <https://discuss.elastic.co/t/perform-union-on-two-sorted-queries/23603>\
**Category:** Elasticsearch\
**Created:** [May 11, 2015, 11:42am UTC](https://discuss.elastic.co/t/perform-union-on-two-sorted-queries/23603 "2015-05-11T11:42:28Z")\
**Posts on this page:** 2\
**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:42am UTC](https://discuss.elastic.co/t/perform-union-on-two-sorted-queries/23603/1 "2015-05-11T11:42:28Z")

</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:** ![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:14am UTC](https://discuss.elastic.co/t/perform-union-on-two-sorted-queries/23603/2 "2017-07-06T00:14:39Z")

</div>


