# Terms\_stats facet

**URL:** <https://discuss.elastic.co/t/terms-stats-facet/7306>\
**Category:** Elasticsearch\
**Created:** [April 12, 2012, 11:23am UTC](https://discuss.elastic.co/t/terms-stats-facet/7306 "2012-04-12T11:23:27Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marcin\_Dojwa](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/marcin_dojwa/32/2307_2.png) [@Marcin\_Dojwa](https://discuss.elastic.co/u/Marcin_Dojwa)\
**Post date:** [April 12, 2012, 11:23am UTC](https://discuss.elastic.co/t/terms-stats-facet/7306/1 "2012-04-12T11:23:27Z")

</div>

Hi,

I am not sure if this is a bug or it just works like this. Lets take an  
example:  
curl -XPUT '[http://localhost:9200/test1](http://localhost:9200/test1)'  
curl -XPUT '[http://localhost:9200/test1/type1/1](http://localhost:9200/test1/type1/1)' -d '{"obj1" : [{"name" :  
"blue","count" : 4},{"name" : "green","count" : 6}]}'  
curl -XPOST '[http://localhost:9200/test1/type1/\_search?pretty=1](http://localhost:9200/test1/type1/_search?pretty=1)' -d  
'{"query": {"match\_all": {}},"facets": {"facet1": {"terms\_stats":  
{"key\_field" : "obj1.name","value\_field": "obj1.count"}}}}'  
This returns:  
{  
"took":1,  
"timed\_out":false,  
"\_shards":{  
"total":5,  
"successful":5,  
"failed":0  
},  
"hits":{  
"total":1,  
"max\_score":1.0,  
"hits":[  
{  
"\_index":"test1",  
"\_type":"type1",  
"\_id":"1",  
"\_score":1.0,  
"\_source":{  
"obj1":[  
{  
"name":"blue",  
"count":4  
},  
{  
"name":"green",  
"count":6  
}  
]  
}  
}  
]  
},  
"facets":{  
"facet1":{  
"\_type":"terms\_stats",  
"missing":0,  
"terms":[  
{  
"term":"green",  
"count":1,  
"total\_count":2,  
"min":4.0,  
"max":6.0,  
"total":10.0,  
"mean":5.0  
},  
{  
"term":"blue",  
"count":1,  
"total\_count":2,  
"min":4.0,  
"max":6.0,  
"total":10.0,  
"mean":5.0  
}  
]  
}  
}  
}

I have a question about this  
{  
"term":"green",  
"count":1,  
"total\_count":2,  
"min":4.0,  
"max":6.0,  
"total":10.0,  
"mean":5.0  
},  
{  
"term":"blue",  
"count":1,  
"total\_count":2,  
"min":4.0,  
"max":6.0,  
"total":10.0,  
"mean":5.0  
}  
Why total\_count, min, max, total and mean are calculated for whole document  
obj1 and not just for specified terms (green, blue) separately ?

When obj1 has nested type and the request is:  
curl -XPOST '[http://localhost:9200/test/type1/\_search?pretty=1](http://localhost:9200/test/type1/_search?pretty=1)' -d  
'{"query": {"match\_all": {}},"facets": {"facet1": {"terms\_stats":  
{"key\_field" : "name","value\_field": "count"}},"nested":"obj1"}}'  
the answer is OK:  
{  
"took":0,  
"timed\_out":false,  
"\_shards":{  
"total":5,  
"successful":5,  
"failed":0  
},  
"hits":{  
"total":1,  
"max\_score":1.0,  
"hits":[  
{  
"\_index":"test",  
"\_type":"type1",  
"\_id":"1",  
"\_score":1.0,  
"\_source":{  
"obj1":[  
{  
"name":"blue",  
"count":4  
},  
{  
"name":"green",  
"count":6  
}  
]  
}  
}  
]  
},  
"facets":{  
"facet1":{  
"\_type":"terms\_stats",  
"missing":0,  
"terms":[  
{  
"term":"green",  
"count":1,  
"total\_count":1,  
"min":6.0,  
"max":6.0,  
"total":6.0,  
"mean":6.0  
},  
{  
"term":"blue",  
"count":1,  
"total\_count":1,  
"min":4.0,  
"max":4.0,  
"total":4.0,  
"mean":4.0  
}  
]  
}  
}  
}

But the question is why in the first case is like above. Is it a bug or a  
feature ? 🙂

