# Statitical facets on length of the list

**URL:** <https://discuss.elastic.co/t/statitical-facets-on-length-of-the-list/10560>\
**Category:** Elasticsearch\
**Created:** [January 30, 2013, 6:07pm UTC](https://discuss.elastic.co/t/statitical-facets-on-length-of-the-list/10560 "2013-01-30T18:07:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![haizaar](https://avatars.discourse-cdn.com/v4/letter/h/3d9bf3/32.png) [@haizaar](https://discuss.elastic.co/u/haizaar)\
**Post date:** [January 30, 2013, 6:07pm UTC](https://discuss.elastic.co/t/statitical-facets-on-length-of-the-list/10560/1 "2013-01-30T18:07:10Z")

</div>

I have the following sample mapipng:

{  
"book" : {  
"properties" : {  
"author" : { "type" : "string" },  
"title" : { "type" : "string" },  
"reviews" : {  
"properties" : {  
"url" : { "type" : "string" },  
"score" : { "type" : "integer" }  
}  
},  
"chapters" : {  
"include\_in\_root" : 1,  
"type" : "nested",  
"properties" : {  
"name" : { "type" : "string" }  
}  
}  
}  
}  
}

I would like to get a facet on number of reviews - i.e. length of the  
"reviews" array.  
For instance, verbally spoken results I need are: "100 documents with 10  
reviews, 20 documents with 5 reviews, ..."

I'm trying the following statistical facet:  
{  
"query" : {  
"match\_all" : {}  
},  
"facets" : {  
"stat1" : {  
"statistical" : {"script" :  
"doc['reviews.score'].values.size()"}  
}  
}  
}

but it keeps failing with:  
{  
"error" : "SearchPhaseExecutionException[Failed to execute phase  
[query\_fetch], total failure; shardFailures  
{[mDsNfjLhRIyPObaOcxQo2w][facettest][0]:  
QueryPhaseExecutionException[[facettest][0]:  
query[ConstantScore(NotDeleted(cache(org.elasticsearch.index.search.nested.NonNestedDocsFilter@a2a5984b)))],from[0],size[10]:  
Query Failed [Failed to execute main query]]; nested:  
PropertyAccessException[[Error: could not access: reviews; in class:  
org.elasticsearch.search.lookup.DocLookup]  
[Near : {... doc[reviews.score].values.size() ....}]  
^  
[Line: 1, Column: 5]]; }]",  
"status" : 500  
}

How can I achieve my goal?

Thank you in advance.

Here is my example data:

{  
"author" : "Mark Twain",  
"title" : "The Adventures of Tom Sawyer",  
"reviews" : [  
{  
"url" : "[amazon.com](http://amazon.com)",  
"score" : 10  
},  
{  
"url" : "[www.barnesandnoble.com](http://www.barnesandnoble.com)",  
"score" : 9  
}  
],  
"chapters" : [  
{ "name" : "Chapter 1" }, { "name" : "Chapter 2" }  
]  
}

{  
"author" : "Jack London",  
"title" : "The Call of the Wild",  
"reviews" : [  
{  
"url" : "[amazon.com](http://amazon.com)",  
"score" : 8  
},  
{  
"url" : "[www.barnesandnoble.com](http://www.barnesandnoble.com)",  
"score" : 9  
},  
{  
"url" : "[www.books.com](http://www.books.com)",  
"score" : 5  
}  
],  
"chapters" : [  
{ "name" : "Chapter 1" }, { "name" : "Chapter 2" }  
]  
}

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![haizaar](https://avatars.discourse-cdn.com/v4/letter/h/3d9bf3/32.png) [@haizaar](https://discuss.elastic.co/u/haizaar)\
**Post date:** [January 30, 2013, 6:11pm UTC](https://discuss.elastic.co/t/statitical-facets-on-length-of-the-list/10560/2 "2013-01-30T18:11:34Z")

</div>

BTW, Elasticsearch version 0.19.9.

On Wednesday, January 30, 2013 8:07:10 PM UTC+2, Zaar Hai wrote:

> I have the following sample mapipng:
> 
> {  
> "book" : {  
> "properties" : {  
> "author" : { "type" : "string" },  
> "title" : { "type" : "string" },  
> "reviews" : {  
> "properties" : {  
> "url" : { "type" : "string" },  
> "score" : { "type" : "integer" }  
> }  
> },  
> "chapters" : {  
> "include\_in\_root" : 1,  
> "type" : "nested",  
> "properties" : {  
> "name" : { "type" : "string" }  
> }  
> }  
> }  
> }  
> }
> 
> I would like to get a facet on number of reviews - i.e. length of the  
> "reviews" array.  
> For instance, verbally spoken results I need are: "100 documents with 10  
> reviews, 20 documents with 5 reviews, ..."
> 
> I'm trying the following statistical facet:  
> {  
> "query" : {  
> "match\_all" : {}  
> },  
> "facets" : {  
> "stat1" : {  
> "statistical" : {"script" :  
> "doc['reviews.score'].values.size()"}  
> }  
> }  
> }
> 
> but it keeps failing with:  
> {  
> "error" : "SearchPhaseExecutionException[Failed to execute phase  
> [query\_fetch], total failure; shardFailures  
> {[mDsNfjLhRIyPObaOcxQo2w][facettest][0]:  
> QueryPhaseExecutionException[[facettest][0]:  
> query[ConstantScore(NotDeleted(cache(org.elasticsearch.index.search.nested.NonNestedDocsFilter@a2a5984b)))],from[0],size[10]:  
> Query Failed [Failed to execute main query]]; nested:  
> PropertyAccessException[[Error: could not access: reviews; in class:  
> org.elasticsearch.search.lookup.DocLookup]  
> [Near : {... doc[reviews.score].values.size() ....}]  
> ^  
> [Line: 1, Column: 5]]; }]",  
> "status" : 500  
> }
> 
> How can I achieve my goal?
> 
> Thank you in advance.
> 
> Here is my example data:
> 
> {  
> "author" : "Mark Twain",  
> "title" : "The Adventures of Tom Sawyer",  
> "reviews" : [  
> {  
> "url" : "[amazon.com](http://amazon.com)",  
> "score" : 10  
> },  
> {  
> "url" : "[www.barnesandnoble.com](http://www.barnesandnoble.com)",  
> "score" : 9  
> }  
> ],  
> "chapters" : [  
> { "name" : "Chapter 1" }, { "name" : "Chapter 2" }  
> ]  
> }
> 
> {  
> "author" : "Jack London",  
> "title" : "The Call of the Wild",  
> "reviews" : [  
> {  
> "url" : "[amazon.com](http://amazon.com)",  
> "score" : 8  
> },  
> {  
> "url" : "[www.barnesandnoble.com](http://www.barnesandnoble.com)",  
> "score" : 9  
> },  
> {  
> "url" : "[www.books.com](http://www.books.com)",  
> "score" : 5  
> }  
> ],  
> "chapters" : [  
> { "name" : "Chapter 1" }, { "name" : "Chapter 2" }  
> ]  
> }

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![haizaar](https://avatars.discourse-cdn.com/v4/letter/h/3d9bf3/32.png) [@haizaar](https://discuss.elastic.co/u/haizaar)\
**Post date:** [January 31, 2013, 8:31am UTC](https://discuss.elastic.co/t/statitical-facets-on-length-of-the-list/10560/3 "2013-01-31T08:31:55Z")

</div>

Here is the answer:

> <https://stackoverflow.com/questions/14610811/elasticserach-statistical-facets-on-length-of-the-list>

On Wednesday, January 30, 2013 8:11:34 PM UTC+2, Zaar Hai wrote:

> BTW, Elasticsearch version 0.19.9.
> 
> On Wednesday, January 30, 2013 8:07:10 PM UTC+2, Zaar Hai wrote:
> 
> > I have the following sample mapipng:
> > 
> > {  
> > "book" : {  
> > "properties" : {  
> > "author" : { "type" : "string" },  
> > "title" : { "type" : "string" },  
> > "reviews" : {  
> > "properties" : {  
> > "url" : { "type" : "string" },  
> > "score" : { "type" : "integer" }  
> > }  
> > },  
> > "chapters" : {  
> > "include\_in\_root" : 1,  
> > "type" : "nested",  
> > "properties" : {  
> > "name" : { "type" : "string" }  
> > }  
> > }  
> > }  
> > }  
> > }
> > 
> > I would like to get a facet on number of reviews - i.e. length of the  
> > "reviews" array.  
> > For instance, verbally spoken results I need are: "100 documents with 10  
> > reviews, 20 documents with 5 reviews, ..."
> > 
> > I'm trying the following statistical facet:  
> > {  
> > "query" : {  
> > "match\_all" : {}  
> > },  
> > "facets" : {  
> > "stat1" : {  
> > "statistical" : {"script" :  
> > "doc['reviews.score'].values.size()"}  
> > }  
> > }  
> > }
> > 
> > but it keeps failing with:  
> > {  
> > "error" : "SearchPhaseExecutionException[Failed to execute phase  
> > [query\_fetch], total failure; shardFailures  
> > {[mDsNfjLhRIyPObaOcxQo2w][facettest][0]:  
> > QueryPhaseExecutionException[[facettest][0]:  
> > query[ConstantScore(NotDeleted(cache(org.elasticsearch.index.search.nested.NonNestedDocsFilter@a2a5984b)))],from[0],size[10]:  
> > Query Failed [Failed to execute main query]]; nested:  
> > PropertyAccessException[[Error: could not access: reviews; in class:  
> > org.elasticsearch.search.lookup.DocLookup]  
> > [Near : {... doc[reviews.score].values.size() ....}]  
> > ^  
> > [Line: 1, Column: 5]]; }]",  
> > "status" : 500  
> > }
> > 
> > How can I achieve my goal?
> > 
> > Thank you in advance.
> > 
> > Here is my example data:
> > 
> > {  
> > "author" : "Mark Twain",  
> > "title" : "The Adventures of Tom Sawyer",  
> > "reviews" : [  
> > {  
> > "url" : "[amazon.com](http://amazon.com)",  
> > "score" : 10  
> > },  
> > {  
> > "url" : "[www.barnesandnoble.com](http://www.barnesandnoble.com)",  
> > "score" : 9  
> > }  
> > ],  
> > "chapters" : [  
> > { "name" : "Chapter 1" }, { "name" : "Chapter 2" }  
> > ]  
> > }
> > 
> > {  
> > "author" : "Jack London",  
> > "title" : "The Call of the Wild",  
> > "reviews" : [  
> > {  
> > "url" : "[amazon.com](http://amazon.com)",  
> > "score" : 8  
> > },  
> > {  
> > "url" : "[www.barnesandnoble.com](http://www.barnesandnoble.com)",  
> > "score" : 9  
> > },  
> > {  
> > "url" : "[www.books.com](http://www.books.com)",  
> > "score" : 5  
> > }  
> > ],  
> > "chapters" : [  
> > { "name" : "Chapter 1" }, { "name" : "Chapter 2" }  
> > ]  
> > }

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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, 2:53am UTC](https://discuss.elastic.co/t/statitical-facets-on-length-of-the-list/10560/4 "2017-07-06T02:53:50Z")

</div>


