lucene-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Rasik Pandey" <rasik.pan...@ajlsm.com>
Subject RE : Query Term Collector (was: Re: New highlighter package available)
Date Wed, 17 Mar 2004 12:36:08 GMT
Hello All,

I don't know how this Thread/issue was resolved, but if you are still interested I have a
simple way of doing this term collection ONLY at query time. I've tested it and it works with
highlighting, etc. without the extra rewrite() call on the index.

Comments are welcome!


package org.apache.lucene.search;

import org.apache.lucene.search.Weight;
import org.apache.lucene.search.Searcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.Similarity;
import org.apache.lucene.index.IndexReader;

import java.io.IOException;

/*Rasik Pandey rasik.pandey@ajlsm.com*/
/**Simple wrapper for a Lucene query that
 * collects all queries generated by calling
 * rewrite on the original Lucene query and stores
 * them in a BooleanQuery.
 *
 * A Searcher will call the rewrite() method
 * for each index and hence generate a query
 * containing terms for the respective index. This
 * class collects these queries so that they may be
 * used for highlighting, query expansion, etc. by
 * retrieving the underlying terms.
 *
 * @see #rewrite
 * @see #getRewrittenQueries
 * @see #resetRewrittenQueries
 * @see #getOriginalQuery
 */
public class CachingRewrittenQueryWrapper extends Query{
    protected org.apache.lucene.search.Query originalQuery = null;
    protected BooleanQuery rewrittenQueries = new BooleanQuery();

    public CachingRewrittenQueryWrapper(Query originalQuery) {
        this.originalQuery = originalQuery;
    }

    public BooleanQuery getRewrittenQueries() {
        return this.rewrittenQueries;
    }

    public void resetRewrittenQueries() {
        BooleanQuery newCachedQuery = new BooleanQuery();
        newCachedQuery.setMaxClauseCount(this.rewrittenQueries.getMaxClauseCount());
        this.rewrittenQueries = newCachedQuery;
    }

    public Query getOriginalQuery() {
        return this.originalQuery;
    }

    public void setBoost(float b) {
        this.originalQuery.setBoost(b);
    }

    public float getBoost() {
        return this.originalQuery.getBoost();
    }


    protected Weight createWeight(Searcher searcher) {
        return this.originalQuery.createWeight(searcher);
    }

    public Query rewrite(IndexReader reader) throws IOException {
        Query rewrittenQuery =  this.originalQuery.rewrite(reader);
        this.rewrittenQueries.add(rewrittenQuery, false, false);
        return rewrittenQuery;
    }

    public Query combine(Query[] queries) {
        return this.originalQuery.combine(queries);
    }

    public Similarity getSimilarity(Searcher searcher) {
        return this.originalQuery.getSimilarity(searcher);
    }

    protected void finalize() throws Throwable {
        super.finalize();
        //TODO maybe something here to ensure that all resources held by rewrittenQueries
are cleaned up properly
    }

    public String toString() {
        return this.originalQuery.toString();
    }

    public String toString(String field) {
       return this.originalQuery.toString(field);
    }
}



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


Mime
View raw message