Erik Hatcher <erik <at> ehatchersolutions.com> writes:

> Lucene's index works with any String.  But, when dealing with numbers 
> and dates such that range queries work, they need to be formatted in a 
> way that makes them orderable.

What I am suggesting here is storing numeric values as unsigned binary values so
that they can be used in a range query.  To convert a signed 2's complement
number to an unsigned number you just flip the left most bit.  The easiest way
to do this is to add the min value (eg Integer.MIN_VALUE).

Then to convert the integer to chars I would do somehing like this:

int unsigned = signed + Integer.MIN_VALUE;
char c2 = (char) (unsigned & 0x0000FFFF);
char c1 = (char) (unsigned >> 16 & 0x0000FFFF);
String intText new String(new char[] {c1, c2});

As I don't often think in binary I wrote a little test to verify that this is
correct according to String.compareTo() which is used during index creation to
sort the fields.

public class NumberCompareTest
{
  public static String convertTotText(int input)
  {
    int unsigned = input + Integer.MIN_VALUE;
    char c2 = (char) (unsigned & 0x0000FFFF);
    char c1 = (char) (unsigned >> 16 & 0x0000FFFF);
    return new String(new char[] {c1, c2});
  }
  
  public static void main(String[] args)
  {
    isLessThan(convertTotText(-1), convertTotText(0));
    isLessThan(convertTotText(0), convertTotText(1));
    isLessThan(convertTotText(Integer.MIN_VALUE), convertTotText(0));
    isLessThan(convertTotText(Integer.MIN_VALUE),
convertTotText(Integer.MAX_VALUE));
    isLessThan(convertTotText(0), convertTotText(Integer.MAX_VALUE));
        
    // will be false
    isLessThan(convertTotText(45), convertTotText(21));
  }

  public static void isLessThan(String s1, String s2)
  {
    System.out.println(s1.compareTo(s2) < 0);
  }
}

It all seems to work OK so will this also work for Lucene?  Possibly Field.java
could be have a isNumeric field added to it.

John






---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to