Get documents matching type 1 and not in type 2

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.

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

From source:

Filters documents that only have the provided ids.

How is this going to solve my problem?

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.

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.

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.

1 Like

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.

1 Like

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