lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Uwe Schindler" <...@thetaphi.de>
Subject RE: Automatic Prefix Query
Date Mon, 26 Sep 2011 15:33:23 GMT
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


Mime
View raw message