Lucene 4.0: Custom Query Parser newTermQuery(Term term) override

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Lucene 4.0: Custom Query Parser newTermQuery(Term term) override

Jamie Band
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: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Lucene 4.0: Custom Query Parser newTermQuery(Term term) override

Yonik Seeley-2-2
On Wed, Jul 11, 2012 at 9:34 AM, Jamie <[hidden email]> wrote:
> 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

Random pointer: for most special case field handling, one would want
to override getFieldQuery or newFieldQuery rather than the lower level
newTermQuery.

-Yonik
http://lucidimagination.com

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Lucene 4.0: Custom Query Parser newTermQuery(Term term) override

Jamie Band
Yonik

Thanks for the tip.However, from what I can see, I still need to return
a TermQuery specific to each data type. Does anyone know how to convert
a string value to TermQuery for each data type?

Jamie


On 2012/07/11 3:42 PM, Yonik Seeley wrote:

> On Wed, Jul 11, 2012 at 9:34 AM, Jamie<[hidden email]>  wrote:
>> 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
> Random pointer: for most special case field handling, one would want
> to override getFieldQuery or newFieldQuery rather than the lower level
> newTermQuery.
>
> -Yonik
> http://lucidimagination.com
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>


--
MailArchiva - Email Archiving and Compliance
Stimulus Software
Tel: +1-713-343-8824 ext 100
Fax: +1-(877)-350-2328


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]