How to narrow scope for statistical facet?


(Shane Witbeck) #1

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


(Igor Motov) #2

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


(Shane Witbeck) #3

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


(Shay Banon) #4

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.comwrote:

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


(system) #5