# Sum of True (Boolean Field)

**URL:** https://discuss.elastic.co/t/sum-of-true-boolean-field/89418
**Category:** Kibana
**Created:** [June 14, 2017, 4:04pm UTC](https://discuss.elastic.co/t/sum-of-true-boolean-field/89418 "2017-06-14T16:04:18Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![Vinuja1](https://avatars.discourse-cdn.com/v4/letter/v/c57346/32.png) [@Vinuja1](https://discuss.elastic.co/u/Vinuja1)
#### Post date: [June 14, 2017, 4:04pm UTC](https://discuss.elastic.co/t/sum-of-true-boolean-field/89418/1 "2017-06-14T16:04:18Z")

</div>

Hello everyone,

In order to use Extended Metric and create a hit ratio, I need to count the number of True (boolean field).  
I have looked at scripted fields and mapping to convert my boolean to a number field and then do a sum of 1.  
But all the solutions I have tried do not work.  
Do you have an idea of how I can do it.

Thank you for you help.

---

<div class="post-metadata">

### Author: ![spalger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spalger/32/14092_2.png) [@spalger](https://discuss.elastic.co/u/spalger)
#### Post date: [June 14, 2017, 5:13pm UTC](https://discuss.elastic.co/t/sum-of-true-boolean-field/89418/2 "2017-06-14T17:13:15Z")

</div>

I would definitely use a scripted field for this.

My scripted field would look something like this:

 ![](https://us1.discourse-cdn.com/elastic/original/3X/4/3/436e3f2dab5ca12f22ff5b8f284b334eda1d3127.png)

Which produces the expected results:

 ![](https://us1.discourse-cdn.com/elastic/original/3X/6/5/6560cfa92eb985ce7d34b1296bb854214744ba1b.png)

---

<div class="post-metadata">

### Author: ![Vinuja1](https://avatars.discourse-cdn.com/v4/letter/v/c57346/32.png) [@Vinuja1](https://discuss.elastic.co/u/Vinuja1)
#### Post date: [June 14, 2017, 6:42pm UTC](https://discuss.elastic.co/t/sum-of-true-boolean-field/89418/4 "2017-06-14T18:42:08Z")

</div>

Hi,

It does not work for me.  
Below are my settings. I followed your example.

 ![](https://us1.discourse-cdn.com/elastic/original/3X/1/2/121a8767973a2bbfa5578d37d51186d9c27dc62a.png)

Am I missing anything?  
Thanks for your help

Regards  
Vinuja

---

<div class="post-metadata">

### Author: ![spalger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spalger/32/14092_2.png) [@spalger](https://discuss.elastic.co/u/spalger)
#### Post date: [June 14, 2017, 7:28pm UTC](https://discuss.elastic.co/t/sum-of-true-boolean-field/89418/5 "2017-06-14T19:28:57Z")

</div>

Can you try changing your script to just `doc['Executed'].value` and use the "String" format and type? It seems like the value you are getting is not a boolean value but something that instead always converts to `true`

---

<div class="post-metadata">

### Author: ![Vinuja1](https://avatars.discourse-cdn.com/v4/letter/v/c57346/32.png) [@Vinuja1](https://discuss.elastic.co/u/Vinuja1)
#### Post date: [June 14, 2017, 7:42pm UTC](https://discuss.elastic.co/t/sum-of-true-boolean-field/89418/6 "2017-06-14T19:42:54Z")

</div>

I can map Executed as a String, so in that case, is

> doc['Executed'].value:'True'? 1 : 0 ?

supposed to work?

Because I have already tried it, it doesn't.

Regards

---

<div class="post-metadata">

### Author: ![spalger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spalger/32/14092_2.png) [@spalger](https://discuss.elastic.co/u/spalger)
#### Post date: [June 14, 2017, 8:25pm UTC](https://discuss.elastic.co/t/sum-of-true-boolean-field/89418/7 "2017-06-14T20:25:48Z")

</div>

I think it would be

```auto
doc['Executed'].value == 'True' ? 1 : 0

```

---

<div class="post-metadata">

### Author: ![Vinuja1](https://avatars.discourse-cdn.com/v4/letter/v/c57346/32.png) [@Vinuja1](https://discuss.elastic.co/u/Vinuja1)
#### Post date: [June 16, 2017, 4:07pm UTC](https://discuss.elastic.co/t/sum-of-true-boolean-field/89418/8 "2017-06-16T16:07:06Z")

</div>

I have the following error :

> Error: Request to Elasticsearch failed: {"error":{"root\_cause":[{"type":"script\_exception","reason":"runtime error","script\_stack":["org.elasticsearch.index.mapper.TextFieldMapper$TextFieldType.fielddataBuilder(TextFieldMapper.java:336)","org.elasticsearch.index.fielddata.IndexFieldDataService.getForField(IndexFieldDataService.java:111)","org.elasticsearch.search.lookup.LeafDocLookup$1.run(LeafDocLookup.java:84)","org.elasticsearch.search.lookup.LeafDocLookup$1.run(LeafDocLookup.java:81)","java.security.AccessController.doPrivileged(Native Method)","org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:81)","org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:36)","doc['Executed'].value == 'True' ? 1 : 0"," ^---- HERE"],"script":"doc['Executed'].value == 'True' ? 1 : 0","lang":"painless"}],"type":"search\_phase\_execution\_exception","reason":"all shards failed","phase":"fetch","grouped":true,"failed\_shards":[{"shard":0,"index":"logs-","node":"kZctzZOzRAijRFQ7\_jpzQA","reason":{"type":"script\_exception","reason":"runtime error","script\_stack":["org.elasticsearch.index.mapper.TextFieldMapper$TextFieldType.fielddataBuilder(TextFieldMapper.java:336)","org.elasticsearch.index.fielddata.IndexFieldDataService.getForField(IndexFieldDataService.java:111)","org.elasticsearch.search.lookup.LeafDocLookup$1.run(LeafDocLookup.java:84)","org.elasticsearch.search.lookup.LeafDocLookup$1.run(LeafDocLookup.java:81)","java.security.AccessController.doPrivileged(Native Method)","org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:81)","org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:36)","doc['Executed'].value == 'True' ? 1 : 0"," ^---- HERE"],"script":"doc['Executed'].value == 'True' ? 1 : 0","lang":"painless","caused\_by":{"type":"illegal\_argument\_exception","reason":"Fielddata is disabled on text fields by default. Set fielddata=true on [Executed] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead."}}}]},"status":500}  
> at [http://170.50.179.48:5601/bundles/kibana.bundle.js?v=15063:269:7195](http://170.50.179.48:5601/bundles/kibana.bundle.js?v=15063:269:7195)  
> at Function.Promise.try ([http://170.50.179.48:5601/bundles/commons.bundle.js?v=15063:128:22593](http://170.50.179.48:5601/bundles/commons.bundle.js?v=15063:128:22593))  
> at [http://170.50.179.48:5601/bundles/commons.bundle.js?v=15063:128:21963](http://170.50.179.48:5601/bundles/commons.bundle.js?v=15063:128:21963)  
> at Array.map (native)  
> at Function.Promise.map ([http://170.50.179.48:5601/bundles/commons.bundle.js?v=15063:128:21918](http://170.50.179.48:5601/bundles/commons.bundle.js?v=15063:128:21918))  
> at callResponseHandlers ([http://170.50.179.48:5601/bundles/kibana.bundle.js?v=15063:269:6811](http://170.50.179.48:5601/bundles/kibana.bundle.js?v=15063:269:6811))  
> at [http://170.50.179.48:5601/bundles/kibana.bundle.js?v=15063:268:26491](http://170.50.179.48:5601/bundles/kibana.bundle.js?v=15063:268:26491)  
> at processQueue ([http://170.50.179.48:5601/bundles/commons.bundle.js?v=15063:38:23621](http://170.50.179.48:5601/bundles/commons.bundle.js?v=15063:38:23621))  
> at [http://170.50.179.48:5601/bundles/commons.bundle.js?v=15063:38:23888](http://170.50.179.48:5601/bundles/commons.bundle.js?v=15063:38:23888)  
> at Scope.$eval ([http://170.50.179.48:5601/bundles/commons.bundle.js?v=15063:39:4619](http://170.50.179.48:5601/bundles/commons.bundle.js?v=15063:39:4619))  
> at Scope.$digest ([http://170.50.179.48:5601/bundles/commons.bundle.js?v=15063:39:2359](http://170.50.179.48:5601/bundles/commons.bundle.js?v=15063:39:2359))  
> at Scope.$apply ([http://170.50.179.48:5601/bundles/commons.bundle.js?v=15063:39:5037](http://170.50.179.48:5601/bundles/commons.bundle.js?v=15063:39:5037))  
> at done ([http://170.50.179.48:5601/bundles/commons.bundle.js?v=15063:37:25027](http://170.50.179.48:5601/bundles/commons.bundle.js?v=15063:37:25027))  
> at completeRequest ([http://170.50.179.48:5601/bundles/commons.bundle.js?v=15063:37:28702](http://170.50.179.48:5601/bundles/commons.bundle.js?v=15063:37:28702))  
> at XMLHttpRequest.xhr.onload ([http://170.50.179.48:5601/bundles/commons.bundle.js?v=15063:37:29634](http://170.50.179.48:5601/bundles/commons.bundle.js?v=15063:37:29634))
> 
> ![](https://us1.discourse-cdn.com/elastic/original/3X/c/c/ccfff46afd51d98b5c4374de5344f539c1ec8c8f.png)

---

<div class="post-metadata">

### Author: ![Vinuja1](https://avatars.discourse-cdn.com/v4/letter/v/c57346/32.png) [@Vinuja1](https://discuss.elastic.co/u/Vinuja1)
#### Post date: [June 16, 2017, 6:07pm UTC](https://discuss.elastic.co/t/sum-of-true-boolean-field/89418/9 "2017-06-16T18:07:45Z")

</div>

## Are you sure we can have conditions on String field? I have seen Painless Scripts, but it doen't work. : doc['Executed'].value.equals('True')? 1 : 0

---

<div class="post-metadata">

### Author: ![Vinuja1](https://avatars.discourse-cdn.com/v4/letter/v/c57346/32.png) [@Vinuja1](https://discuss.elastic.co/u/Vinuja1)
#### Post date: [June 16, 2017, 8:19pm UTC](https://discuss.elastic.co/t/sum-of-true-boolean-field/89418/10 "2017-06-16T20:19:34Z")

</div>

I have found a solution . I have use painless scripts

---

<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 14, 2017, 8:20pm UTC](https://discuss.elastic.co/t/sum-of-true-boolean-field/89418/11 "2017-07-14T20:20:57Z")

</div>

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