views:

1307

answers:

2

I'm using Rangequery to get all the documents which have amount between say 0 to 2. When i execute the query, Lucene gives me documents which have amount greater than 2 also...What am i missing here?

Here is my code: Term lowerTerm = new Term("amount", minAmount); Term upperTerm = new Term("amount", maxAmount);

            RangeQuery amountQuery = new RangeQuery(lowerTerm, upperTerm, true);

            finalQuery.Add(amountQuery, BooleanClause.Occur.MUST);

and here is what goes into my index:

doc.Add(new Field("amount", amount.ToString(), Field.Store.YES, Field.Index.UN_TOKENIZED, Field.TermVector.YES));

+2  A: 
itsadok
thanks a lot!...i already got this working ...
could you please let me know how do i use rangequery for decimal numbers?thanks!
For decimals (I assume you mean decimals with a fractional component) you need to scale them up, eg. by multiplying by a million, and removing any remainder: 1.2 -> 1200000. The amount you multiply by depends on how many decimal places you need to be accurate to.
RichieHindle
...and of course you still need to zero-pad them, as itsadok says. I should have said 1.2 -> 0001200000
RichieHindle
No need to scale up. Alphabetically, 1.234 comes before 1.3.
itsadok
Hi isadok,As you already know lat/long can be decimal/negative numbers.is below the correct way of indexing a city's lat and long positions in Lucene?string paddedLatitude = PadNumber(latitude);string paddedLongitude = PadNumber(longitude);doc.Add(new Field("latitude", paddedLatitude, Field.Store.YES, Field.Index.UN_TOKENIZED));doc.Add(new Field("longitude", paddedLongitude, Field.Store.YES, Field.Index.UN_TOKENIZED));example:paddedLongitude-->"0041.811846"paddedLongitude-->"-087.820628"Thanks.
That looks about OK. Please post this as a question, and I promise to answer.
itsadok
A: 

I created a PHP function that convert numerics to lucene/solr range searchables.

0.5 is converted to 10000000000.5
-0.5 is converted to 09999999999.5

function luceneNumeric($numeric)
{
    $negative = $numeric < 0;
    $numeric = $negative ? 10000000000 + $numeric : $numeric;

    $parts = explode('.', str_replace(',', '.', $numeric));

    $lucene = $negative ? 0 : 1;
    $lucene .= str_pad($parts[0], 10, '0', STR_PAD_LEFT);
    $lucene .= isset($parts[1]) ? '.' . $parts[1] : '';

    return $lucene;
}

It seems to work, hope this helps someone!

Znarkus