# Compute Benford's law into Scripted Fields with Painless

**URL:** <https://discuss.elastic.co/t/compute-benfords-law-into-scripted-fields-with-painless/123139>\
**Category:** Elasticsearch\
**Created:** [March 8, 2018, 9:09pm UTC](https://discuss.elastic.co/t/compute-benfords-law-into-scripted-fields-with-painless/123139 "2018-03-08T21:09:31Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Elasticon](https://avatars.discourse-cdn.com/v4/letter/e/f14d63/32.png) [@Elasticon](https://discuss.elastic.co/u/Elasticon)\
**Post date:** [March 8, 2018, 9:09pm UTC](https://discuss.elastic.co/t/compute-benfords-law-into-scripted-fields-with-painless/123139/1 "2018-03-08T21:09:32Z")

</div>

Hello everyone,

I would like to compute the Benford's law into elasticsearch from a doc['some\_field'].value with a scripted field,  
I have try some lucene expression, but it's doesn't work and Expression is deprecated and support will be removed in the next major version so i would like to try with painless.

If you have any idea to compute this i take 🙂

Any tips appreciated.

Thanks!

---

<div class="post-metadata">

**Author:** ![rjernst](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rjernst/32/6363_2.png) [@rjernst](https://discuss.elastic.co/u/rjernst)\
**Post date:** [March 8, 2018, 10:12pm UTC](https://discuss.elastic.co/t/compute-benfords-law-into-scripted-fields-with-painless/123139/2 "2018-03-08T22:12:56Z")

</div>

> Expression is deprecated and support will be removed in the next major version

Just to clarify, this is not correct. Expressions are not being removed yet.

> I would like to compute the Benford's law into elasticsearch from a doc['some\_field'].value

I'm not familiar with Bendford's law or how to calculate it. Can you describe the algorithm? Also, what type of script (eg scoring script, scripted field, scripted metric agg, etc)?

---

<div class="post-metadata">

**Author:** ![Elasticon](https://avatars.discourse-cdn.com/v4/letter/e/f14d63/32.png) [@Elasticon](https://discuss.elastic.co/u/Elasticon)\
**Post date:** [March 8, 2018, 10:26pm UTC](https://discuss.elastic.co/t/compute-benfords-law-into-scripted-fields-with-painless/123139/3 "2018-03-08T22:26:24Z")

</div>

Hello @rjernst thank you for your answer,

The goal to the Benford's law it's to analyse the first digit distribution.

For example, the first digit of:  
15435 is 1  
56 is 5  
and so on

In my elasticsearch indices i have some number fields and i would like to apply this approach.  
But actually it's doesn't work ☹

Benford’s Distribution Formula

[![](http://www.statisticalconsultants.co.nz/blog/B42/benfords-distribution-formula.jpg) ](http://www.statisticalconsultants.co.nz/blog/B42/benfords-distribution-formula.jpg)

Code exemple in Java

```
double p = 0.0;
    for( int k = Math.pow(10,position-1); k < Math.pow(10,position); k++ {
        p += Math.log( 1+1.0/(k*10 + digit) );
    }

return p/Math.log(10);

```

Thank you for your help

---

<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 5, 2018, 10:26pm UTC](https://discuss.elastic.co/t/compute-benfords-law-into-scripted-fields-with-painless/123139/4 "2018-04-05T22:26:28Z")

</div>

Closed on April 5, 2018, 10:26pm UTC

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