lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Michael McCandless <luc...@mikemccandless.com>
Subject Re: How do you properly use NumericField
Date Fri, 09 Oct 2009 19:48:36 GMT
On Fri, Oct 9, 2009 at 3:26 PM, Paul Taylor <paul_t100@fastmail.fm> wrote:

> I currently use NumberTools.longToString() to add integer fields to
> an index and allow range searching, then when searching I then
> preprocess the query (using regular expressions) and convert integer
> fields to NumberTools.longToString before it is parsed by the
> QueryParser, then when I return the results I use
> NumberTools.stringToLong(), so my implementation is flaky. Now I'm
> using Lucene 2.9 I thought Id use NumericField and hoped I could
> remove the preprocessing instead but I'm really not clear what I do
> on the indexing and searching side. I've even just bought the MEAP
> version of Lucene Action 2nd Edition and it doesn't even get a
> mention (nor does NumberTools for that matter it just mentions
> padding numbers with zeroes).
>
> So please anyone got a simple example of how to add a numeric field
> to an index, and what has to be done on the search side, assuming
> receiving a text string that gets parsed by the QueryParser

The next LIA2 MEAP update, which should be out very soon, covers
NumericField and also shows how to extend QueryParser (by subclassing
and overriding newRangeQuery) to properly create a NumericRangeQuery,
like this:

  static class NumericRangeQueryParser extends QueryParser {
    public NumericRangeQueryParser(String field, Analyzer a) {
      super(field, a);
    }
    public Query getRangeQuery(String field,
                               String part1,
                               String part2,
                               boolean inclusive)
        throws ParseException {
      TermRangeQuery query = (TermRangeQuery)
        super.getRangeQuery(field, part1, part2,
                              inclusive);
      if ("price".equals(field)) {
        return NumericRangeQuery.newDoubleRange(
                      "price",
                      Double.parseDouble(
                           query.getLowerTerm()),
                      Double.parseDouble(
                           query.getUpperTerm()),
                      query.includesLower(),
                      query.includesUpper());
      } else {
        return query;
      }
    }
  }

It still relies on super.getRangeQuery() for non-numeric fields.  If
you don't have non-numeric fields that accept range queries you can
simply call NumericRangeQuery.newXXXRange directly.

Mike

---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-user-help@lucene.apache.org


Mime
View raw message