Comparison operators with strings using Lucene

2.8k views Asked by At

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.

2

There are 2 answers

1
BitByter GS On BEST ANSWER

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

2
ArtemGr On

[If you really need the original value of the field then]
Keep the original in a separate Lucene field, e.g. "originalAge".

P.S. It seems you can control the query parser to some extent by overriding getFieldQuery. That way it should be possible to delegate text queries to the text field, while keeping numeric queries to the integer field. Scala sample:

val qp = new org.apache.lucene.queryparser.classic.QueryParser (Version.LUCENE_43, "age", ANALYZER_RUS) {
  override def getFieldQuery (field: String, queryText: String, quoted: Boolean): org.apache.lucene.search.Query = {
    println (s"getFieldQuery ($field, $queryText, $quoted)")
    if (field == "age") {
      if (queryText.matches ("^age(\\>|\\<)\\d+$")) super.getFieldQuery (field, queryText, quoted)
      else super.getFieldQuery ("originalAge", queryText, quoted)
    } else super.getFieldQuery (field, queryText, quoted)
  }
}
println (qp.parse ("undefined"))  // originalAge:undefined
println (qp.parse ("age>3"))  // age:age age:3

You might also check the flexible query parser.