# How to narrow scope for statistical facet?

**URL:** <https://discuss.elastic.co/t/how-to-narrow-scope-for-statistical-facet/7288>\
**Category:** Elasticsearch\
**Created:** [April 11, 2012, 12:04am UTC](https://discuss.elastic.co/t/how-to-narrow-scope-for-statistical-facet/7288 "2012-04-11T00:04:46Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shane\_Witbeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/shane_witbeck/32/2803_2.png) [@Shane\_Witbeck](https://discuss.elastic.co/u/Shane_Witbeck)\
**Post date:** [April 11, 2012, 12:04am UTC](https://discuss.elastic.co/t/how-to-narrow-scope-for-statistical-facet/7288/1 "2012-04-11T00:04:46Z")

</div>

I'm using the statistical facet to get the average value of a field. It  
currently returns stats based on all values in the index.

How would I filter or narrow the scope based on another field (a date field  
in my case)?

Thanks,  
Shane

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [April 11, 2012, 1:51am UTC](https://discuss.elastic.co/t/how-to-narrow-scope-for-statistical-facet/7288/2 "2012-04-11T01:51:56Z")

</div>

You can narrow the scope by using non match\_all { } query with your facet.  
For example, the following query will calculate facets only for record with  
date after Apr 1, 2012.

{  
"query" : {  
"query\_string" : {  
"query" : "date:[2012-04-01T00:00:00Z TO \*]"  
}  
},  
"size" : 0,  
"facets" : {  
"stat1" : {  
"statistical" : {  
"field" : "num1"  
}  
}  
}  
}

On Tuesday, April 10, 2012 8:04:46 PM UTC-4, Shane Witbeck wrote:

> I'm using the statistical facet to get the average value of a field. It  
> currently returns stats based on all values in the index.
> 
> How would I filter or narrow the scope based on another field (a date  
> field in my case)?
> 
> Thanks,  
> Shane

---

<div class="post-metadata">

**Author:** ![Shane\_Witbeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/shane_witbeck/32/2803_2.png) [@Shane\_Witbeck](https://discuss.elastic.co/u/Shane_Witbeck)\
**Post date:** [April 11, 2012, 2:05am UTC](https://discuss.elastic.co/t/how-to-narrow-scope-for-statistical-facet/7288/3 "2012-04-11T02:05:53Z")

</div>

Thanks Igor. I actually figured it out and I neglected to say that I need  
to narrow scope at the facet level since I have several facets in the same  
call. Here's what I did:

```
                    "query":{
                        "match_all":{}
                    },
                    "facets":{
                        "nps_stats_ytd":{
                            "statistical":{
                                "field":"nps.vote"
                            },
                            "facet_filter" : {
                                "query":{
                                    "query_string":{
                                        "query":"lastPostDate:[" + 

```

fmtYtd + " TO " + fmtTomorrow + "]"  
}  
}  
}  
},  
...more facets

On Tuesday, April 10, 2012 9:51:56 PM UTC-4, Igor Motov wrote:

> You can narrow the scope by using non match\_all { } query with your facet.  
> For example, the following query will calculate facets only for record with  
> date after Apr 1, 2012.
> 
> {  
> "query" : {  
> "query\_string" : {  
> "query" : "date:[2012-04-01T00:00:00Z TO \*]"  
> }  
> },  
> "size" : 0,  
> "facets" : {  
> "stat1" : {  
> "statistical" : {  
> "field" : "num1"  
> }  
> }  
> }  
> }
> 
> On Tuesday, April 10, 2012 8:04:46 PM UTC-4, Shane Witbeck wrote:
> 
> > I'm using the statistical facet to get the average value of a field. It  
> > currently returns stats based on all values in the index.
> > 
> > How would I filter or narrow the scope based on another field (a date  
> > field in my case)?
> > 
> > Thanks,  
> > Shane

---

<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 11, 2012, 12:26pm UTC](https://discuss.elastic.co/t/how-to-narrow-scope-for-statistical-facet/7288/4 "2012-04-11T12:26:51Z")

</div>

Yes, facet filter is the best option. Note, use range filter for that  
(which is cached), don't wrap a query\_string it in unless you really need  
to.

On Wed, Apr 11, 2012 at 5:05 AM, Shane Witbeck [shane@digitalsanctum.com](mailto:shane@digitalsanctum.com)wrote:

> Thanks Igor. I actually figured it out and I neglected to say that I need  
> to narrow scope at the facet level since I have several facets in the same  
> call. Here's what I did:
> 
> ```
> "query":{
> "match_all":{}
> },
> "facets":{
> "nps_stats_ytd":{
> "statistical":{
> "field":"nps.vote"
> },
> "facet_filter" : {
> "query":{
> "query_string":{
> "query":"lastPostDate:[" +
> 
> ```
> 
> fmtYtd + " TO " + fmtTomorrow + "]"  
> }  
> }  
> }  
> },  
> ...more facets
> 
> On Tuesday, April 10, 2012 9:51:56 PM UTC-4, Igor Motov wrote:
> 
> > You can narrow the scope by using non match\_all { } query with your  
> > facet. For example, the following query will calculate facets only for  
> > record with date after Apr 1, 2012.
> > 
> > {  
> > "query" : {  
> > "query\_string" : {  
> > "query" : "date:[2012-04-01T00:00:00Z TO \*]"  
> > }  
> > },  
> > "size" : 0,  
> > "facets" : {  
> > "stat1" : {  
> > "statistical" : {  
> > "field" : "num1"  
> > }  
> > }  
> > }  
> > }
> > 
> > On Tuesday, April 10, 2012 8:04:46 PM UTC-4, Shane Witbeck wrote:
> > 
> > > I'm using the statistical facet to get the average value of a field. It  
> > > currently returns stats based on all values in the index.
> > > 
> > > How would I filter or narrow the scope based on another field (a date  
> > > field in my case)?
> > > 
> > > Thanks,  
> > > Shane

---

<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/how-to-narrow-scope-for-statistical-facet/7288/5 "2017-07-06T03:32:57Z")

</div>

Closed on July 6, 2017, 3:32am UTC


