lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jamie <ja...@stimulussoft.com>
Subject Lucene 4.0: Custom Query Parser newTermQuery(Term term) override
Date Wed, 11 Jul 2012 13:34:09 GMT
Hi Everyone

I am busying attempting to integrate Lucene 4.0 Alpha into my code base. 
I have a custom QueryParser that extends QueryParser and overrides 
newRangeQuery and newTermQuery to support typed fields.
I think I've figured out the newRangeQuery method translation (shown 
further down below), but newTermQuery method override is proving a 
little more tricky. I am not sure how to implement the equivalent of 
(Lucene 3.6 code below):

return new TermQuery(new Term(term.field(),
                     
NumericUtils.doubleToPrefixCoded(Double.parseDouble(term
                             .text()))));

for the various field type.

A quick pointer would be most appreciated.

Thanks

Jamie

public class CustomQueryParser extends QueryParser {


@Override
     protected Query newTermQuery(Term term) {
         if (term.field().equals("uid")) {
             return super.newTermQuery(term);
         }

         com.stimulus.field.Field field = 
Config.getConfig().getBlobFactory().getBlobFieldsFromCategory(blobCategory).get(term.field());

// this is our custom field
         if (field == null || field.getType() == Type.STRING) {
             return super.newTermQuery(term);
         } else if (field.getType() == Type.INTEGER
                 || field.getType() == Type.BOOLEAN) {
             return new TermQuery(
                     new Term((new 
Term(term.field(),NumericUtils.intToPrefixCoded(Integer.parseInteger(term.text()))));
         } else if (field.getType() == Type.DOUBLE) {
             return new TermQuery(new Term(term.field(),
                     
NumericUtils.doubleToPrefixCoded(Double.parseDouble(term
                             .text()))));
         } else if (field.getType() == Type.FLOAT
                 || field.getType() == Type.SIZE) {
             return new TermQuery(new Term(term.field(),
                     NumericUtils.floatToPrefixCoded(Float.parseFloat(term
                             .text()))));
         } else if (field.getType() == Type.DATE || field.getType() == 
Type.SIZE) {
             return new TermQuery(
                     new Term(term.field(), 
NumericUtils.longToPrefixCoded(Long
                             .parseLong(term.text()))));
         } else {
             return super.newTermQuery(term);
         }
     }

@Override
     protected Query newRangeQuery(String fieldStr, String part1, String 
part2,boolean startInclusive, boolean endInclusive) {
         TermRangeQuery query = (TermRangeQuery) 
super.newRangeQuery(fieldStr,part1, part2, startInclusive, endInclusive);
         Field field = Config.getConfig().getBlobFactory()
                 .getBlobFieldsFromCategory(blobCategory).get(fieldStr);
         if (field == null || field.getType() == Type.STRING) {
             return query;
         } else if (field.getType() == Type.INTEGER) {
             return NumericRangeQuery.newIntRange(field.getIndexField(), 4,
                     Integer.parseInt(query.getLowerTerm().utf8ToString()),
                     Integer.parseInt(query.getUpperTerm().utf8ToString()),
                     query.includesLower(), query.includesUpper());
         } else if (field.getType() == Type.LONG) {
             return NumericRangeQuery.newLongRange(field.getIndexField(), 8,
                     Long.parseLong(query.getLowerTerm().utf8ToString()),
                     Long.parseLong(query.getUpperTerm().utf8ToString()),
                     query.includesLower(), query.includesUpper());
         } else if (field.getType() == Type.DOUBLE) {
             return 
NumericRangeQuery.newDoubleRange(field.getIndexField(), 8,
                     
Double.parseDouble(query.getLowerTerm().utf8ToString()),
                     
Double.parseDouble(query.getUpperTerm().utf8ToString()),
                     query.includesLower(), query.includesUpper());
         } else if (field.getType() == Type.FLOAT) {
             return 
NumericRangeQuery.newFloatRange(field.getIndexField(), 4,
                     Float.parseFloat(query.getLowerTerm().utf8ToString()),
                     Float.parseFloat(query.getUpperTerm().utf8ToString()),
                     query.includesLower(), query.includesUpper());
         } else if (field.getType() == Type.DATE || field.getType() == 
Type.SIZE) {
             return NumericRangeQuery.newLongRange(field.getIndexField(), 8,
                     Long.parseLong(query.getLowerTerm().utf8ToString()),
                     Long.parseLong(query.getUpperTerm().utf8ToString()),
                     query.includesLower(), query.includesUpper());
         }
         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