Amount/Money mapping in Elasticsearch

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

--

1 Like

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

Where it defines a name as having a first_name and a last_name field.
This is different than you see at

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
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

--

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

--

--

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

--

--

1 Like