As we told you before. The default QueryParser has no support fro NumericField (as it doesn't know the schema). To get it running, subclass it and overwrite newRangeQuery method to create a NumericRangeQuery for field names that are indexed using NumericField.
The recommended way is to instantiate the NumericQueries directly and not via a query parser. You can combina a text query with query parser then together with various numeric ranges using a BooleanQuery on top of it. If you really need a query string range representation for numeric values, there is no way around extending QueryParser. ----- Uwe Schindler H.-H.-Meier-Allee 63, D-28213 Bremen http://www.thetaphi.de eMail: u...@thetaphi.de > -----Original Message----- > From: Paul Taylor [mailto:paul_t...@fastmail.fm] > Sent: Sunday, October 11, 2009 7:58 PM > Cc: Michael McCandless; Lucene Users > Subject: Re: How do you properly use NumericField > > Michael McCandless wrote: > > On the indexing side you do this: > > > > doc.add(new NumericField("price").setDoubleValue(19.99)); > > > > The NumericField is not stored by default (there's also a ctor to > > specify Store.YES or Store.NO). > > > > If the numeric field is not being used in a range query, how is it > > being used? EG for sorting, it will just work. If you did store the > > field, when you retrieve it, it will come back as a normal field with > > a String value (equal to the .toString of original numeric value). > > > > (You can play with precisionStep, to trade off disk space & > > performance; especially if you will do range querying and eg only > > sorting, you should set precisionStep=Integer.MAX_VALUE; but these are > > advanced optimizations). > > > > Mike > > > Hmm, Im being dense here but even a simple non range search doesn't seem > to work when using Numeric Fields, in the test below it matches 789 okay > but not 123 > > Paul > > package org.musicbrainz.search.analysis; > > import junit.framework.TestCase; > import org.apache.lucene.analysis.Analyzer; > import org.apache.lucene.analysis.standard.StandardAnalyzer; > import org.apache.lucene.store.RAMDirectory; > import org.apache.lucene.index.IndexWriter; > import org.apache.lucene.document.Document; > import org.apache.lucene.document.NumericField; > import org.apache.lucene.document.Field; > import org.apache.lucene.search.IndexSearcher; > import org.apache.lucene.search.Query; > import org.apache.lucene.queryParser.QueryParser; > > public class NumericFieldTest extends TestCase { > > public void testNumericFields() throws Exception { > Analyzer analyzer = new StandardAnalyzer(); > RAMDirectory dir = new RAMDirectory(); > IndexWriter writer = new IndexWriter(dir, analyzer, true, > IndexWriter.MaxFieldLength.LIMITED); > Document doc = new Document(); > NumericField nf = new NumericField("dur"); > nf.setIntValue(123); > doc.add(nf); > doc.add(new Field("dur", "789", > Field.Store.NO,Field.Index.ANALYZED )); > writer.addDocument(doc); > writer.close(); > > IndexSearcher searcher = new IndexSearcher(dir,true); > { > Query q = new QueryParser("dur",analyzer).parse("789"); > assertEquals(1, searcher.search(q,10).totalHits); > > q = new QueryParser("dur",analyzer).parse("123"); > assertEquals(1, searcher.search(q,10).totalHits); > > > } > } > > > } > > > --------------------------------------------------------------------- > 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