Thanks.  
Best regards.  
Marcin.

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [April 13, 2012, 12:19pm UTC](https://discuss.elastic.co/t/terms-stats-facet/7306/2 "2012-04-13T12:19:21Z")

</div>

First, gist samples, its hard to read it in the mail. The terms stats facet  
won't work properly when both the key and the value are multi valued within  
the document. You can use nested mapping and nested scope facet to treat  
each object in the array as its own "document".

On Thu, Apr 12, 2012 at 2:23 PM, Marcin Dojwa [m.dojwa@livechatinc.com](mailto:m.dojwa@livechatinc.com)wrote:

> Hi,
> 
> I am not sure if this is a bug or it just works like this. Lets take an  
> example:  
> curl -XPUT '[http://localhost:9200/test1](http://localhost:9200/test1)'  
> curl -XPUT '[http://localhost:9200/test1/type1/1](http://localhost:9200/test1/type1/1)' -d '{"obj1" : [{"name" :  
> "blue","count" : 4},{"name" : "green","count" : 6}]}'  
> curl -XPOST '[http://localhost:9200/test1/type1/\_search?pretty=1](http://localhost:9200/test1/type1/_search?pretty=1)' -d  
> '{"query": {"match\_all": {}},"facets": {"facet1": {"terms\_stats":  
> {"key\_field" : "obj1.name","value\_field": "obj1.count"}}}}'  
> This returns:  
> {  
> "took":1,  
> "timed\_out":false,  
> "\_shards":{  
> "total":5,  
> "successful":5,  
> "failed":0  
> },  
> "hits":{  
> "total":1,  
> "max\_score":1.0,  
> "hits":[  
> {  
> "\_index":"test1",  
> "\_type":"type1",  
> "\_id":"1",  
> "\_score":1.0,  
> "\_source":{  
> "obj1":[  
> {  
> "name":"blue",  
> "count":4  
> },  
> {  
> "name":"green",  
> "count":6  
> }  
> ]  
> }  
> }  
> ]  
> },  
> "facets":{  
> "facet1":{  
> "\_type":"terms\_stats",  
> "missing":0,  
> "terms":[  
> {  
> "term":"green",  
> "count":1,  
> "total\_count":2,  
> "min":4.0,  
> "max":6.0,  
> "total":10.0,  
> "mean":5.0  
> },  
> {  
> "term":"blue",  
> "count":1,  
> "total\_count":2,  
> "min":4.0,  
> "max":6.0,  
> "total":10.0,  
> "mean":5.0  
> }  
> ]  
> }  
> }  
> }
> 
> I have a question about this  
> {  
> "term":"green",  
> "count":1,  
> "total\_count":2,  
> "min":4.0,  
> "max":6.0,  
> "total":10.0,  
> "mean":5.0  
> },  
> {  
> "term":"blue",  
> "count":1,  
> "total\_count":2,  
> "min":4.0,  
> "max":6.0,  
> "total":10.0,  
> "mean":5.0  
> }  
> Why total\_count, min, max, total and mean are calculated for whole  
> document obj1 and not just for specified terms (green, blue) separately ?
> 
> When obj1 has nested type and the request is:  
> curl -XPOST '[http://localhost:9200/test/type1/\_search?pretty=1](http://localhost:9200/test/type1/_search?pretty=1)' -d  
> '{"query": {"match\_all": {}},"facets": {"facet1": {"terms\_stats":  
> {"key\_field" : "name","value\_field": "count"}},"nested":"obj1"}}'  
> the answer is OK:  
> {  
> "took":0,  
> "timed\_out":false,  
> "\_shards":{  
> "total":5,  
> "successful":5,  
> "failed":0  
> },  
> "hits":{  
> "total":1,  
> "max\_score":1.0,  
> "hits":[  
> {  
> "\_index":"test",  
> "\_type":"type1",  
> "\_id":"1",  
> "\_score":1.0,  
> "\_source":{  
> "obj1":[  
> {  
> "name":"blue",  
> "count":4  
> },  
> {  
> "name":"green",  
> "count":6  
> }  
> ]  
> }  
> }  
> ]  
> },  
> "facets":{  
> "facet1":{  
> "\_type":"terms\_stats",  
> "missing":0,  
> "terms":[  
> {  
> "term":"green",  
> "count":1,  
> "total\_count":1,  
> "min":6.0,  
> "max":6.0,  
> "total":6.0,  
> "mean":6.0  
> },  
> {  
> "term":"blue",  
> "count":1,  
> "total\_count":1,  
> "min":4.0,  
> "max":4.0,  
> "total":4.0,  
> "mean":4.0  
> }  
> ]  
> }  
> }  
> }
> 
> But the question is why in the first case is like above. Is it a bug or a  
> feature ? 🙂
> 
> Thanks.  
> Best regards.  
> Marcin.

---

<div class="post-metadata">

