# Filter results by another query

**URL:** <https://discuss.elastic.co/t/filter-results-by-another-query/338763>\
**Category:** Elasticsearch\
**Created:** [July 19, 2023, 10:01am UTC](https://discuss.elastic.co/t/filter-results-by-another-query/338763 "2023-07-19T10:01:07Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ndtreviv](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ndtreviv/32/22494_2.png) [@ndtreviv](https://discuss.elastic.co/u/ndtreviv)\
**Post date:** [July 19, 2023, 10:01am UTC](https://discuss.elastic.co/t/filter-results-by-another-query/338763/1 "2023-07-19T10:01:07Z")

</div>

I have a usecase where I want to search for documents that do not have counterparts in the index.

For example, consider the following:

I have a document format like this:

```auto
{
    "file": "myfile.ext",
    "classification": "class-1",
    "source": "source-1"
}

```

I might have multiple with different sources and different classifications:

```auto
{
    "file": "myfile.ext",
    "classification": "class-2",
    "source": "source-2"
}

```

I want to do a query that brings back only documents where there isn't a counterpart that has a `classification`.

eg, given the following data:

```auto
[
   {
       "file": "file-1",
       "classification": "class-2",
       "source": "source-2"
   },
   {
       "file": "file-1",
       "source": "source-5"
   },
   {
       "file": "file-2",
       "source": "source-3"
   }
]

```

only the last document (`file-2`) would be returned.

is this even possible in elasticsearch?

Initially I thought about using field collapsing on the `file` property and somehow filtering results where there were inner hits that had the `classification` field, but I'm not sure that's possible.

The next option might be to use an agg that applied filters with a sub agg that showed top hits or similar? But I'm not sure if that's even possible.

Any help/advice appreciated.

---

<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:** [August 16, 2023, 10:02am UTC](https://discuss.elastic.co/t/filter-results-by-another-query/338763/2 "2023-08-16T10:02:04Z")

</div>

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