# Facets and sums for value\_script

**URL:** <https://discuss.elastic.co/t/facets-and-sums-for-value-script/17645>\
**Category:** Elasticsearch\
**Created:** [May 21, 2014, 3:51pm UTC](https://discuss.elastic.co/t/facets-and-sums-for-value-script/17645 "2014-05-21T15:51:16Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![kevins](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kevins/32/946_2.png) [@kevins](https://discuss.elastic.co/u/kevins)\
**Post date:** [May 21, 2014, 3:51pm UTC](https://discuss.elastic.co/t/facets-and-sums-for-value-script/17645/1 "2014-05-21T15:51:16Z")

</div>

My documents have a field called "debit" and SOME of them have a field  
called "credit".

I am interested in returning a facet that aggregates the debit + credit

I can do this for one or the other, but not both. Here is the query that I  
am using:

{  
"query" : {  
"match\_all" : { }  
},  
"sort" : [{ "credit" : {"order" : "desc"} }],  
"facets" : {  
"debit\_stats" : {  
"terms\_stats" : {  
"key\_field" : "origin",  
"value\_field" : "credit",  
"order": "term"  
}  
}  
}  
, size: 0  
}

I have tried to use a value\_script instead of a value\_field like this:

"value\_script" : "doc['credit'].value + doc['debit'].value",

Which gives me an error,

org.elasticsearch.index.fielddata.ScriptDocValues$Empty

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/971f422a-78bd-4b01-ab99-d893ed00104b%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/971f422a-78bd-4b01-ab99-d893ed00104b%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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, 1:27am UTC](https://discuss.elastic.co/t/facets-and-sums-for-value-script/17645/2 "2017-07-06T01:27:51Z")

</div>

Closed on July 6, 2017, 1:27am UTC


