lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Uwe Schindler" <>
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
> 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
> 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(...) {
  protected Query newTermQuery(Term term) {
    final BooleanQuery bq = new BooleanQuery();
    bq.add(super.newTermQuery(term), BooleanClause.Occur.SHOULD);
    final Query pq = new PrefixQuery(term);
    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.


To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message