Facet: define and use a function instead of the traditional count?


(didier.baquier) #1

Hi folks,

I am wondering if it is possible, for any kind of facets, to define and use
a function instead of the count ?

Quick example:

I have a term facet on a field which can be Paid or Unpaid, and I got the
number of hits.

term: Paid
count: 12

term: Unpaid
count: 10

I also have another field "amount", and I am wondering if I can define a
function to say : for each match, add the amount to the sum.

term: Paid
count: 120 (for an amount of 10)

term: Unpaid
count: 100

Thanks for your help

--
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.
For more options, visit https://groups.google.com/groups/opt_out.


(Ivan Brusic) #2

Look into using the value_script with the terms stats facet.

http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-facets-terms-stats-facet.html

--
Ivan

On Mon, Oct 14, 2013 at 2:16 PM, didier.baquier@gmail.com wrote:

Hi folks,

I am wondering if it is possible, for any kind of facets, to define and
use a function instead of the count ?

Quick example:

I have a term facet on a field which can be Paid or Unpaid, and I got the
number of hits.

term: Paid
count: 12

term: Unpaid
count: 10

I also have another field "amount", and I am wondering if I can define a
function to say : for each match, add the amount to the sum.

term: Paid
count: 120 (for an amount of 10)

term: Unpaid
count: 100

Thanks for your help

--
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.
For more options, visit https://groups.google.com/groups/opt_out.

--
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.
For more options, visit https://groups.google.com/groups/opt_out.


(didier.baquier) #3

Hi Ivan,

thanks for your help, now I got my termsStatsFacet but I don't know how to
do the sum:
termsStatsFacet("payment").keyField("payment.code").valueScript(
"doc['amount'].value")

any clue?

thanks

Le lundi 14 octobre 2013 14:23:16 UTC-7, Ivan Brusic a écrit :

Look into using the value_script with the terms stats facet.

http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-facets-terms-stats-facet.html

--
Ivan

On Mon, Oct 14, 2013 at 2:16 PM, <didier....@gmail.com <javascript:>>wrote:

Hi folks,

I am wondering if it is possible, for any kind of facets, to define and
use a function instead of the count ?

Quick example:

I have a term facet on a field which can be Paid or Unpaid, and I got the
number of hits.

term: Paid
count: 12

term: Unpaid
count: 10

I also have another field "amount", and I am wondering if I can define a
function to say : for each match, add the amount to the sum.

term: Paid
count: 120 (for an amount of 10)

term: Unpaid
count: 100

Thanks for your help

--
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 <javascript:>.
For more options, visit https://groups.google.com/groups/opt_out.

--
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.
For more options, visit https://groups.google.com/groups/opt_out.


(didier.baquier) #4

I am still blocked on that. I tried to use a param by doing something like
that (sum is my param) :
sum = sum + doc['amount'].value; return sum;

but it is not working and I am not really sure that's the good way to do it.

Does anyone can help?

Le lundi 14 octobre 2013 16:19:58 UTC-7, didier....@gmail.com a écrit :

Hi Ivan,

thanks for your help, now I got my termsStatsFacet but I don't know how to
do the sum:
termsStatsFacet("payment").keyField("payment.code").valueScript(
"doc['amount'].value")

any clue?

thanks

Le lundi 14 octobre 2013 14:23:16 UTC-7, Ivan Brusic a écrit :

Look into using the value_script with the terms stats facet.

http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-facets-terms-stats-facet.html

--
Ivan

On Mon, Oct 14, 2013 at 2:16 PM, didier....@gmail.com wrote:

Hi folks,

I am wondering if it is possible, for any kind of facets, to define and
use a function instead of the count ?

Quick example:

I have a term facet on a field which can be Paid or Unpaid, and I got
the number of hits.

term: Paid
count: 12

term: Unpaid
count: 10

I also have another field "amount", and I am wondering if I can define a
function to say : for each match, add the amount to the sum.

term: Paid
count: 120 (for an amount of 10)

term: Unpaid
count: 100

Thanks for your help

--
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.
For more options, visit https://groups.google.com/groups/opt_out.

--
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.
For more options, visit https://groups.google.com/groups/opt_out.


(didier.baquier) #5

I am a bit confused, the termsStatsFacet should return the sum in the
"total" field. but the total is wrong (the min and max are good).

as you can see below, there are 3 hits, but the stats are wrong (except for
the min and the max)

I am really confused about the "count" why it's 3 and 7?

here is, how I declare my facet:
search.addFacet(termsStatsFacet("payment_stats").keyField("statuses.payment.code").valueField("amount"));

here is the response from elasticsearch:

{
"took" : 3,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 3,
"max_score" : null,
"hits" : [ {
"_index" : "sales",
"_type" : "bill",
"_id" : "5201_12",
"_score" : null,
"fields" : {
"amount" : 2456.0,
"payment" : {
"code" : "not.paid"
},
....
},
"sort" : [ 1383609600000 ]
}, {
"_index" : "sales",
"_type" : "bill",
"_id" : "5203_12",
"_score" : null,
"fields" : {
"amount" : 314.96,
"payment" : {
"code" : "paid"
},
....
},
"sort" : [ -9223372036854775808 ]
}, {
"_index" : "sales",
"_type" : "bill",
"_id" : "5204_12",
"_score" : null,
"fields" : {
"amount" : 68.96,
"payment" : {
"code" : "paid"
},
....
},
"sort" : [ -9223372036854775808 ]
} ]
},
"facets" : {
"payment_stats" : {
"_type" : "terms_stats",
"missing" : 0,
"terms" : [ {
"term" : "paid",
"count" : 7,
"total_count" : 7,
"min" : 68.96,
"max" : 314.96,
"total" : 1466.72,
"mean" : 209.53142857142856
}, {
"term" : "not.paid",
"count" : 3,
"total_count" : 3,
"min" : 2456.0,
"max" : 2456.0,
"total" : 7368.0,
"mean" : 2456.0
} ]
}
}
}

If I declare a simple TermFacet I got the good count:
"payment" : {
"_type" : "terms",
"missing" : 0,
"total" : 3,
"other" : 0,
"terms" : [ {
"term" : "not.paid",
"count" : 1
}, {
"term" : "paid",
"count" : 2
} ]
}

thanks for your help

Le lundi 14 octobre 2013 16:19:58 UTC-7, didier....@gmail.com a écrit :

Hi Ivan,

thanks for your help, now I got my termsStatsFacet but I don't know how to
do the sum:
termsStatsFacet("payment").keyField("payment.code").valueScript(
"doc['amount'].value")

any clue?

thanks

Le lundi 14 octobre 2013 14:23:16 UTC-7, Ivan Brusic a écrit :

Look into using the value_script with the terms stats facet.

http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-facets-terms-stats-facet.html

--
Ivan

On Mon, Oct 14, 2013 at 2:16 PM, didier....@gmail.com wrote:

Hi folks,

I am wondering if it is possible, for any kind of facets, to define and
use a function instead of the count ?

Quick example:

I have a term facet on a field which can be Paid or Unpaid, and I got
the number of hits.

term: Paid
count: 12

term: Unpaid
count: 10

I also have another field "amount", and I am wondering if I can define a
function to say : for each match, add the amount to the sum.

term: Paid
count: 120 (for an amount of 10)

term: Unpaid
count: 100

Thanks for your help

--
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.
For more options, visit https://groups.google.com/groups/opt_out.

--
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.
For more options, visit https://groups.google.com/groups/opt_out.


(system) #6