# Count number of times a field has a particular value

**URL:** https://discuss.elastic.co/t/count-number-of-times-a-field-has-a-particular-value/179854
**Category:** Elasticsearch
**Created:** [May 6, 2019, 10:37pm UTC](https://discuss.elastic.co/t/count-number-of-times-a-field-has-a-particular-value/179854 "2019-05-06T22:37:04Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Corey\_F](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/corey_f/32/45664_2.png) [@Corey\_F](https://discuss.elastic.co/u/Corey_F)
#### Post date: [May 6, 2019, 10:37pm UTC](https://discuss.elastic.co/t/count-number-of-times-a-field-has-a-particular-value/179854/1 "2019-05-06T22:37:04Z")

</div>

Hi there,

I have a query like this:

```
"query": { 
    "multi_match" : {
            "fields" : ["form.*"],
            "lenient": true,
            "query" : "Ineligible"
        }
  }  

```

This is essentially what I want, in that it finds all the records with "Ineligible" regardless of field, however I'm trying to figure out a way to get just the name of the fields, ideally with a count. Is this possible with ElasticSearch?

To reiterate, within the index, any of the text fields could possibly start with the word "Ineligible", is there a way to query ElasticSearch and have it return a count for the number of times each field has a value that starts with Ineligible?

I'm assuming this would involve an aggregation but I'm having trouble finding the appropriate one.

An example of what would be an ideal outcome would look like:

```
[
    {
       "key": "form.fieldA",
       "doc_count": 15
    },
    {
       "key": "form.fieldB",
       "doc_count": 9
    }
]

```

Thanks!

---

<div class="post-metadata">

### Author: ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)
#### Post date: [May 7, 2019, 8:39am UTC](https://discuss.elastic.co/t/count-number-of-times-a-field-has-a-particular-value/179854/2 "2019-05-07T08:39:28Z")

</div>

> [@Corey\_F](#):
>
> I'm assuming this would involve an aggregation but I'm having trouble finding the appropriate one.

Hi Corey.  
My guess is it would be the [filters](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-filters-aggregation.html) aggregation - you'd use one filter per field so you can segregate the counts. Each filter would have a [span\_first](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-span-first-query.html) query to check the word ineligible was the first in that particular text field.

---

<div class="post-metadata">

### Author: ![Corey\_F](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/corey_f/32/45664_2.png) [@Corey\_F](https://discuss.elastic.co/u/Corey_F)
#### Post date: [May 7, 2019, 5:15pm UTC](https://discuss.elastic.co/t/count-number-of-times-a-field-has-a-particular-value/179854/3 "2019-05-07T17:15:55Z")

</div>

Hey Mark, thanks for the reply.

My only issue is this part:

> [@Mark\_Harwood](#):
>
> you'd use one filter per field

Is there a way to do it with wildcards? In my use case there are many fields to consider, and I'd like to consider multiple indexes so it doesn't seem reasonable to update and maintain the query for every field addition.

---

<div class="post-metadata">

### Author: ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)
#### Post date: [May 7, 2019, 7:43pm UTC](https://discuss.elastic.co/t/count-number-of-times-a-field-has-a-particular-value/179854/4 "2019-05-07T19:43:06Z")

</div>

I don’t think scripted aggs/queries offer the APIs to do the sort of efficient word position checking you need here.  
Your requirement is a slightly unusual one so sadly not something we offer a succinct way of doing.

---

<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: [June 4, 2019, 7:43pm UTC](https://discuss.elastic.co/t/count-number-of-times-a-field-has-a-particular-value/179854/5 "2019-06-04T19:43:12Z")

</div>

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