Sum of True (Boolean Field)

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.

I would definitely use a scripted field for this.

My scripted field would look something like this:

Which produces the expected results:

Hi,

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

Am I missing anything?
Thanks for your help

Regards
Vinuja

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

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

I think it would be

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

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
at Function.Promise.try (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
at Array.map (native)
at Function.Promise.map (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)
at 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)
at 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)
at Scope.$digest (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)
at done (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)
at XMLHttpRequest.xhr.onload (http://170.50.179.48:5601/bundles/commons.bundle.js?v=15063:37:29634)

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

I have found a solution . I have use painless scripts

1 Like

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