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 Do you still have to override QueryParser to allow numeric range searches in Lucene 4.1
Date Mon, 25 Feb 2013 10:50:39 GMT
In my 3.6 code I was adding numeric field to my index as follows:

public void addNumericField(IndexField field, Integer value) {
         addField(field, NumericUtils.intToPrefixCoded(value));
     }


but I've chnaged it to (work in progress)

public void addNumericField(IndexField field, Integer value) {
         FieldType ft = new FieldType();
         ft.setStored(true);
         ft.setIndexed(true);
         ft.setNumericType(FieldType.NumericType.INT);
         doc.add(new IntField(field.getName(), value, ft));
     }

whihc seems neater

In 3.6 I also add to override queryparser to make it work for numeric 
range searches, do I still have to do this and if so how do I modify it 
to work for
IntField.

package org.musicbrainz.search.servlet;

import org.apache.lucene.index.Term;
import org.apache.lucene.queryparser.classic.MultiFieldQueryParser;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TermRangeQuery;
import org.apache.lucene.util.NumericUtils;
import org.musicbrainz.search.LuceneVersion;
import org.musicbrainz.search.index.LabelIndexField;
import org.musicbrainz.search.servlet.mmd1.LabelType;

public class LabelQueryParser extends MultiFieldQueryParser {

     public LabelQueryParser(java.lang.String[] strings, 
org.apache.lucene.analysis.Analyzer analyzer)
     {
         super(LuceneVersion.LUCENE_VERSION, strings, analyzer);
     }

      protected Query newTermQuery(Term term) {

          if(
                  ( term.field() == LabelIndexField.TYPE.getName())

             ) {
             try {
                 int typeId = Integer.parseInt(term.text());
                 if (typeId >= LabelType.getMinSearchId() && typeId <= 
LabelType.getMaxSearchId()) {
                     TermQuery tq = new TermQuery(new Term(term.field(), 
LabelType.getBySearchId(typeId).getName()));
                     return tq;
                 }
                 else {
                     return super.newTermQuery(term);
                 }
             }
             catch(NumberFormatException nfe) {
                 return super.newTermQuery(term);

             }
         } else if(
                 (term.field() == LabelIndexField.CODE.getName())
                 ){
             try {
                 int number = Integer.parseInt(term.text());
                 TermQuery tq = new TermQuery(new Term(term.field(), 
NumericUtils.intToPrefixCoded(number)));
                 return tq;
             }
             catch (NumberFormatException nfe) {
                 //If not provided numeric argument just leave as is, 
won't give matches
                 return super.newTermQuery(term);
             }
         } else {
             return super.newTermQuery(term);

         }
     }

     /**
      *
      * Convert Numeric Fields
      *
      * @param field
      * @param part1
      * @param part2
      * @param inclusive
      * @return
      */
     @Override
     public Query newRangeQuery(String field,
                                String part1,
                                String part2,
                                boolean inclusive) {

         if (
                 (field.equals(LabelIndexField.CODE.getName()))
             )
         {
             part1 = NumericUtils.intToPrefixCoded(Integer.parseInt(part1));
             part2 = NumericUtils.intToPrefixCoded(Integer.parseInt(part2));
         }
         TermRangeQuery query = (TermRangeQuery)
                 super.newRangeQuery(field, part1, part2,inclusive);
         return query;
     }

}


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