# XContentBuilder.field() method doesn't provide overload for BigDecimal

**URL:** <https://discuss.elastic.co/t/xcontentbuilder-field-method-doesnt-provide-overload-for-bigdecimal/8783>\
**Category:** Elasticsearch\
**Created:** [August 19, 2012, 2:40pm UTC](https://discuss.elastic.co/t/xcontentbuilder-field-method-doesnt-provide-overload-for-bigdecimal/8783 "2012-08-19T14:40:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jondow](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jondow/32/2111_2.png) [@Jondow](https://discuss.elastic.co/u/Jondow)\
**Post date:** [August 19, 2012, 2:40pm UTC](https://discuss.elastic.co/t/xcontentbuilder-field-method-doesnt-provide-overload-for-bigdecimal/8783/1 "2012-08-19T14:40:49Z")

</div>

I'm working on a Grails project that uses the ElasticSearch plugin for  
Grails. The author of the plugin has done a great job, but I've encountered  
an issue that I believe is rooted in the XContentBuilder class used to  
build the JSON to index my domain class.

In the plugin, the code determines a suitable marshaller for an object  
based on its class. It would appear that for most of the standard classes,  
this equates to using the plugins DefaultMarshaller, which effectively  
calls XContentBuilder.field(String name, \>Class of object\< object).

Unfortunately looking at the ES source, there is no field(String,  
BigDecimal) so it seems the default is called - field(String, Object),  
which simply calls toString() on the class and as a result my JSON renders  
a String instead of a numeric value for the BigDecimal, which is for a  
price field in my domain class. This causes the indexing to fail for some  
reason (which I haven't quite figured out what yet).

Is there a reason BigDecimal isn't overloaded? What approach should I use -  
I've forked the ES Grails plugin code and am happy to make whatever changes  
might be needed there and do a push request to the author.

Regards,  
Darryl Pentz

--

---

<div class="post-metadata">

**Author:** ![jprante](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jprante/32/44941_2.png) [@jprante](https://discuss.elastic.co/u/jprante)\
**Post date:** [August 20, 2012, 12:55pm UTC](https://discuss.elastic.co/t/xcontentbuilder-field-method-doesnt-provide-overload-for-bigdecimal/8783/2 "2012-08-20T12:55:35Z")

</div>

Yes, it seems the XContentBuilder needs some love. BigDecimal is not  
straightforward to output as it may produce a double or a string, whatever  
the user prefers. By default it should map to a double, to allow numeric  
range queries, and rounding should be like taught in school. For finer  
control, BigDecimal fields output should be parametrized by a scale and a  
rounding method (and a flag indicating what JSON field type is intended).

I opened a pull request:

> <https://github.com/elastic/elasticsearch/pull/2188>
>
> A pull request on demand for supporting BigDecimal in XContentBuilder
> https://gr…oups.google.com/forum/?fromgroups#!topic/elasticsearch/H74o1mNZXPk%5B1-25%5D

Best regards,

Jörg

On Sunday, August 19, 2012 4:40:49 PM UTC+2, Jondow wrote:

> I'm working on a Grails project that uses the Elasticsearch plugin for  
> Grails. The author of the plugin has done a great job, but I've encountered  
> an issue that I believe is rooted in the XContentBuilder class used to  
> build the JSON to index my domain class.
> 
> In the plugin, the code determines a suitable marshaller for an object  
> based on its class. It would appear that for most of the standard classes,  
> this equates to using the plugins DefaultMarshaller, which effectively  
> calls XContentBuilder.field(String name, \>Class of object\< object).
> 
> Unfortunately looking at the ES source, there is no field(String,  
> BigDecimal) so it seems the default is called - field(String, Object),  
> which simply calls toString() on the class and as a result my JSON renders  
> a String instead of a numeric value for the BigDecimal, which is for a  
> price field in my domain class. This causes the indexing to fail for some  
> reason (which I haven't quite figured out what yet).
> 
> Is there a reason BigDecimal isn't overloaded? What approach should I use
> 
> - I've forked the ES Grails plugin code and am happy to make whatever  
> changes might be needed there and do a push request to the author.
> 
> Regards,  
> Darryl Pentz

--

---

<div class="post-metadata">

**Author:** ![Jondow](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jondow/32/2111_2.png) [@Jondow](https://discuss.elastic.co/u/Jondow)\
**Post date:** [August 20, 2012, 1:25pm UTC](https://discuss.elastic.co/t/xcontentbuilder-field-method-doesnt-provide-overload-for-bigdecimal/8783/3 "2012-08-20T13:25:23Z")

</div>

Wow that looks great Jörg! Looks like that'll do the job perfectly. Look  
forward to the updated ES lib.

Warm Regards,  
Darryl

On Monday, 20 August 2012 14:55:35 UTC+2, Jörg Prante wrote:

> Yes, it seems the XContentBuilder needs some love. BigDecimal is not  
> straightforward to output as it may produce a double or a string, whatever  
> the user prefers. By default it should map to a double, to allow numeric  
> range queries, and rounding should be like taught in school. For finer  
> control, BigDecimal fields output should be parametrized by a scale and a  
> rounding method (and a flag indicating what JSON field type is intended).
> 
> I opened a pull request:  
> [mapping BigDecimal to double or string in XContentBuilder by jprante · Pull Request #2188 · elastic/elasticsearch · GitHub](https://github.com/elasticsearch/elasticsearch/pull/2188)
> 
> Best regards,
> 
> Jörg
> 
> On Sunday, August 19, 2012 4:40:49 PM UTC+2, Jondow wrote:
> 
> > I'm working on a Grails project that uses the Elasticsearch plugin for  
> > Grails. The author of the plugin has done a great job, but I've encountered  
> > an issue that I believe is rooted in the XContentBuilder class used to  
> > build the JSON to index my domain class.
> > 
> > In the plugin, the code determines a suitable marshaller for an object  
> > based on its class. It would appear that for most of the standard classes,  
> > this equates to using the plugins DefaultMarshaller, which effectively  
> > calls XContentBuilder.field(String name, \>Class of object\< object).
> > 
> > Unfortunately looking at the ES source, there is no field(String,  
> > BigDecimal) so it seems the default is called - field(String, Object),  
> > which simply calls toString() on the class and as a result my JSON renders  
> > a String instead of a numeric value for the BigDecimal, which is for a  
> > price field in my domain class. This causes the indexing to fail for some  
> > reason (which I haven't quite figured out what yet).
> > 
> > Is there a reason BigDecimal isn't overloaded? What approach should I use
> > 
> > - I've forked the ES Grails plugin code and am happy to make whatever  
> > changes might be needed there and do a push request to the author.
> > 
> > Regards,  
> > Darryl Pentz

--

---

<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:15am UTC](https://discuss.elastic.co/t/xcontentbuilder-field-method-doesnt-provide-overload-for-bigdecimal/8783/4 "2017-07-06T03:15:49Z")

</div>


