Search code examples
javasearchlucenequery-parser

Comparison operators with strings using Lucene


Integrated the QueryParser of Lucene in to my application's search. I have a stringField for which i have to perform some comparison operators also.

eg: age>3 age<4

But i cannot make the field Int.Because sometimes it may have the string values like "NIL","Undefined" etc.

So is there anyway to apply multiple types to the same field.Or is it possible to apply comparison operators with the stringField itself? Please help.


Solution

  • Use Range query:

    • age:[3 TO 5] equivalent to age between 3 and 5 (inclusive)
    • age:[3 TO *] equivalent to age > 3

    It will work for String type also.
    Reference: http://lucene.apache.org/core/2_9_4/queryparsersyntax.html#Range%20Searches