# Count when element has field "tcode"

**URL:** <https://discuss.elastic.co/t/count-when-element-has-field-tcode/80119>\
**Category:** Kibana\
**Created:** [March 27, 2017, 10:19am UTC](https://discuss.elastic.co/t/count-when-element-has-field-tcode/80119 "2017-03-27T10:19:12Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![bertvervaele](https://avatars.discourse-cdn.com/v4/letter/b/8e7dd6/32.png) [@bertvervaele](https://discuss.elastic.co/u/bertvervaele)\
**Post date:** [March 27, 2017, 10:19am UTC](https://discuss.elastic.co/t/count-when-element-has-field-tcode/80119/1 "2017-03-27T10:19:12Z")

</div>

I would like a graph that only counts the elements that have the field "tcode".  
I think I have to use JSON for it but I'm not sure maybe there is an easier way.  
This is the JSON code I tried but it doesn't work (it still counts every element):  
{  
"query": {  
"exists":{  
"field": "tcode"  
}  
}  
}

Greating Bert

---

<div class="post-metadata">

**Author:** ![weltenwort](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/weltenwort/32/53885_2.png) [@weltenwort](https://discuss.elastic.co/u/weltenwort)\
**Post date:** [March 27, 2017, 11:06am UTC](https://discuss.elastic.co/t/count-when-element-has-field-tcode/80119/2 "2017-03-27T11:06:17Z")

</div>

Hi @bertvervaele,

you should be able to achieve that by using the "Count" aggregation in combination with a [query](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html#query-string-syntax) like `_exists_:tcode` in the top query bar.

---

<div class="post-metadata">

**Author:** ![bertvervaele](https://avatars.discourse-cdn.com/v4/letter/b/8e7dd6/32.png) [@bertvervaele](https://discuss.elastic.co/u/bertvervaele)\
**Post date:** [March 27, 2017, 11:37am UTC](https://discuss.elastic.co/t/count-when-element-has-field-tcode/80119/3 "2017-03-27T11:37:10Z")

</div>

Thanks for the help! It does work when I do it like that .  
But I'm look to make a graph with 2 Y-axis ( 1 with all elements and 1 with the elements that does have the tcode field.  
So is there an other way then to use the filter?

---

<div class="post-metadata">

**Author:** ![weltenwort](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/weltenwort/32/53885_2.png) [@weltenwort](https://discuss.elastic.co/u/weltenwort)\
**Post date:** [March 27, 2017, 12:29pm UTC](https://discuss.elastic.co/t/count-when-element-has-field-tcode/80119/4 "2017-03-27T12:29:08Z")

</div>

I see - in that case you should be able to use the "Filters" aggregation with two filters to split the results into two buckets: `*` for all documents and `_exists_:tcode` for all documents that have the field.

 ![](https://us1.discourse-cdn.com/elastic/original/3X/d/c/dc87267169393e0ea229cff1e3cad03becb0ed61.png)

You can also combine that with further aggregations, such as "Date Histogram".

---

<div class="post-metadata">

**Author:** ![bertvervaele](https://avatars.discourse-cdn.com/v4/letter/b/8e7dd6/32.png) [@bertvervaele](https://discuss.elastic.co/u/bertvervaele)\
**Post date:** [March 27, 2017, 12:32pm UTC](https://discuss.elastic.co/t/count-when-element-has-field-tcode/80119/5 "2017-03-27T12:32:13Z")

</div>

Thank you again this is exactly what I was looking for 🙂

---

<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:** [April 24, 2017, 12:32pm UTC](https://discuss.elastic.co/t/count-when-element-has-field-tcode/80119/6 "2017-04-24T12:32:23Z")

</div>

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