Hi Paul, for creating NumericFields just refer to the JavaDoc. As Mike said on the query side you can create NumericRangeQuery directly (recommended) - see javadocs. If you want to use QueryParser, you have to customize it, as QueryParser does not support NumericRangeQuery natively.
Uwe ----- Uwe Schindler H.-H.-Meier-Allee 63, D-28213 Bremen http://www.thetaphi.de eMail: u...@thetaphi.de > -----Original Message----- > From: Michael McCandless [mailto:luc...@mikemccandless.com] > Sent: Friday, October 09, 2009 10:49 PM > To: java-user@lucene.apache.org; paul_t...@fastmail.fm > Subject: Re: How do you properly use NumericField > > On Fri, Oct 9, 2009 at 3:26 PM, Paul Taylor <paul_t...@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-unsubscr...@lucene.apache.org > For additional commands, e-mail: java-user-h...@lucene.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: java-user-unsubscr...@lucene.apache.org For additional commands, e-mail: java-user-h...@lucene.apache.org