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 Mon, 12 Oct 2009 11:00:14 GMT
Uwe Schindler wrote:
> I forgot: The format of numeric fields is also not plain text, because of
> this a simple TermQuery as generated by your query parser will not work,
> too.
>
> If you want to hit numeric values without a NumericRangeQuery with lower and
> upper bound equal, you have to use NumericUtils to translate the term text,
> e.g. new TermQuery(new Term("field",
> NumericUtils.intToPrefixCoded(value,precstep)))
>
> If you want support for this in QueryParser, you have to override
> QueryParser.newTermQuery as explained before for newRangeQuery. By the way,
> Solr does this in exactly that way.
>
> Uwe
>   

Ok, Im trying my best here but still cannot get range or single term 
query searching to work.

package org.musicbrainz.search.servlet;

import junit.framework.TestCase;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.NumericField;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.*;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.NumericUtils;
import org.musicbrainz.search.index.TrackAnalyzer;

public class NumericFieldTest extends TestCase {

    public void testNumericFields() throws Exception {
        Analyzer analyzer = new TrackAnalyzer();
        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);
        writer.addDocument(doc);
        writer.close();

        IndexSearcher searcher = new IndexSearcher(dir,true);
        {

            Query q = new 
MusicbrainzQueryParser("dur",analyzer).parse("[12 TO 124]");
            assertEquals(1, searcher.search(q,10).totalHits);


            q = new MusicbrainzQueryParser("dur",analyzer).parse("123");
            assertEquals(1, searcher.search(q,10).totalHits);


        }
    }

    static class MusicbrainzQueryParser extends QueryParser {

        public MusicbrainzQueryParser(String field, Analyzer a) {
            super(field, a);
            System.out.println("init parser");
        }

        public Query newRangeQuery(String field,
                                   String part1,
                                   String part2,
                                   boolean inclusive)
                 {
            System.out.println("RangeQuery");
            TermRangeQuery query = (TermRangeQuery)
                    super.newRangeQuery(field, part1, part2,
                            inclusive);

            if ("dur".equals(field)) {
                System.out.println("durRangeQuery");

                return NumericRangeQuery.newIntRange(
                        "dur",
                        Integer.parseInt(query.getLowerTerm()),
                        Integer.parseInt(query.getUpperTerm()),
                        query.includesLower(),
                        query.includesUpper());
            } else {
                return query;
            }
        }

        protected Query newTermQuery(Term term)
        {
            System.out.println("newTermQuery");
            if(term.field().equals("dur")) {
               System.out.println("dur,newTermQuery");
               TermQuery tq =  new TermQuery(new Term("field",
               
NumericUtils.intToPrefixCoded(Integer.parseInt(term.text()),NumericUtils.PRECISION_STEP_DEFAULT)));
               return tq;
            }
            else {
                return super.newTermQuery(term);
            }
        }
    }

}

---------------------------------------------------------------------
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