Solr 3.6 New Feature Review: CurrencyField
Join the DZone community and get the full member experience.Join For Free
solr 3.6 will have an interesting new feature for currency handling called solr.currencyfield. why do we need this when you can just use float for currency handling? i this post you'll see why this feature is going to be useful.
lets start with the field type configuration, which is quite typical when it comes to solr. to the schema.xml file we need to add another field type declaration, like the following:
<fieldtype class="solr.currencyfield" name="currencyfield" defaultcurrency="usd" currencyconfig="currencyexchange.xml" />
in the above configuration we see two additional attributes (compared to the usual type definition) that define currencyfield behavior. first we see the defaultcurrency attribute, which defines the default currency for a given field. it defines in which form data will be written into the index (change of this attribute value requires reindexing). the second attribute, the currencyconfig defines an exchange file. it's worthwhile to remember that using the second parameter only makes sense with the default exchange provider ( fileexchangerateprovided ) distributed with solr. but let’s take a look at the currencyexchange.xml file:
exchange configuration file for fileexchangerateprovider
so now, lets look at the contents of the currencyexchange.xml file which provides the exchange rates for the default exchange provider:
<currencyconfig version="1.0"> <rates> <rate from="usd" to="pln" rate="3.1"/> <rate from="eur" to="pln" rate="2.5"/> <rate from="usd" to="eur" rate="2.5"/> </rates> </currencyconfig>
as you can see the structure is quite simple, we define the base currency ( from ), output currency ( to ) and the exchange rate ( rate ). so it’s quite simple
in order to index the data with the defined currencyfield we should specify the value and the currency prefixed with a comma character. for example:
querying is more or less like indexing. you have to pass two pieces of information to solr – the value and currency. let’s look at the two example filters using currency, the first one with a simple term query and the second one a range query:
fq=price:29.99,pln fq=price:[10.00 to 29.99,eur]
as you can see, after setting the value (or range) we have to specify the comma character and the currency we are interested in. whats more, we are not bound to the default currency – we can query other currencies as long as we have the exchange rate defined. this means that solr will make the exchange calculation for us. please remember, that for now, the values returned in the results will only contain the default currency and there is no way to change that behavior.
your own currency provider
in addition to the default exchange provider, solr will enable us to plug in our own implementation. in order to do that we need to develop a class that implements org.apache.solr.schema.exchangerateprovider interface and let solr know by passing the class name as the value of providerclass attribute defined for our currency type. assuming that we have the class pl.solr.schema.dynamicrateexchangeprovider that implements the mentioned interface and we would like to use that class, our field type definition would look something like that:
<fieldtype class="solr.currencyfield" name="currencyfield" defaultcurrency="usd" providerclass="pl.solr.schema.dynamicrateexchangeprovider" />
i like this feature as we are not bound to the xml exchange file, but we can develop our own implementation which for example use webservice to dynamically read data from some external source.
what’s left to implement ?
when the post was published you could not use range faceting on fields based on currencyfield type.
to sum up
in my opinion the currencyfield is a functionality worth waiting for. instead of calculating exchange in our application we can let solr do that for us. in addition to that we will be able to plug in our own implementations of exchange providers, which will enable us to write connectors to different systems and just watch how solr does all the work for us
Published at DZone with permission of Rafał Kuć, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.