# Difference between results of two queries

**URL:** https://discuss.elastic.co/t/difference-between-results-of-two-queries/270991
**Category:** Elasticsearch
**Created:** [April 22, 2021, 4:38pm UTC](https://discuss.elastic.co/t/difference-between-results-of-two-queries/270991 "2021-04-22T16:38:00Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Askia\_Mohamed\_Kadri](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/askia_mohamed_kadri/32/134665_2.png) [@Askia\_Mohamed\_Kadri](https://discuss.elastic.co/u/Askia_Mohamed_Kadri)
#### Post date: [April 22, 2021, 4:38pm UTC](https://discuss.elastic.co/t/difference-between-results-of-two-queries/270991/1 "2021-04-22T16:38:00Z")

</div>

Hi!  
I'm stuck and I really need help. Here is my use case: I would like to perform two queries and get as final result the difference between those two queries based on a specific field.  
Let's assume that I have these docs:  
{name: A, state: Washington, Age: 18}  
{name: B, state: Washington, Age: 26}  
{name: A, state: Georgia, Age: 18}  
{name: B, state: Texas, Age: 26}  
{name: C, state: Washington, Age: 18}

and I would like to do the difference between query 1: docs with state different from Washington and query 2: docs with state = Washington based on the name. I keep all results from query 1 and and the results of query 2 if the name is not present in the results of query 1.  
That would give the result:  
{name: A, state: Georgia, Age: 18}  
{name: B, state: Texas, Age: 26}  
{name: C, state: Washington, Age: 18}

Thanks

---

<div class="post-metadata">

### Author: ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)
#### Post date: [April 26, 2021, 2:09pm UTC](https://discuss.elastic.co/t/difference-between-results-of-two-queries/270991/2 "2021-04-26T14:09:37Z")

</div>

That sounds like a join, which is not supported within Elasticsearch. You probably need to run 2 queries and use data from the first in the second.

---

<div class="post-metadata">

### Author: ![Askia\_Mohamed\_Kadri](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/askia_mohamed_kadri/32/134665_2.png) [@Askia\_Mohamed\_Kadri](https://discuss.elastic.co/u/Askia_Mohamed_Kadri)
#### Post date: [April 26, 2021, 2:15pm UTC](https://discuss.elastic.co/t/difference-between-results-of-two-queries/270991/3 "2021-04-26T14:15:43Z")

</div>

So there is no way to run two different queries, name them and run a third query using the name of the two first queries?  
Isn't it possible with scripts?

---

<div class="post-metadata">

### Author: ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)
#### Post date: [April 26, 2021, 2:22pm UTC](https://discuss.elastic.co/t/difference-between-results-of-two-queries/270991/4 "2021-04-26T14:22:10Z")

</div>

You would need to create and run the queries from outside Elasticsearch.

---

<div class="post-metadata">

### Author: ![Askia\_Mohamed\_Kadri](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/askia_mohamed_kadri/32/134665_2.png) [@Askia\_Mohamed\_Kadri](https://discuss.elastic.co/u/Askia_Mohamed_Kadri)
#### Post date: [April 26, 2021, 2:40pm UTC](https://discuss.elastic.co/t/difference-between-results-of-two-queries/270991/5 "2021-04-26T14:40:26Z")

</div>

Alright. Thanks

---

<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: [May 24, 2021, 2:40pm UTC](https://discuss.elastic.co/t/difference-between-results-of-two-queries/270991/6 "2021-05-24T14:40:30Z")

</div>

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