Example usage for org.apache.lucene.search TimeLimitingCollector setBaseline

List of usage examples for org.apache.lucene.search TimeLimitingCollector setBaseline

Introduction

In this page you can find the example usage for org.apache.lucene.search TimeLimitingCollector setBaseline.

Prototype

public void setBaseline(long clockTime) 

Source Link

Document

Sets the baseline for this collector.

Usage

From source file:org.languagetool.dev.index.Searcher.java

License:Open Source License

private PossiblyLimitedTopDocs getTopDocs(Query query, Sort sort) throws IOException {
    final TopFieldCollector topCollector = TopFieldCollector.create(sort, maxHits, true, false, false, false);
    final Counter clock = Counter.newCounter(true);
    final int waitMillis = 1000;
    // TODO: if we interrupt the whole thread anyway, do we still need the TimeLimitingCollector?
    final TimeLimitingCollector collector = new TimeLimitingCollector(topCollector, clock,
            maxSearchTimeMillis / waitMillis);
    collector.setBaseline(0);
    final Thread counterThread = new Thread() {
        @Override/*from ww  w  . ja v  a  2s . co  m*/
        public void run() {
            final long startTime = System.currentTimeMillis();
            while (true) {
                final long runTimeMillis = System.currentTimeMillis() - startTime;
                if (runTimeMillis > maxSearchTimeMillis) {
                    // make sure there's no lingering thread for too long
                    return;
                }
                clock.addAndGet(1);
                try {
                    Thread.sleep(waitMillis);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    };
    counterThread.setName("LuceneSearchTimeoutThread");
    counterThread.start();

    boolean timeLimitActivated = false;
    try {
        indexSearcher.search(query, collector);
    } catch (TimeLimitingCollector.TimeExceededException e) {
        timeLimitActivated = true;
    }
    return new PossiblyLimitedTopDocs(topCollector.topDocs(), timeLimitActivated);
}