lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Kunemann Frank" <>
Subject AW: cancel search
Date Fri, 09 Sep 2005 09:01:56 GMT
Exceptions didn't work as you need to implement the HitCollector class. Its method "collect"
doesn't throw any exceptions and I don't want to rewrite anything of Lucene, so I'm using
"searcher.close()" instead:

public class TimedHitCollector extends HitCollector
    private Searcher searcher;
    private ArrayList ids = new ArrayList();
    private long startTime;
    private long maxTime;
    private long endTime;
    private int maxDocs = 10;

    public TimedHitCollector(Searcher searcher, ArrayList list, long maxTime)
        startTime = System.currentTimeMillis();
        this.searcher = searcher;
        this.ids = list;
        this.maxTime = maxTime;

    public void collect(int id, float score)
        long diff = System.currentTimeMillis() - startTime;

        if((diff > maxTime) || (ids.size() >= maxDocs)) {
            endTime = System.currentTimeMillis();
            System.out.println("canceling search...");

            try {
            catch(IOException e) {
        else {
            ids.add(new Integer(id));
            endTime = System.currentTimeMillis();

Be sure to catch IOException when calling from your application, otherwise
your program will exit sometimes. In this case no results will be returned.
That's why I can't use it to cancel the search after a given time or amount of documents found.
But it's good enough for a cancel-button.

Hope this will help everyone having the same problem.


-----Urspr√ľngliche Nachricht-----
Von: Yonik Seeley [] 
Gesendet: Donnerstag, 8. September 2005 15:09
Betreff: Re: cancel search

You could create your own HitCollector that checked a flag on each hit, and 
throw an exception if it was set.
In a separate thread, you could set the flag to cancel the search.

Now hiring --

On 9/8/05, Kunemann Frank <> wrote:
> The problem is that when searching there is no real save point to stop
> the thread. The only line that takes time is this one:
> Hits hits =;
> Frank
> >I've had such a long lasting search too. I sounds good to start the
> >search in another thread. I've done this for the indexing procedure.
> >This is started in another thread and the gui will be informed when
> >indexing is performed. If the user will stop it he has to click on a
> >button stop and then an event is send to the indexerthread. The
> >indexerthread stops if he reaches a safe point. Surely this is for
> >indexing but I think this would work for searching also.
> >stefan
> ---------------------------------------------------------------------
> To unsubscribe, e-mail:
> For additional commands, e-mail:

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

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

View raw message