# Is it possible to filter multiple records of one index from another index in elasticsearch query?

**URL:** <https://discuss.elastic.co/t/is-it-possible-to-filter-multiple-records-of-one-index-from-another-index-in-elasticsearch-query/210464>\
**Category:** Elasticsearch\
**Created:** [December 4, 2019, 6:09am UTC](https://discuss.elastic.co/t/is-it-possible-to-filter-multiple-records-of-one-index-from-another-index-in-elasticsearch-query/210464 "2019-12-04T06:09:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![adarshzphin](https://avatars.discourse-cdn.com/v4/letter/a/958977/32.png) [@adarshzphin](https://discuss.elastic.co/u/adarshzphin)\
**Post date:** [December 4, 2019, 6:09am UTC](https://discuss.elastic.co/t/is-it-possible-to-filter-multiple-records-of-one-index-from-another-index-in-elasticsearch-query/210464/1 "2019-12-04T06:09:26Z")

</div>

Lets say I have 100 records in index1 and 10 records in index2. And I want to get like this:

```
select * from index1 where id not in (select id from index2)

```

Can we have the same above query in elasticsearch or is it even possible.

I have tried with term query its working only for 1 or some ids. I need to filter out around millions of ids from another index. Below is my query:

```
GET index1/_search
{
  "query": {
    "bool": {
      "must_not": [
        {
          "terms": {
        "id.keyword": {
            "index" : "index2",
            "id" : "some_id_value",
            "path" : "id"
        }
    }
        }
      ]
    } 
    
  }
}

```

I am using elasticsearch-7.3.0

Please suggest!

---

<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:** [December 4, 2019, 8:36am UTC](https://discuss.elastic.co/t/is-it-possible-to-filter-multiple-records-of-one-index-from-another-index-in-elasticsearch-query/210464/2 "2019-12-04T08:36:47Z")

</div>

You are trying to do a "join" basically which is not really supported.  
You probably to do that with a batch or similar I guess.

So scroll over all the `_id` from `index2` and for each do a `GET /index/_doc/{id}`. Multiget is probably easier. Or using the [ids query](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-ids-query.html).

---

<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:** [January 1, 2020, 8:36am UTC](https://discuss.elastic.co/t/is-it-possible-to-filter-multiple-records-of-one-index-from-another-index-in-elasticsearch-query/210464/3 "2020-01-01T08:36:48Z")

</div>

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