# One-hit buckets

**URL:** <https://discuss.elastic.co/t/one-hit-buckets/60986>\
**Category:** Elasticsearch\
**Created:** [September 20, 2016, 8:32am UTC](https://discuss.elastic.co/t/one-hit-buckets/60986 "2016-09-20T08:32:50Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![gm42](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gm42/32/11804_2.png) [@gm42](https://discuss.elastic.co/u/gm42)\
**Post date:** [September 20, 2016, 8:32am UTC](https://discuss.elastic.co/t/one-hit-buckets/60986/1 "2016-09-20T08:32:50Z")

</div>

Scenario:

I would like to aggregate documents on some terms, but I only care to know if that term is there for at least 1 document, and absolutely do not care about the count of matching documents.

What would be a good way to implement this? A custom metric bucket? I could easily see a boolean check in a reduce operation.

The advantage over proper counting would be to spare some computations.

---

<div class="post-metadata">

**Author:** ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)\
**Post date:** [September 20, 2016, 9:54am UTC](https://discuss.elastic.co/t/one-hit-buckets/60986/2 "2016-09-20T09:54:36Z")

</div>

Do you know the terms that you want to check in advance? If yes you could just add these terms to a FILTER clause and then use `terminate_after=1` in order to stop processing the request after the first match.

---

<div class="post-metadata">

**Author:** ![gm42](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gm42/32/11804_2.png) [@gm42](https://discuss.elastic.co/u/gm42)\
**Post date:** [September 20, 2016, 9:55am UTC](https://discuss.elastic.co/t/one-hit-buckets/60986/3 "2016-09-20T09:55:38Z")

</div>

Yes, I do.

Wow, thanks! I think that will work, I will engineer the aggregation filter so that it uses terminate\_after

---

<div class="post-metadata">

**Author:** ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)\
**Post date:** [September 20, 2016, 10:18am UTC](https://discuss.elastic.co/t/one-hit-buckets/60986/4 "2016-09-20T10:18:12Z")

</div>

Actually, aggregation filter will not help, I was more thinking of using one search request per term that you want to check the presence of (you can put them all in a single multi-search request to save round trips).

Something like

```auto
GET _search?terminate_after=1&size=0
{
  "query": {
    "bool": {
      "must": [
        // your query
      ],
      "filter": [
        {
          "term": {
            "field_to_check": "term_to_check"
          }
        }
      ]
    }
  }
}

```

You can check wether there are documents that match both your query and `term_to_check` by looking ot whether this query has a total number of hits that is greater than 0.

---

<div class="post-metadata">

**Author:** ![gm42](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gm42/32/11804_2.png) [@gm42](https://discuss.elastic.co/u/gm42)\
**Post date:** [September 20, 2016, 11:45am UTC](https://discuss.elastic.co/t/one-hit-buckets/60986/5 "2016-09-20T11:45:06Z")

</div>

I just noticed that terminate\_after applies to all aggregations, not specific ones, so that wouldn't help.

Thanks for your second reply; I am afraid this second-query approach wouldn't work as the computation for the terms is scripted and expensive; I will use a regular aggregation for the time being, and maybe later on I will try some scripted metric aggregation if I can wrap my head around it.

---

<div class="post-metadata">

**Author:** ![gm42](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gm42/32/11804_2.png) [@gm42](https://discuss.elastic.co/u/gm42)\
**Post date:** [September 20, 2016, 2:01pm UTC](https://discuss.elastic.co/t/one-hit-buckets/60986/6 "2016-09-20T14:01:15Z")

</div>

I will use a global bucket for this, and since I am using function score I hope it will be re-used across documents, thus allowing me to save the computations

---

<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:** [July 5, 2017, 10:18pm UTC](https://discuss.elastic.co/t/one-hit-buckets/60986/7 "2017-07-05T22:18:46Z")

</div>


