# Get documents matching type 1 and not in type 2

**URL:** <https://discuss.elastic.co/t/get-documents-matching-type-1-and-not-in-type-2/102979>\
**Category:** Elasticsearch\
**Created:** [October 6, 2017, 9:13am UTC](https://discuss.elastic.co/t/get-documents-matching-type-1-and-not-in-type-2/102979 "2017-10-06T09:13:03Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jaisharma](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jaisharma/32/22833_2.png) [@jaisharma](https://discuss.elastic.co/u/jaisharma)\
**Post date:** [October 6, 2017, 9:13am UTC](https://discuss.elastic.co/t/get-documents-matching-type-1-and-not-in-type-2/102979/1 "2017-10-06T09:13:03Z")

</div>

I've an index structure where types are dates and each type has several documents. I want to get all documents present in type-1 but must be absent in type-2. As an example:

```
Type-1 ---> id list = [1,2,3,4,5,6]
Type-2 ---> id list = [2,3,7,8,9]

```

The query i'm looking for should return documents from type-1 only with id = [1,4,5,6] I'm using es-py client for the above purpose.

PS: I can always get documents matching type-1 and type-2 individually followed by iterating through both of them to get what i need. But this is a solution i want to avoid for sake of performance.

Edit 1: Someone on stackoverflow suggested me the below query:

```
POST _search
{
  "query": {
    "bool" : {
      "must" : {
        "term" : { "_type" : "Type-1" }
      },
      "must_not" : {
        "term" : { "_type" : "Type-2" }
      }
    }
  }
}

```

but its not going to work as it'll give me all documents present in type-1 which do not have \_type equal to Type-2. This is basically equivalent to giving all documents matching Type-1. **This is not what i want.**

---

<div class="post-metadata">

**Author:** ![s1monw](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/s1monw/32/3637_2.png) [@s1monw](https://discuss.elastic.co/u/s1monw)\
**Post date:** [October 13, 2017, 10:15am UTC](https://discuss.elastic.co/t/get-documents-matching-type-1-and-not-in-type-2/102979/2 "2017-10-13T10:15:59Z")

</div>

maybe I miss something but what about the IDs query [https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-ids-query.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-ids-query.html)

---

<div class="post-metadata">

**Author:** ![jaisharma](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jaisharma/32/22833_2.png) [@jaisharma](https://discuss.elastic.co/u/jaisharma)\
**Post date:** [November 3, 2017, 11:42am UTC](https://discuss.elastic.co/t/get-documents-matching-type-1-and-not-in-type-2/102979/3 "2017-11-03T11:42:28Z")

</div>

From source:

> Filters documents that only have the provided ids.

How is this going to solve my problem?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [November 3, 2017, 11:59am UTC](https://discuss.elastic.co/t/get-documents-matching-type-1-and-not-in-type-2/102979/4 "2017-11-03T11:59:19Z")

</div>

You can wrap the ids query within a "bool -\> must not" clause I think.

BTW note that creating multiple types within the same index is not supported in 6.0 anymore.

---

<div class="post-metadata">

**Author:** ![jaisharma](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jaisharma/32/22833_2.png) [@jaisharma](https://discuss.elastic.co/u/jaisharma)\
**Post date:** [November 7, 2017, 9:24am UTC](https://discuss.elastic.co/t/get-documents-matching-type-1-and-not-in-type-2/102979/5 "2017-11-07T09:24:18Z")

</div>

Please read the problem statement carefully. I am not trying to do any manual stuff here. I don't have a list of IDs which i can supply in the query and get a **not** of it.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [November 7, 2017, 10:28am UTC](https://discuss.elastic.co/t/get-documents-matching-type-1-and-not-in-type-2/102979/6 "2017-11-07T10:28:15Z")

</div>

So you are trying to do some king of "joins" between documents?

We don't really support that I believe.

May be if you come with a full example with some sample document, we can try to find a way to solve your use case.

---

<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:** [November 7, 2017, 10:39am UTC](https://discuss.elastic.co/t/get-documents-matching-type-1-and-not-in-type-2/102979/7 "2017-11-07T10:39:36Z")

</div>

It looks like you are trying to get all the documents in one set of results (\_type = Type-1) that are not present in another set (\_type = Type-2). This is effectively a join, which Elasticsearch does not support. You will therefore need to perform 2 requests.

---

<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:** [December 5, 2017, 10:39am UTC](https://discuss.elastic.co/t/get-documents-matching-type-1-and-not-in-type-2/102979/8 "2017-12-05T10:39:36Z")

</div>

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