lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Michael Szediwy" <michael.szed...@syslink.ch>
Subject RE: Automatic Prefix Query
Date Tue, 27 Sep 2011 07:52:57 GMT
Thanks a lot ... It works! :-)
Cheers 
Michael

-----Original Message-----
From: Uwe Schindler [mailto:uwe@thetaphi.de] 
Sent: Montag, 26. September 2011 17:33
To: java-user@lucene.apache.org
Subject: RE: Automatic Prefix Query

One thing to mention:

> You can subclass QueryParser and override newFieldQuery to produce a 
> PrefixQuery instead of TermQuery - that's all. Here a simple example 
> with
an
> anonymous inner class:
> 
> qp = new QueryParser(...) {
>   @Override
>   protected Query newTermQuery(Term term) {
>     return new PrefixQuery(term);
>   }
> };
> 
> After that this QueryParser instance will produce PrefixQuery for each
token,
> where it created a TermQuery before (the default impl does "new 
> TermQuery(term)").

PrefixQuery does no scoring at all, so all results get the same score. A
good trick to emphasize exact hits is adding both queries (TermQuery and
PrefixQuery), maybe boost the PrefixQuery even down by 0.2 or like that:

qp = new QueryParser(...) {
  @Override
  protected Query newTermQuery(Term term) {
    final BooleanQuery bq = new BooleanQuery();
    bq.add(super.newTermQuery(term), BooleanClause.Occur.SHOULD);
    final Query pq = new PrefixQuery(term);
    pq.setBoost(0.2f);
    bq.add(pq, BooleanClause.Occur.SHOULD);
    return bq;
  }
};

If you then get a exact hit, this hits both clauses and gets extra
boost.
Hits only on the prefix get a much lower score.

Uwe


---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-user-help@lucene.apache.org


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