**Author:** ![Marcin\_Dojwa](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/marcin_dojwa/32/2307_2.png) [@Marcin\_Dojwa](https://discuss.elastic.co/u/Marcin_Dojwa)\
**Post date:** [April 14, 2012, 10:55am UTC](https://discuss.elastic.co/t/terms-stats-facet/7306/3 "2012-04-14T10:55:07Z")

</div>

Hi,

Sorry for but I do not know what is "gist sample", could you explain ? 🙂  
No I get how the terms facets work with multi valued fields and this seems  
to be even advantage to me.

I have one question about nested fields: is it possible to access document  
fields from nested scope facet ? For example (example document):  
{  
"name":"sample name",  
"tags":[  
{  
"name":"tag1",  
"value":"value1"  
},  
{  
"name":"tag2",  
"value":"value2"  
}  
]  
}  
Lets asume that 'tags' is a nested field. Is it possible to access 'name'  
field from nested scope facet for 'tags' ? I mean if it is possible to  
construct such a facet for 'tags' that includes "term" filter checking  
field 'name' to count documents with given value for 'name' only? As far as  
I know it is not possible but I want to be sure I don't miss something.

Thanks.  
Best regards.

2012/4/13 Shay Banon [kimchy@gmail.com](mailto:kimchy@gmail.com)

> First, gist samples, its hard to read it in the mail. The terms stats  
> facet won't work properly when both the key and the value are multi valued  
> within the document. You can use nested mapping and nested scope facet to  
> treat each object in the array as its own "document".
> 
> On Thu, Apr 12, 2012 at 2:23 PM, Marcin Dojwa [m.dojwa@livechatinc.com](mailto:m.dojwa@livechatinc.com)wrote:
> 
> > Hi,
> > 
> > I am not sure if this is a bug or it just works like this. Lets take an  
> > example:  
> > curl -XPUT '[http://localhost:9200/test1](http://localhost:9200/test1)'  
> > curl -XPUT '[http://localhost:9200/test1/type1/1](http://localhost:9200/test1/type1/1)' -d '{"obj1" : [{"name"  
> > : "blue","count" : 4},{"name" : "green","count" : 6}]}'  
> > curl -XPOST '[http://localhost:9200/test1/type1/\_search?pretty=1](http://localhost:9200/test1/type1/_search?pretty=1)' -d  
> > '{"query": {"match\_all": {}},"facets": {"facet1": {"terms\_stats":  
> > {"key\_field" : "obj1.name","value\_field": "obj1.count"}}}}'  
> > This returns:  
> > {  
> > "took":1,  
> > "timed\_out":false,  
> > "\_shards":{  
> > "total":5,  
> > "successful":5,  
> > "failed":0  
> > },  
> > "hits":{  
> > "total":1,  
> > "max\_score":1.0,  
> > "hits":[  
> > {  
> > "\_index":"test1",  
> > "\_type":"type1",  
> > "\_id":"1",  
> > "\_score":1.0,  
> > "\_source":{  
> > "obj1":[  
> > {  
> > "name":"blue",  
> > "count":4  
> > },  
> > {  
> > "name":"green",  
> > "count":6  
> > }  
> > ]  
> > }  
> > }  
> > ]  
> > },  
> > "facets":{  
> > "facet1":{  
> > "\_type":"terms\_stats",  
> > "missing":0,  
> > "terms":[  
> > {  
> > "term":"green",  
> > "count":1,  
> > "total\_count":2,  
> > "min":4.0,  
> > "max":6.0,  
> > "total":10.0,  
> > "mean":5.0  
> > },  
> > {  
> > "term":"blue",  
> > "count":1,  
> > "total\_count":2,  
> > "min":4.0,  
> > "max":6.0,  
> > "total":10.0,  
> > "mean":5.0  
> > }  
> > ]  
> > }  
> > }  
> > }
> > 
> > I have a question about this  
> > {  
> > "term":"green",  
> > "count":1,  
> > "total\_count":2,  
> > "min":4.0,  
> > "max":6.0,  
> > "total":10.0,  
> > "mean":5.0  
> > },  
> > {  
> > "term":"blue",  
> > "count":1,  
> > "total\_count":2,  
> > "min":4.0,  
> > "max":6.0,  
> > "total":10.0,  
> > "mean":5.0  
> > }  
> > Why total\_count, min, max, total and mean are calculated for whole  
> > document obj1 and not just for specified terms (green, blue) separately ?
> > 
> > When obj1 has nested type and the request is:  
> > curl -XPOST '[http://localhost:9200/test/type1/\_search?pretty=1](http://localhost:9200/test/type1/_search?pretty=1)' -d  
> > '{"query": {"match\_all": {}},"facets": {"facet1": {"terms\_stats":  
> > {"key\_field" : "name","value\_field": "count"}},"nested":"obj1"}}'  
> > the answer is OK:  
> > {  
> > "took":0,  
> > "timed\_out":false,  
> > "\_shards":{  
> > "total":5,  
> > "successful":5,  
> > "failed":0  
> > },  
> > "hits":{  
> > "total":1,  
> > "max\_score":1.0,  
> > "hits":[  
> > {  
> > "\_index":"test",  
> > "\_type":"type1",  
> > "\_id":"1",  
> > "\_score":1.0,  
> > "\_source":{  
> > "obj1":[  
> > {  
> > "name":"blue",  
> > "count":4  
> > },  
> > {  
> > "name":"green",  
> > "count":6  
> > }  
> > ]  
> > }  
> > }  
> > ]  
> > },  
> > "facets":{  
> > "facet1":{  
> > "\_type":"terms\_stats",  
> > "missing":0,  
> > "terms":[  
> > {  
> > "term":"green",  
> > "count":1,  
> > "total\_count":1,  
> > "min":6.0,  
> > "max":6.0,  
> > "total":6.0,  
> > "mean":6.0  
> > },  
> > {  
> > "term":"blue",  
> > "count":1,  
> > "total\_count":1,  
> > "min":4.0,  
> > "max":4.0,  
> > "total":4.0,  
> > "mean":4.0  
> > }  
> > ]  
> > }  
> > }  
> > }
> > 
> > But the question is why in the first case is like above. Is it a bug or a  
> > feature ? 🙂
> > 
> > Thanks.  
> > Best regards.  
> > Marcin.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [April 14, 2012, 4:14pm UTC](https://discuss.elastic.co/t/terms-stats-facet/7306/4 "2012-04-14T16:14:45Z")

</div>

Everything about gist is here : [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/help/)  
[http://www.elasticsearch.org/help/](http://www.elasticsearch.org/help/)

David.

Le 14 avril 2012 à 12:55, Marcin Dojwa [m.dojwa@livechatinc.com](mailto:m.dojwa@livechatinc.com) a écrit :

> Sorry for but I do not know what is "gist sample", could you explain ? 🙂  
> No I get how the terms facets work with multi valued fields and this seems  
> to be even advantage to me.
> 
> I have one question about nested fields: is it possible to access document  
> fields from nested scope facet ? For example (example document):  
> {  
> "name":"sample name",  
> "tags":[  
> {  
> "name":"tag1",  
> "value":"value1"  
> },  
> {  
> "name":"tag2",  
> "value":"value2"  
> }  
> ]  
> }  
> Lets asume that 'tags' is a nested field. Is it possible to access 'name'  
> field from nested scope facet for 'tags' ? I mean if it is possible to  
> construct such a facet for 'tags' that includes "term" filter checking  
> field 'name' to count documents with given value for 'name' only? As far as  
> I know it is not possible but I want to be sure I don't miss something.
> 
> Thanks.  
> Best regards.
> 
> 2012/4/13 Shay Banon \< [kimchy@gmail.com](mailto:kimchy@gmail.com) [mailto:kimchy@gmail.com](mailto:kimchy@gmail.com) \>
> 
> > First, gist samples, its hard to read it in the mail. The terms stats facet  
> > won't work properly when both the key and the value are multi valued within  
> > the document. You can use nested mapping and nested scope facet to treat  
> > each object in the array as its own "document".
> > 
> > On Thu, Apr 12, 2012 at 2:23 PM, Marcin Dojwa \< [m.dojwa@livechatinc.com](mailto:m.dojwa@livechatinc.com)  
> > [mailto:m.dojwa@livechatinc.com](mailto:m.dojwa@livechatinc.com) \> wrote:
> > 
> > > I am not sure if this is a bug or it just works like this. Lets take an  
> > > example:  
> > > curl -XPUT ' [http://localhost:9200/test1](http://localhost:9200/test1) [http://localhost:9200/test1](http://localhost:9200/test1) '  
> > > curl -XPUT ' [http://localhost:9200/test1/type1/1](http://localhost:9200/test1/type1/1)  
> > > [http://localhost:9200/test1/type1/1](http://localhost:9200/test1/type1/1) ' -d '{"obj1" : [{"name" :  
> > > "blue","count" : 4},{"name" : "green","count" : 6}]}'  
> > > curl -XPOST ' [http://localhost:9200/test1/type1/\_search?pretty=1](http://localhost:9200/test1/type1/_search?pretty=1)  
> > > [http://localhost:9200/test1/type1/\_search?pretty=1](http://localhost:9200/test1/type1/_search?pretty=1) ' -d '{"query":  
> > > {"match\_all": {}},"facets": {"facet1": {"terms\_stats": {"key\_field" : "  
> > > obj1.name [http://obj1.name](http://obj1.name) ","value\_field": "obj1.count"}}}}'  
> > > This returns:  
> > > {  
> > > "took" : 1 ,  
> > > "timed\_out" : false ,  
> > > "\_shards" : {  
> > > "total" : 5 ,  
> > > "successful" : 5 ,  
> > > "failed" : 0  
> > > } ,  
> > > "hits" : {  
> > > "total" : 1 ,  
> > > "max\_score" : 1.0 ,  
> > > "hits" : [  
> > > {  
> > > "\_index" : "test1" ,  
> > > "\_type" : "type1" ,  
> > > "\_id" : "1" ,  
> > > "\_score" : 1.0 ,  
> > > "\_source" : {  
> > > "obj1" : [  
> > > {  
> > > "name" : "blue" ,  
> > > "count" : 4  
> > > } ,  
> > > {  
> > > "name" : "green" ,  
> > > "count" : 6  
> > > }  
> > > ]  
> > > }  
> > > }  
> > > ]  
> > > } ,  
> > > "facets" : {  
> > > "facet1" : {  
> > > "\_type" : "terms\_stats" ,  
> > > "missing" : 0 ,  
> > > "terms" : [  
> > > {  
> > > "term" : "green" ,  
> > > "count" : 1 ,  
> > > "total\_count" : 2 ,  
> > > "min" : 4.0 ,  
> > > "max" : 6.0 ,  
> > > "total" : 10.0 ,  
> > > "mean" : 5.0  
> > > } ,  
> > > {  
> > > "term" : "blue" ,  
> > > "count" : 1 ,  
> > > "total\_count" : 2 ,  
> > > "min" : 4.0 ,  
> > > "max" : 6.0 ,  
> > > "total" : 10.0 ,  
> > > "mean" : 5.0  
> > > }  
> > > ]  
> > > }  
> > > }  
> > > }
> > > 
> > > I have a question about this  
> > > {  
> > > "term" : "green" ,  
> > > "count" : 1 ,  
> > > "total\_count" : 2 ,  
> > > "min" : 4.0 ,  
> > > "max" : 6.0 ,  
> > > "total" : 10.0 ,  
> > > "mean" : 5.0  
> > > } ,  
> > > {  
> > > "term" : "blue" ,  
> > > "count" : 1 ,  
> > > "total\_count" : 2 ,  
> > > "min" : 4.0 ,  
> > > "max" : 6.0 ,  
> > > "total" : 10.0 ,  
> > > "mean" : 5.0  
> > > }  
> > > Why total\_count, min, max, total and mean are calculated for whole  
> > > document  
> > > obj1 and not just for specified terms (green, blue) separately ?
> > > 
> > > When obj1 has nested type and the request is:  
> > > curl -XPOST ' [http://localhost:9200/test/type1/\_search?pretty=1](http://localhost:9200/test/type1/_search?pretty=1)  
> > > [http://localhost:9200/test/type1/\_search?pretty=1](http://localhost:9200/test/type1/_search?pretty=1) ' -d '{"query":  
> > > {"match\_all": {}},"facets": {"facet1": {"terms\_stats": {"key\_field" :  
> > > "name","value\_field": "count"}},"nested":"obj1"}}'  
> > > the answer is OK:  
> > > {  
> > > "took" : 0 ,  
> > > "timed\_out" : false ,  
> > > "\_shards" : {  
> > > "total" : 5 ,  
> > > "successful" : 5 ,  
> > > "failed" : 0  
> > > } ,  
> > > "hits" : {  
> > > "total" : 1 ,  
> > > "max\_score" : 1.0 ,  
> > > "hits" : [  
> > > {  
> > > "\_index" : "test" ,  
> > > "\_type" : "type1" ,  
> > > "\_id" : "1" ,  
> > > "\_score" : 1.0 ,  
> > > "\_source" : {  
> > > "obj1" : [  
> > > {  
> > > "name" : "blue" ,  
> > > "count" : 4  
> > > } ,  
> > > {  
> > > "name" : "green" ,  
> > > "count" : 6  
> > > }  
> > > ]  
> > > }  
> > > }  
> > > ]  
> > > } ,  
> > > "facets" : {  
> > > "facet1" : {  
> > > "\_type" : "terms\_stats" ,  
> > > "missing" : 0 ,  
> > > "terms" : [  
> > > {  
> > > "term" : "green" ,  
> > > "count" : 1 ,  
> > > "total\_count" : 1 ,  
> > > "min" : 6.0 ,  
> > > "max" : 6.0 ,  
> > > "total" : 6.0 ,  
> > > "mean" : 6.0  
> > > } ,  
> > > {  
> > > "term" : "blue" ,  
> > > "count" : 1 ,  
> > > "total\_count" : 1 ,  
> > > "min" : 4.0 ,  
> > > "max" : 4.0 ,  
> > > "total" : 4.0 ,  
> > > "mean" : 4.0  
> > > }  
> > > ]  
> > > }  
> > > }  
> > > }
> > > 
> > > But the question is why in the first case is like above. Is it a bug or a  
> > > feature ? 🙂
> > > 
> > > Thanks.  
> > > Best regards.  
> > > Marcin.

--  
David Pilato  
[http://dev.david.pilato.fr/](http://dev.david.pilato.fr/)  
Twitter : @dadoonet

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [April 17, 2012, 8:07am UTC](https://discuss.elastic.co/t/terms-stats-facet/7306/5 "2012-04-17T08:07:13Z")

</div>

When you say access name, what do you mean? As a script, or being able to  
compose a query that checks on name and some nested values? If its the  
latter, then yes, you can do it, a query against nested fields just need to  
be wrapped in a nested query.

On Sat, Apr 14, 2012 at 1:55 PM, Marcin Dojwa [m.dojwa@livechatinc.com](mailto:m.dojwa@livechatinc.com)wrote:

> Hi,
> 
> Sorry for but I do not know what is "gist sample", could you explain ? 🙂  
> No I get how the terms facets work with multi valued fields and this seems  
> to be even advantage to me.
> 
> I have one question about nested fields: is it possible to access document  
> fields from nested scope facet ? For example (example document):  
> {  
> "name":"sample name",  
> "tags":[  
> {  
> "name":"tag1",  
> "value":"value1"  
> },  
> {  
> "name":"tag2",  
> "value":"value2"  
> }  
> ]  
> }  
> Lets asume that 'tags' is a nested field. Is it possible to access 'name'  
> field from nested scope facet for 'tags' ? I mean if it is possible to  
> construct such a facet for 'tags' that includes "term" filter checking  
> field 'name' to count documents with given value for 'name' only? As far as  
> I know it is not possible but I want to be sure I don't miss something.
> 
> Thanks.  
> Best regards.
> 
> 2012/4/13 Shay Banon [kimchy@gmail.com](mailto:kimchy@gmail.com)
> 
> > First, gist samples, its hard to read it in the mail. The terms stats  
> > facet won't work properly when both the key and the value are multi valued  
> > within the document. You can use nested mapping and nested scope facet to  
> > treat each object in the array as its own "document".
> > 
> > On Thu, Apr 12, 2012 at 2:23 PM, Marcin Dojwa [m.dojwa@livechatinc.com](mailto:m.dojwa@livechatinc.com)wrote:
> > 
> > > Hi,
> > > 
> > > I am not sure if this is a bug or it just works like this. Lets take an  
> > > example:  
> > > curl -XPUT '[http://localhost:9200/test1](http://localhost:9200/test1)'  
> > > curl -XPUT '[http://localhost:9200/test1/type1/1](http://localhost:9200/test1/type1/1)' -d '{"obj1" : [{"name"  
> > > : "blue","count" : 4},{"name" : "green","count" : 6}]}'  
> > > curl -XPOST '[http://localhost:9200/test1/type1/\_search?pretty=1](http://localhost:9200/test1/type1/_search?pretty=1)' -d  
> > > '{"query": {"match\_all": {}},"facets": {"facet1": {"terms\_stats":  
> > > {"key\_field" : "obj1.name","value\_field": "obj1.count"}}}}'  
> > > This returns:  
> > > {  
> > > "took":1,  
> > > "timed\_out":false,  
> > > "\_shards":{  
> > > "total":5,  
> > > "successful":5,  
> > > "failed":0  
> > > },  
> > > "hits":{  
> > > "total":1,  
> > > "max\_score":1.0,  
> > > "hits":[  
> > > {  
> > > "\_index":"test1",  
> > > "\_type":"type1",  
> > > "\_id":"1",  
> > > "\_score":1.0,  
> > > "\_source":{  
> > > "obj1":[  
> > > {  
> > > "name":"blue",  
> > > "count":4  
> > > },  
> > > {  
> > > "name":"green",  
> > > "count":6  
> > > }  
> > > ]  
> > > }  
> > > }  
> > > ]  
> > > },  
> > > "facets":{  
> > > "facet1":{  
> > > "\_type":"terms\_stats",  
> > > "missing":0,  
> > > "terms":[  
> > > {  
> > > "term":"green",  
> > > "count":1,  
> > > "total\_count":2,  
> > > "min":4.0,  
> > > "max":6.0,  
> > > "total":10.0,  
> > > "mean":5.0  
> > > },  
> > > {  
> > > "term":"blue",  
> > > "count":1,  
> > > "total\_count":2,  
> > > "min":4.0,  
> > > "max":6.0,  
> > > "total":10.0,  
> > > "mean":5.0  
> > > }  
> > > ]  
> > > }  
> > > }  
> > > }
> > > 
> > > I have a question about this  
> > > {  
> > > "term":"green",  
> > > "count":1,  
> > > "total\_count":2,  
> > > "min":4.0,  
> > > "max":6.0,  
> > > "total":10.0,  
> > > "mean":5.0  
> > > },  
> > > {  
> > > "term":"blue",  
> > > "count":1,  
> > > "total\_count":2,  
> > > "min":4.0,  
> > > "max":6.0,  
> > > "total":10.0,  
> > > "mean":5.0  
> > > }  
> > > Why total\_count, min, max, total and mean are calculated for whole  
> > > document obj1 and not just for specified terms (green, blue) separately ?
> > > 
> > > When obj1 has nested type and the request is:  
> > > curl -XPOST '[http://localhost:9200/test/type1/\_search?pretty=1](http://localhost:9200/test/type1/_search?pretty=1)' -d  
> > > '{"query": {"match\_all": {}},"facets": {"facet1": {"terms\_stats":  
> > > {"key\_field" : "name","value\_field": "count"}},"nested":"obj1"}}'  
> > > the answer is OK:  
> > > {  
> > > "took":0,  
> > > "timed\_out":false,  
> > > "\_shards":{  
> > > "total":5,  
> > > "successful":5,  
> > > "failed":0  
> > > },  
> > > "hits":{  
> > > "total":1,  
> > > "max\_score":1.0,  
> > > "hits":[  
> > > {  
> > > "\_index":"test",  
> > > "\_type":"type1",  
> > > "\_id":"1",  
> > > "\_score":1.0,  
> > > "\_source":{  
> > > "obj1":[  
> > > {  
> > > "name":"blue",  
> > > "count":4  
> > > },  
> > > {  
> > > "name":"green",  
> > > "count":6  
> > > }  
> > > ]  
> > > }  
> > > }  
> > > ]  
> > > },  
> > > "facets":{  
> > > "facet1":{  
> > > "\_type":"terms\_stats",  
> > > "missing":0,  
> > > "terms":[  
> > > {  
> > > "term":"green",  
> > > "count":1,  
> > > "total\_count":1,  
> > > "min":6.0,  
> > > "max":6.0,  
> > > "total":6.0,  
> > > "mean":6.0  
> > > },  
> > > {  
> > > "term":"blue",  
> > > "count":1,  
> > > "total\_count":1,  
> > > "min":4.0,  
> > > "max":4.0,  
> > > "total":4.0,  
> > > "mean":4.0  
> > > }  
> > > ]  
> > > }  
> > > }  
> > > }
> > > 
> > > But the question is why in the first case is like above. Is it a bug or  
> > > a feature ? 🙂
> > > 
> > > Thanks.  
> > > Best regards.  
> > > Marcin.

