# Rarest term filter

**URL:** <https://discuss.elastic.co/t/rarest-term-filter/319455>\
**Category:** Elasticsearch\
**Created:** [November 21, 2022, 1:20pm UTC](https://discuss.elastic.co/t/rarest-term-filter/319455 "2022-11-21T13:20:22Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Karel\_Haerens](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/karel_haerens/32/103075_2.png) [@Karel\_Haerens](https://discuss.elastic.co/u/Karel_Haerens)\
**Post date:** [November 21, 2022, 1:20pm UTC](https://discuss.elastic.co/t/rarest-term-filter/319455/1 "2022-11-21T13:20:22Z")

</div>

Hi all,

would there be a way to filter matches to a query in such a way that only matches are accepted where (at least) the rarest term in the result occurs in the query.

worded differently: there cannot be any unmatched terms in the result that are rarer than the matching terms.

By rare I mean terms contained in the least amount of documents.

example:

index containing

```auto
- foo corp
- bar corp

```

if I query  
`spam corp`  
I want 0 matched: both `foo corp` and `bar corp` share the word `corp` with the query, but they both contain a term that is rarer than `corp`, so they should be filtered out.

if I query  
`bar inc`  
I want `bar corp` as a match, because `bar` is the rarest term in the result, and it is matched to the query.

I basically want results that match on their most "unique" part.  
I don't immediately see a way to achieve this with scripts.

Kind regards

---

<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 19, 2022, 1:20pm UTC](https://discuss.elastic.co/t/rarest-term-filter/319455/2 "2022-12-19T13:20:28Z")

</div>

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