lucene-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Rasik Pandey" <>
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!


import org.apache.lucene.index.IndexReader;


/*Rasik Pandey*/
/**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 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();
        this.rewrittenQueries = newCachedQuery;

    public Query getOriginalQuery() {
        return this.originalQuery;

    public void setBoost(float 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 {
        //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:
For additional commands, e-mail:

View raw message