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 {
searcher.close();
}
catch(IOException e) {
e.printStackTrace();
}
}
else {
ids.add(new Integer(id));
endTime = System.currentTimeMillis();
}
}
}
Be sure to catch IOException when calling searcher.search 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.
Frank
-----Ursprüngliche Nachricht-----
Von: Yonik Seeley [mailto:[EMAIL PROTECTED]
Gesendet: Donnerstag, 8. September 2005 15:09
An: [email protected]
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.
-Yonik
Now hiring -- http://tinyurl.com/7m67g
On 9/8/05, Kunemann Frank <[EMAIL PROTECTED]> 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 = searcher.search(query);
>
>
> 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: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]