Lucene TrieRangeQuery is a cool contrib in Lucene (think not yet in the official release) created by Uwe Schindler. I had heard about it before but learned about it in the LuceneMeetUp in ApacheCon EU. Uwe gave a great speach about it. As I found it a really useful feature will try to explain the basics.
TrieRangeQuery mainly sort out some RangeQuery problems:
- Tipical RangeQuery can end in TooManyClausesException if our ranges are so large.
- Tipical RangeQuery or even ConstantScoreRangeQuery are slow if have to classify using large ranges or the index is huge.
To explain it in an easy way, what TrieRangeQuery do is to search the data values skipping the less relevant “digits” in function of a precision parameter.
Let’s say for example we need to classify thousands of numbers of 6 figures. This could be a slow process using ConstantScoreRangeQuery in a huge index, not with TrieRangeQuery. Ranges will be divided recurively in function of a precision parámeter (set at index time). Numbers from the middle of the range will be classified using the minimum precision value while numbers from extrems will use a higher precision. This will make the query run extremely much faster.
Depending on the level of presicionStep parameter given at index time we will be able to search with more or less precision. The more precision marging we choose the more the lucene document will occuppy. It is due to we will have to index the field more times with the different precisions.
We need to index data in a special way to be able to search it using Lucene TrieRangeQuery. We must index our fields using TrieUtils. We can index numbers directly. It supports java signed int, long, float, double. There’s no loss of precision for doubles or floats. There’s no round for their creation, instead a long/int representation is used for cents.
Indexing numbers with TrieUtils will make us forget about maual padding.
We can index Dates aswell (from java timestamps data type).
As seen, Lucene TrieRangeQuery is totally a step forward for Lucene queries scalability.

