lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Paul Taylor <paul_t...@fastmail.fm>
Subject Re: How do you properly use NumericField
Date Sun, 11 Oct 2009 17:58:20 GMT
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-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-user-help@lucene.apache.org


Mime
View raw message