# Query with union and subtract by field

**URL:** https://discuss.elastic.co/t/query-with-union-and-subtract-by-field/235056
**Category:** Elasticsearch
**Created:** [May 31, 2020, 9:15am UTC](https://discuss.elastic.co/t/query-with-union-and-subtract-by-field/235056 "2020-05-31T09:15:13Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![wildfire\_enterprise](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wildfire_enterprise/32/67296_2.png) [@wildfire\_enterprise](https://discuss.elastic.co/u/wildfire_enterprise)
#### Post date: [May 31, 2020, 9:15am UTC](https://discuss.elastic.co/t/query-with-union-and-subtract-by-field/235056/1 "2020-05-31T09:15:13Z")

</div>

Hi. I'm new in elasticsearch, need some help to write a query.  
Assume this is my index data:

```auto
    "_source" : {
      "id" : "1",
      "tag" : "tag1"
    }
    "_source" : {
      "id" : "2",
      "tag" : "tag1"
    }
    "_source" : {
      "id" : "2",
      "tag" : "tag2"
    }
    "_source" : {
      "id" : "3",
      "tag" : "tag2"
    }
    "_source" : {
      "id" : "1",
      "tag" : "tag3"
    }
    "_source" : {
      "id" : "4",
      "tag" : "tag4"
    }

```

I want a query return docs with unique id of `{"tag1", "tag2"}-{"tag3", "tag4"}` where the subtract is by field id. As a result:

```auto
    "_source" : {
      "id" : "2",
      "tag" : "tag1"
    }
    "_source" : {
      "id" : "3",
      "tag" : "tag2"
    }

```

---

<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: [June 28, 2020, 9:15am UTC](https://discuss.elastic.co/t/query-with-union-and-subtract-by-field/235056/2 "2020-06-28T09:15:25Z")

</div>

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