# Scripted field & range analysis not working properly?

**URL:** <https://discuss.elastic.co/t/scripted-field-range-analysis-not-working-properly/322361>\
**Category:** Kibana\
**Tags:** painless\
**Created:** [January 3, 2023, 10:59am UTC](https://discuss.elastic.co/t/scripted-field-range-analysis-not-working-properly/322361 "2023-01-03T10:59:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![SalvoDM91](https://avatars.discourse-cdn.com/v4/letter/s/e19adc/32.png) [@SalvoDM91](https://discuss.elastic.co/u/SalvoDM91)\
**Post date:** [January 3, 2023, 10:59am UTC](https://discuss.elastic.co/t/scripted-field-range-analysis-not-working-properly/322361/1 "2023-01-03T10:59:51Z")

</div>

Hi guys,

happy new year 🙂  
I have a scripet field (number, percentage) with this code:

```auto
double tot_acc_nom_gruppo = doc['tot_acc_nom_gruppo'].value;
double tot_acc_pond_gruppo = doc['tot_acc_pond_gruppo'].value;
double delta_pratica = 1;
if((tot_acc_nom_gruppo != 0) && (tot_acc_pond_gruppo != 0)){
    delta_pratica = tot_acc_nom_gruppo / tot_acc_pond_gruppo;
}

return delta_pratica;

```

This painless code produces some results like:  
47.345% or 137.238% and this is fine for me.

But if I try to create a table with a filter column in order to see the unique count of my documents for each range (0 - 10%, 10% - 20%, 20% - 30%) the total sum of unique count for each range is greather then the real unique count of my documents.

It's like some document is part of 2 or more range. Could you please help me?

Thanks!  
Salvo

---

<div class="post-metadata">

**Author:** ![Dzmitry](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dzmitry/32/65026_2.png) [@Dzmitry](https://discuss.elastic.co/u/Dzmitry)\
**Post date:** [January 4, 2023, 4:46pm UTC](https://discuss.elastic.co/t/scripted-field-range-analysis-not-working-properly/322361/2 "2023-01-04T16:46:15Z")

</div>

Hi @SalvoDM91 ,

Could you share an example of your aggregation? It could be the same document ends in different [buckets](https://www.elastic.co/guide/en/elasticsearch/reference/master/search-aggregations-bucket.html) (data sets) based on your conditions.

Regards, Dima

---

<div class="post-metadata">

**Author:** ![SalvoDM91](https://avatars.discourse-cdn.com/v4/letter/s/e19adc/32.png) [@SalvoDM91](https://discuss.elastic.co/u/SalvoDM91)\
**Post date:** [January 4, 2023, 5:44pm UTC](https://discuss.elastic.co/t/scripted-field-range-analysis-not-working-properly/322361/3 "2023-01-04T17:44:12Z")

</div>

Ciao Dzmitry, I just found the problem.  
I have more then 900K documents in my index and I see inside the documentation that Unique count is an an **approximate count of distinct values**.

This is a big issue for me... there is any way in Kibana in order to see the exact number?

Thanks in advance  
Salvo

---

<div class="post-metadata">

**Author:** ![Dzmitry](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dzmitry/32/65026_2.png) [@Dzmitry](https://discuss.elastic.co/u/Dzmitry)\
**Post date:** [January 5, 2023, 8:14am UTC](https://discuss.elastic.co/t/scripted-field-range-analysis-not-working-properly/322361/4 "2023-01-05T08:14:47Z")

</div>

I'm afraid with these amount of docs Elasticsearch might not return the exact count, but approximate one. But I would double check with Elasticsearch community by posting question there.

Regards, DIma

---

<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:** [February 2, 2023, 8:15am UTC](https://discuss.elastic.co/t/scripted-field-range-analysis-not-working-properly/322361/5 "2023-02-02T08:15:42Z")

</div>

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