# Statistical facet on length of string field

**URL:** <https://discuss.elastic.co/t/statistical-facet-on-length-of-string-field/16974>\
**Category:** Elasticsearch\
**Created:** [April 12, 2014, 9:55am UTC](https://discuss.elastic.co/t/statistical-facet-on-length-of-string-field/16974 "2014-04-12T09:55:31Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![CC1](https://avatars.discourse-cdn.com/v4/letter/c/9fc348/32.png) [@CC1](https://discuss.elastic.co/u/CC1)\
**Post date:** [April 12, 2014, 9:55am UTC](https://discuss.elastic.co/t/statistical-facet-on-length-of-string-field/16974/1 "2014-04-12T09:55:31Z")

</div>

I would like to retrieve statistical data about a string field like the  
min, max and average length (by counting the number of characters inside  
the string). I am using CURL to do this and I have what I have tried so far

"query":{  
"match\_all": {}  
},  
"facets":{  
"stats":{  
"statistical":{  
"field":"title"}  
}  
}

returns me exception because the facet expects a number field. How can I  
achieve my goal? Thanks!

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/3c1a08f2-e467-47c3-a667-43837ac9b352%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/3c1a08f2-e467-47c3-a667-43837ac9b352%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![vineeth\_mohan\_2](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vineeth_mohan_2/32/747_2.png) [@vineeth\_mohan\_2](https://discuss.elastic.co/u/vineeth_mohan_2)\
**Post date:** [April 12, 2014, 12:50pm UTC](https://discuss.elastic.co/t/statistical-facet-on-length-of-string-field/16974/2 "2014-04-12T12:50:04Z")

</div>

Hello Cristina ,

This is not possible using stat facet. It expect the field to be a number  
and it calculates max/min etc based on its different values.

A way to do this would be to add a new field while indexing which would be  
the length of the title.  
Then performing stats over it might work.

There might be other solutions based on scripting , but i need to explore  
...

Thanks  
Vineeth

On Sat, Apr 12, 2014 at 3:25 PM, Cristina Garbacea \<  
[cr1st1na.garbacea@gmail.com](mailto:cr1st1na.garbacea@gmail.com)\> wrote:

> I would like to retrieve statistical data about a string field like the  
> min, max and average length (by counting the number of characters inside  
> the string). I am using CURL to do this and I have what I have tried so far
> 
> "query":{  
> "match\_all": {}  
> },  
> "facets":{  
> "stats":{  
> "statistical":{  
> "field":"title"}  
> }  
> }
> 
> returns me exception because the facet expects a number field. How can I  
> achieve my goal? Thanks!
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/3c1a08f2-e467-47c3-a667-43837ac9b352%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/3c1a08f2-e467-47c3-a667-43837ac9b352%40googlegroups.com)[https://groups.google.com/d/msgid/elasticsearch/3c1a08f2-e467-47c3-a667-43837ac9b352%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/3c1a08f2-e467-47c3-a667-43837ac9b352%40googlegroups.com?utm_medium=email&utm_source=footer)  
> .  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CAGdPd5mPjyDiGtodYWQEMxZy%2BjOVnd1pXV4AYW%2BrrmRLWRh6qw%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAGdPd5mPjyDiGtodYWQEMxZy%2BjOVnd1pXV4AYW%2BrrmRLWRh6qw%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![CC1](https://avatars.discourse-cdn.com/v4/letter/c/9fc348/32.png) [@CC1](https://discuss.elastic.co/u/CC1)\
**Post date:** [April 12, 2014, 2:15pm UTC](https://discuss.elastic.co/t/statistical-facet-on-length-of-string-field/16974/3 "2014-04-12T14:15:59Z")

</div>

Thank you very much for your reply. I tried using script fields, like  
this:

"query":{  
"match\_all": {}  
},  
"script\_fields":{  
"test1":{  
"script": "\_source.title.length"}  
}  
}

but my issue now is that I get "PropertyAccessException: Could not access  
length". My title field is of type string and I was expecting Elasticsearch  
to know about it. I am also using CURL for queries, is there any way to  
overcome this error?

Thank you,  
Cristina

On Saturday, April 12, 2014 2:50:04 PM UTC+2, vineeth mohan wrote:

> Hello Cristina ,
> 
> This is not possible using stat facet. It expect the field to be a number  
> and it calculates max/min etc based on its different values.
> 
> A way to do this would be to add a new field while indexing which would be  
> the length of the title.  
> Then performing stats over it might work.
> 
> There might be other solutions based on scripting , but i need to explore  
> ...
> 
> Thanks  
> Vineeth
> 
> On Sat, Apr 12, 2014 at 3:25 PM, Cristina Garbacea \<[cr1st1na...@gmail.com](mailto:cr1st1na...@gmail.com)\<javascript:\>
> 
> > wrote:
> 
> > I would like to retrieve statistical data about a string field like the  
> > min, max and average length (by counting the number of characters inside  
> > the string). I am using CURL to do this and I have what I have tried so far
> > 
> > "query":{  
> > "match\_all": {}  
> > },  
> > "facets":{  
> > "stats":{  
> > "statistical":{  
> > "field":"title"}  
> > }  
> > }
> > 
> > returns me exception because the facet expects a number field. How can I  
> > achieve my goal? Thanks!
> > 
> > --  
> > You received this message because you are subscribed to the Google Groups  
> > "elasticsearch" group.  
> > To unsubscribe from this group and stop receiving emails from it, send an  
> > email to [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> > To view this discussion on the web visit  
> > [https://groups.google.com/d/msgid/elasticsearch/3c1a08f2-e467-47c3-a667-43837ac9b352%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/3c1a08f2-e467-47c3-a667-43837ac9b352%40googlegroups.com)[https://groups.google.com/d/msgid/elasticsearch/3c1a08f2-e467-47c3-a667-43837ac9b352%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/3c1a08f2-e467-47c3-a667-43837ac9b352%40googlegroups.com?utm_medium=email&utm_source=footer)  
> > .  
> > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/8245a839-4374-4235-a942-0a531c79cdfa%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/8245a839-4374-4235-a942-0a531c79cdfa%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![CC1](https://avatars.discourse-cdn.com/v4/letter/c/9fc348/32.png) [@CC1](https://discuss.elastic.co/u/CC1)\
**Post date:** [April 12, 2014, 2:22pm UTC](https://discuss.elastic.co/t/statistical-facet-on-length-of-string-field/16974/4 "2014-04-12T14:22:43Z")

</div>

Oh, I found out! I was supposed to use length(). Thanks a lot, you pointed  
me in the right direction! 🙂

On Saturday, April 12, 2014 4:15:59 PM UTC+2, CC wrote:

> Thank you very much for your reply. I tried using script fields, like  
> this:
> 
> "query":{  
> "match\_all": {}  
> },  
> "script\_fields":{  
> "test1":{  
> "script": "\_source.title.length"}  
> }  
> }
> 
> but my issue now is that I get "PropertyAccessException: Could not access  
> length". My title field is of type string and I was expecting Elasticsearch  
> to know about it. I am also using CURL for queries, is there any way to  
> overcome this error?
> 
> Thank you,  
> Cristina
> 
> On Saturday, April 12, 2014 2:50:04 PM UTC+2, vineeth mohan wrote:
> 
> > Hello Cristina ,
> > 
> > This is not possible using stat facet. It expect the field to be a number  
> > and it calculates max/min etc based on its different values.
> > 
> > A way to do this would be to add a new field while indexing which would  
> > be the length of the title.  
> > Then performing stats over it might work.
> > 
> > There might be other solutions based on scripting , but i need to explore  
> > ...
> > 
> > Thanks  
> > Vineeth
> > 
> > On Sat, Apr 12, 2014 at 3:25 PM, Cristina Garbacea \<[cr1st1na...@gmail.com](mailto:cr1st1na...@gmail.com)
> > 
> > > wrote:
> > 
> > > I would like to retrieve statistical data about a string field like the  
> > > min, max and average length (by counting the number of characters inside  
> > > the string). I am using CURL to do this and I have what I have tried so far
> > > 
> > > "query":{  
> > > "match\_all": {}  
> > > },  
> > > "facets":{  
> > > "stats":{  
> > > "statistical":{  
> > > "field":"title"}  
> > > }  
> > > }
> > > 
> > > returns me exception because the facet expects a number field. How can I  
> > > achieve my goal? Thanks!
> > > 
> > > --  
> > > You received this message because you are subscribed to the Google  
> > > Groups "elasticsearch" group.  
> > > To unsubscribe from this group and stop receiving emails from it, send  
> > > an email to [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com).  
> > > To view this discussion on the web visit  
> > > [https://groups.google.com/d/msgid/elasticsearch/3c1a08f2-e467-47c3-a667-43837ac9b352%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/3c1a08f2-e467-47c3-a667-43837ac9b352%40googlegroups.com)[https://groups.google.com/d/msgid/elasticsearch/3c1a08f2-e467-47c3-a667-43837ac9b352%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/3c1a08f2-e467-47c3-a667-43837ac9b352%40googlegroups.com?utm_medium=email&utm_source=footer)  
> > > .  
> > > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/8fbb42c1-636b-41f6-b22d-4e4bbaf21719%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/8fbb42c1-636b-41f6-b22d-4e4bbaf21719%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![CC1](https://avatars.discourse-cdn.com/v4/letter/c/9fc348/32.png) [@CC1](https://discuss.elastic.co/u/CC1)\
**Post date:** [April 13, 2014, 9:21am UTC](https://discuss.elastic.co/t/statistical-facet-on-length-of-string-field/16974/5 "2014-04-13T09:21:52Z")

</div>

Thank you very much for your reply. I tried using script fields, like this:

"query":{  
"match\_all": {}  
},  
"script\_fields":{  
"test1":{  
"script": "\_source.title.length()"}  
}  
}

but what I get back from ES are only counts for the top 10 docs. How can I  
get values like max length, min length and average length for my title  
field? Thank you!

On Saturday, April 12, 2014 2:50:04 PM UTC+2, vineeth mohan wrote:

> Hello Cristina ,
> 
> This is not possible using stat facet. It expect the field to be a number  
> and it calculates max/min etc based on its different values.
> 
> A way to do this would be to add a new field while indexing which would be  
> the length of the title.  
> Then performing stats over it might work.
> 
> There might be other solutions based on scripting , but i need to explore  
> ...
> 
> Thanks  
> Vineeth
> 
> On Sat, Apr 12, 2014 at 3:25 PM, Cristina Garbacea \<[cr1st1na...@gmail.com](mailto:cr1st1na...@gmail.com)\<javascript:\>
> 
> > wrote:
> 
> > I would like to retrieve statistical data about a string field like the  
> > min, max and average length (by counting the number of characters inside  
> > the string). I am using CURL to do this and I have what I have tried so far
> > 
> > "query":{  
> > "match\_all": {}  
> > },  
> > "facets":{  
> > "stats":{  
> > "statistical":{  
> > "field":"title"}  
> > }  
> > }
> > 
> > returns me exception because the facet expects a number field. How can I  
> > achieve my goal? Thanks!
> > 
> > --  
> > You received this message because you are subscribed to the Google Groups  
> > "elasticsearch" group.  
> > To unsubscribe from this group and stop receiving emails from it, send an  
> > email to [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> > To view this discussion on the web visit  
> > [https://groups.google.com/d/msgid/elasticsearch/3c1a08f2-e467-47c3-a667-43837ac9b352%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/3c1a08f2-e467-47c3-a667-43837ac9b352%40googlegroups.com)[https://groups.google.com/d/msgid/elasticsearch/3c1a08f2-e467-47c3-a667-43837ac9b352%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/3c1a08f2-e467-47c3-a667-43837ac9b352%40googlegroups.com?utm_medium=email&utm_source=footer)  
> > .  
> > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/8bd3444c-5095-463c-adb9-60ea2eb42e57%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/8bd3444c-5095-463c-adb9-60ea2eb42e57%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![vineeth\_mohan\_2](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vineeth_mohan_2/32/747_2.png) [@vineeth\_mohan\_2](https://discuss.elastic.co/u/vineeth_mohan_2)\
**Post date:** [April 13, 2014, 5:15pm UTC](https://discuss.elastic.co/t/statistical-facet-on-length-of-string-field/16974/6 "2014-04-13T17:15:53Z")

</div>

Use the script feature in the stats facet -

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

The value computed can also be a script, using the value\_script instead of  
value\_field, in which case the lang can control its language, and params allow  
to provide custom parameters (as in other scripted components).

On Sun, Apr 13, 2014 at 2:51 PM, CC [cr1st1na.garbacea@gmail.com](mailto:cr1st1na.garbacea@gmail.com) wrote:

> Thank you very much for your reply. I tried using script fields, like  
> this:
> 
> "query":{  
> "match\_all": {}  
> },  
> "script\_fields":{  
> "test1":{  
> "script": "\_source.title.length()"}  
> }  
> }
> 
> but what I get back from ES are only counts for the top 10 docs. How can  
> I get values like max length, min length and average length for my title  
> field? Thank you!
> 
> On Saturday, April 12, 2014 2:50:04 PM UTC+2, vineeth mohan wrote:
> 
> > Hello Cristina ,
> > 
> > This is not possible using stat facet. It expect the field to be a number  
> > and it calculates max/min etc based on its different values.
> > 
> > A way to do this would be to add a new field while indexing which would  
> > be the length of the title.  
> > Then performing stats over it might work.
> > 
> > There might be other solutions based on scripting , but i need to explore  
> > ...
> > 
> > Thanks  
> > Vineeth
> > 
> > On Sat, Apr 12, 2014 at 3:25 PM, Cristina Garbacea \<[cr1st1na...@gmail.com](mailto:cr1st1na...@gmail.com)
> > 
> > > wrote:
> > 
> > > I would like to retrieve statistical data about a string field like the  
> > > min, max and average length (by counting the number of characters inside  
> > > the string). I am using CURL to do this and I have what I have tried so far
> > > 
> > > "query":{  
> > > "match\_all": {}  
> > > },  
> > > "facets":{  
> > > "stats":{  
> > > "statistical":{  
> > > "field":"title"}  
> > > }  
> > > }
> > > 
> > > returns me exception because the facet expects a number field. How can I  
> > > achieve my goal? Thanks!
> > > 
> > > --  
> > > You received this message because you are subscribed to the Google  
> > > Groups "elasticsearch" group.  
> > > To unsubscribe from this group and stop receiving emails from it, send  
> > > an email to [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com).
> > > 
> > > To view this discussion on the web visit [https://groups.google.com/d/](https://groups.google.com/d/)  
> > > msgid/elasticsearch/3c1a08f2-e467-47c3-a667-43837ac9b352%  
> > > [40googlegroups.com](http://40googlegroups.com)[https://groups.google.com/d/msgid/elasticsearch/3c1a08f2-e467-47c3-a667-43837ac9b352%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/3c1a08f2-e467-47c3-a667-43837ac9b352%40googlegroups.com?utm_medium=email&utm_source=footer)  
> > > .  
> > > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).
> > 
> > --  
> > You received this message because you are subscribed to the Google Groups  
> > "elasticsearch" group.  
> > To unsubscribe from this group and stop receiving emails from it, send an  
> > email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> > To view this discussion on the web visit  
> > [https://groups.google.com/d/msgid/elasticsearch/8bd3444c-5095-463c-adb9-60ea2eb42e57%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/8bd3444c-5095-463c-adb9-60ea2eb42e57%40googlegroups.com)[https://groups.google.com/d/msgid/elasticsearch/8bd3444c-5095-463c-adb9-60ea2eb42e57%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/8bd3444c-5095-463c-adb9-60ea2eb42e57%40googlegroups.com?utm_medium=email&utm_source=footer)  
> > .
> 
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CAGdPd5%3D-49cUg-nU9eyR6hkPd%2BisgR915hcH2ZuhtOC7Nm72JA%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAGdPd5%3D-49cUg-nU9eyR6hkPd%2BisgR915hcH2ZuhtOC7Nm72JA%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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 6, 2017, 1:36am UTC](https://discuss.elastic.co/t/statistical-facet-on-length-of-string-field/16974/7 "2017-07-06T01:36:09Z")

</div>


