# Amount/Money mapping in Elasticsearch

**URL:** <https://discuss.elastic.co/t/amount-money-mapping-in-elasticsearch/8857>\
**Category:** Elasticsearch\
**Created:** [August 27, 2012, 9:21am UTC](https://discuss.elastic.co/t/amount-money-mapping-in-elasticsearch/8857 "2012-08-27T09:21:02Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael\_Kleen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/michael_kleen/32/2496_2.png) [@Michael\_Kleen](https://discuss.elastic.co/u/Michael_Kleen)\
**Post date:** [August 27, 2012, 9:21am UTC](https://discuss.elastic.co/t/amount-money-mapping-in-elasticsearch/8857/1 "2012-08-27T09:21:02Z")

</div>

Hello,

I am currently looking into the possibilities of mapping monetary types in  
elasticsearch. Unfortunately I couldnt find a designated module for it  
similar to [http://wiki.apache.org/solr/CurrencyField](http://wiki.apache.org/solr/CurrencyField).  
What is the standard approach in elasticsearch for mapping money ? As far  
as I understand a money type could be mapped as a nested document, but  
wouldn't create that additional overhead by storing each money item as a  
separate document ? Does it make sense to implement an own money type with  
an own mapping as elasticsearch plugin ? Are there any example of how to  
implement own types in elasticsearch using FieldData/DocFieldData/Mapper ?

Many thanks,

Michael

--

---

<div class="post-metadata">

**Author:** ![phill](https://avatars.discourse-cdn.com/v4/letter/p/779978/32.png) [@phill](https://discuss.elastic.co/u/phill)\
**Post date:** [August 28, 2012, 4:29pm UTC](https://discuss.elastic.co/t/amount-money-mapping-in-elasticsearch/8857/2 "2012-08-28T16:29:22Z")

</div>

On 8/27/2012 2:21 AM, Michael Kleen wrote:

> Hello,
> 
> I am currently looking into the possibilities of mapping monetary  
> types in elasticsearch. Unfortunately I couldnt find a designated  
> module for it similar to [CurrencyField - Solr - Apache Software Foundation](http://wiki.apache.org/solr/CurrencyField).  
> What is the standard approach in elasticsearch for mapping money ? As  
> far as I understand a money type could be mapped as a nested document,  
> but wouldn't create that additional overhead by storing each money  
> item as a separate document ? Does it make sense to implement an own  
> money type with an own mapping as elasticsearch plugin ? Are there  
> any example of how to implement own types in elasticsearch using  
> FieldData/DocFieldData/Mapper ?
> 
> Many thanks,
> 
> ## Michael

nested object is more than you need, since yes it is stored as a  
separate document.  
All you need is a field defined as an object not a nested object.  
Compare:

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

Where it defines a name as having a first\_name and a last\_name field.  
This is different than you see at

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

where the example is so generic it might not be obvious what is going on.  
But I believe the only _required_ difference is the type field at same  
level as properties, to convert a field  
that contains internal structure to a document that is separate, yet  
connected to the parent document. In a nested document you'll also want to  
set various other values at the same level of the JSON mapping  
properties to define how you'd like to your nested object to be seen in  
the parent document.

If I was modeling money, since I use Java, I'd use something like  
joda.money.Money  
[http://joda-money.sourceforge.net/apidocs/index.html?org/joda/money/Money.html](http://joda-money.sourceforge.net/apidocs/index.html?org/joda/money/Money.html)  
which means I'd need a value and currency unit field and then use  
Jackson to Json to/from Java.

"person": {  
...  
"name" : {  
"type" : "nested", \<-- set this to eliminate the "cross-object" problem. so searching for first\_name = "Paul" and last\_name ="Hill" finds me and not also Paul Vocker or Faith Hill  
"properties" : {  
"first\_name" : {"type" : "string"},  
"last\_name" : {"type" : "string"}  
}  
}  
...  
"annualSalary" : {  
"properties" : {  
"currency\_unit" : {"type" : "string"},  
"amount" : {"type" : "string"}  
}  
}  
...  
}

...  
}

HTH,  
-Paul

--

---

<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:** [August 29, 2012, 9:51pm UTC](https://discuss.elastic.co/t/amount-money-mapping-in-elasticsearch/8857/3 "2012-08-29T21:51:59Z")

</div>

Do you mean indexing multiple amounts per document, each in a different currency? If so, one additional simple option is to simply have "field\_usd", or "field\_euro".

If you want dynamic conversion of currencies, then you can do it on your end, and settle on a single currency you use for all money fields. Similar to what we do with dates where we convert all to UTC.

On Aug 27, 2012, at 12:21 PM, Michael Kleen [michael.kleen@numberfour.eu](mailto:michael.kleen@numberfour.eu) wrote:

> Hello,
> 
> I am currently looking into the possibilities of mapping monetary types in elasticsearch. Unfortunately I couldnt find a designated module for it similar to [CurrencyField - Solr - Apache Software Foundation](http://wiki.apache.org/solr/CurrencyField).  
> What is the standard approach in elasticsearch for mapping money ? As far as I understand a money type could be mapped as a nested document, but wouldn't create that additional overhead by storing each money item as a separate document ? Does it make sense to implement an own money type with an own mapping as elasticsearch plugin ? Are there any example of how to implement own types in elasticsearch using FieldData/DocFieldData/Mapper ?
> 
> Many thanks,
> 
> Michael
> 
> --

--

---

<div class="post-metadata">

**Author:** ![Jordan\_Sitkin](https://avatars.discourse-cdn.com/v4/letter/j/e19b73/32.png) [@Jordan\_Sitkin](https://discuss.elastic.co/u/Jordan_Sitkin)\
**Post date:** [October 4, 2012, 8:41pm UTC](https://discuss.elastic.co/t/amount-money-mapping-in-elasticsearch/8857/4 "2012-10-04T20:41:08Z")

</div>

Just wanted to "+1" this discussion as I am currently working through the  
same challenge.

The gist of it is that we are building a property listing site whose  
primary market is in a country that deals with several currencies. People  
submit their listing prices in one of 3 different currencies.

I understand your suggestion of choosing one currency for storage, but that  
simply isnt an option for us. The exchange rates between all the currencies  
are constantly changing, and we can't have a user enter an amount in one  
currency and then store/display it in a slightly different amount. We must  
store the values along with the currency they belong to. Of course this is  
not a problem with Times and Dates because the conversion to/from UTC never  
changes.

With this in mind, do you have any other ideas for solving this one?

the approach I'm trying now involves creating a filtered query with both  
AND and OR blocks, but I haven't figured out how to get the results I want  
yet.

On Wednesday, August 29, 2012 6:52:01 PM UTC-3, kimchy wrote:

> Do you mean indexing multiple amounts per document, each in a different  
> currency? If so, one additional simple option is to simply have  
> "field\_usd", or "field\_euro".
> 
> If you want dynamic conversion of currencies, then you can do it on your  
> end, and settle on a single currency you use for all money fields. Similar  
> to what we do with dates where we convert all to UTC.
> 
> On Aug 27, 2012, at 12:21 PM, Michael Kleen \<michae...@numberfour.eu\<javascript:\>\>  
> wrote:
> 
> > Hello,
> > 
> > I am currently looking into the possibilities of mapping monetary types  
> > in elasticsearch. Unfortunately I couldnt find a designated module for it  
> > similar to [CurrencyField - Solr - Apache Software Foundation](http://wiki.apache.org/solr/CurrencyField).  
> > What is the standard approach in elasticsearch for mapping money ? As  
> > far as I understand a money type could be mapped as a nested document, but  
> > wouldn't create that additional overhead by storing each money item as a  
> > separate document ? Does it make sense to implement an own money type with  
> > an own mapping as elasticsearch plugin ? Are there any example of how to  
> > implement own types in elasticsearch using FieldData/DocFieldData/Mapper ?
> > 
> > Many thanks,
> > 
> > Michael
> > 
> > --

--

---

<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:10am UTC](https://discuss.elastic.co/t/amount-money-mapping-in-elasticsearch/8857/5 "2017-07-06T03:10:03Z")

</div>