---

<div class="post-metadata">

**Author:** ![Marcin\_Dojwa](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/marcin_dojwa/32/2307_2.png) [@Marcin\_Dojwa](https://discuss.elastic.co/u/Marcin_Dojwa)\
**Post date:** [April 17, 2012, 1:45pm UTC](https://discuss.elastic.co/t/terms-stats-facet/7306/6 "2012-04-17T13:45:44Z")

</div>

Hi,

I mean the latter one. Thank you for the answer Shay.

Best regards.  
Marcin

2012/4/17 Shay Banon [kimchy@gmail.com](mailto:kimchy@gmail.com)

> When you say access name, what do you mean? As a script, or being able to  
> compose a query that checks on name and some nested values? If its the  
> latter, then yes, you can do it, a query against nested fields just need to  
> be wrapped in a nested query.
> 
> On Sat, Apr 14, 2012 at 1:55 PM, Marcin Dojwa [m.dojwa@livechatinc.com](mailto:m.dojwa@livechatinc.com)wrote:
> 
> > Hi,
> > 
> > Sorry for but I do not know what is "gist sample", could you explain ? 🙂  
> > No I get how the terms facets work with multi valued fields and this  
> > seems to be even advantage to me.
> > 
> > I have one question about nested fields: is it possible to access  
> > document fields from nested scope facet ? For example (example document):  
> > {  
> > "name":"sample name",  
> > "tags":[  
> > {  
> > "name":"tag1",  
> > "value":"value1"  
> > },  
> > {  
> > "name":"tag2",  
> > "value":"value2"  
> > }  
> > ]  
> > }  
> > Lets asume that 'tags' is a nested field. Is it possible to access 'name'  
> > field from nested scope facet for 'tags' ? I mean if it is possible to  
> > construct such a facet for 'tags' that includes "term" filter checking  
> > field 'name' to count documents with given value for 'name' only? As far as  
> > I know it is not possible but I want to be sure I don't miss something.
> > 
> > Thanks.  
> > Best regards.
> > 
> > 2012/4/13 Shay Banon [kimchy@gmail.com](mailto:kimchy@gmail.com)
> > 
> > > First, gist samples, its hard to read it in the mail. The terms stats  
> > > facet won't work properly when both the key and the value are multi valued  
> > > within the document. You can use nested mapping and nested scope facet to  
> > > treat each object in the array as its own "document".
> > > 
> > > On Thu, Apr 12, 2012 at 2:23 PM, Marcin Dojwa [m.dojwa@livechatinc.com](mailto:m.dojwa@livechatinc.com)wrote:
> > > 
> > > > Hi,
> > > > 
> > > > I am not sure if this is a bug or it just works like this. Lets take an  
> > > > example:  
> > > > curl -XPUT '[http://localhost:9200/test1](http://localhost:9200/test1)'  
> > > > curl -XPUT '[http://localhost:9200/test1/type1/1](http://localhost:9200/test1/type1/1)' -d '{"obj1" :  
> > > > [{"name" : "blue","count" : 4},{"name" : "green","count" : 6}]}'  
> > > > curl -XPOST '[http://localhost:9200/test1/type1/\_search?pretty=1](http://localhost:9200/test1/type1/_search?pretty=1)' -d  
> > > > '{"query": {"match\_all": {}},"facets": {"facet1": {"terms\_stats":  
> > > > {"key\_field" : "obj1.name","value\_field": "obj1.count"}}}}'  
> > > > This returns:  
> > > > {  
> > > > "took":1,  
> > > > "timed\_out":false,  
> > > > "\_shards":{  
> > > > "total":5,  
> > > > "successful":5,  
> > > > "failed":0  
> > > > },  
> > > > "hits":{  
> > > > "total":1,  
> > > > "max\_score":1.0,  
> > > > "hits":[  
> > > > {  
> > > > "\_index":"test1",  
> > > > "\_type":"type1",  
> > > > "\_id":"1",  
> > > > "\_score":1.0,  
> > > > "\_source":{  
> > > > "obj1":[  
> > > > {  
> > > > "name":"blue",  
> > > > "count":4  
> > > > },  
> > > > {  
> > > > "name":"green",  
> > > > "count":6  
> > > > }  
> > > > ]  
> > > > }  
> > > > }  
> > > > ]  
> > > > },  
> > > > "facets":{  
> > > > "facet1":{  
> > > > "\_type":"terms\_stats",  
> > > > "missing":0,  
> > > > "terms":[  
> > > > {  
> > > > "term":"green",  
> > > > "count":1,  
> > > > "total\_count":2,  
> > > > "min":4.0,  
> > > > "max":6.0,  
> > > > "total":10.0,  
> > > > "mean":5.0  
> > > > },  
> > > > {  
> > > > "term":"blue",  
> > > > "count":1,  
> > > > "total\_count":2,  
> > > > "min":4.0,  
> > > > "max":6.0,  
> > > > "total":10.0,  
> > > > "mean":5.0  
> > > > }  
> > > > ]  
> > > > }  
> > > > }  
> > > > }
> > > > 
> > > > I have a question about this  
> > > > {  
> > > > "term":"green",  
> > > > "count":1,  
> > > > "total\_count":2,  
> > > > "min":4.0,  
> > > > "max":6.0,  
> > > > "total":10.0,  
> > > > "mean":5.0  
> > > > },  
> > > > {  
> > > > "term":"blue",  
> > > > "count":1,  
> > > > "total\_count":2,  
> > > > "min":4.0,  
> > > > "max":6.0,  
> > > > "total":10.0,  
> > > > "mean":5.0  
> > > > }  
> > > > Why total\_count, min, max, total and mean are calculated for whole  
> > > > document obj1 and not just for specified terms (green, blue) separately ?
> > > > 
> > > > When obj1 has nested type and the request is:  
> > > > curl -XPOST '[http://localhost:9200/test/type1/\_search?pretty=1](http://localhost:9200/test/type1/_search?pretty=1)' -d  
> > > > '{"query": {"match\_all": {}},"facets": {"facet1": {"terms\_stats":  
> > > > {"key\_field" : "name","value\_field": "count"}},"nested":"obj1"}}'  
> > > > the answer is OK:  
> > > > {  
> > > > "took":0,  
> > > > "timed\_out":false,  
> > > > "\_shards":{  
> > > > "total":5,  
> > > > "successful":5,  
> > > > "failed":0  
> > > > },  
> > > > "hits":{  
> > > > "total":1,  
> > > > "max\_score":1.0,  
> > > > "hits":[  
> > > > {  
> > > > "\_index":"test",  
> > > > "\_type":"type1",  
> > > > "\_id":"1",  
> > > > "\_score":1.0,  
> > > > "\_source":{  
> > > > "obj1":[  
> > > > {  
> > > > "name":"blue",  
> > > > "count":4  
> > > > },  
> > > > {  
> > > > "name":"green",  
> > > > "count":6  
> > > > }  
> > > > ]  
> > > > }  
> > > > }  
> > > > ]  
> > > > },  
> > > > "facets":{  
> > > > "facet1":{  
> > > > "\_type":"terms\_stats",  
> > > > "missing":0,  
> > > > "terms":[  
> > > > {  
> > > > "term":"green",  
> > > > "count":1,  
> > > > "total\_count":1,  
> > > > "min":6.0,  
> > > > "max":6.0,  
> > > > "total":6.0,  
> > > > "mean":6.0  
> > > > },  
> > > > {  
> > > > "term":"blue",  
> > > > "count":1,  
> > > > "total\_count":1,  
> > > > "min":4.0,  
> > > > "max":4.0,  
> > > > "total":4.0,  
> > > > "mean":4.0  
> > > > }  
> > > > ]  
> > > > }  
> > > > }  
> > > > }
> > > > 
> > > > But the question is why in the first case is like above. Is it a bug or  
> > > > a feature ? 🙂
> > > > 
> > > > Thanks.  
> > > > Best regards.  
> > > > Marcin.

---

<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, 3:32am UTC](https://discuss.elastic.co/t/terms-stats-facet/7306/7 "2017-07-06T03:32:14Z")

</div>


