提问者:小点点

计算Lucene搜索的时间


关于x的结果以xx秒为单位。

我如何能得到准确的时间?他们在lucene库下有这样的工具吗?还是说这个需要我自己算?

编辑:假设在lucene核心库下没有这样的工具:

这是一个搜索代码:

TopScoreDocCollector collector = TopScoreDocCollector.create(100, true);
            is.search(query, collector);
            hits = collector.topDocs().scoreDocs;

如何获得准确的搜索时间(我应该用计时器包装哪一行)?

再编辑一次:我尝试了这个:

        long t1 = System.currentTimeMillis();
        is.search(query, collector);
        hits = collector.topDocs().scoreDocs;
        long t2 = System.currentTimeMillis();

        Long time = (t2-t1);
        System.out.println("time : " + time);

每次搜索我都在控制台上得到< code>time : 0。

这意味着。。。Lucene没有时间搜索?(我的索引中只有100个文档)

还有一个:尝试过这个;

long start = System.nanoTime();
            is.search(query, collector);
            hits = collector.topDocs().scoreDocs;
            long end = System.nanoTime();

            long time = (end - start) / 1000;

这将打印:

time : 10261
time : 12285
time : 14513
time : 1309

共1个答案

匿名用户

在Lucene中搜索对不同的人来说意味着不同的事情。E、 g.仅搜索(indexSearcher.search(…))VS搜索所有文档(或仅一页文档)。或者使用VS而不进行排序。这可能会给已经很复杂的API增加不必要的混乱。

使用时间测量代码包装您的搜索调用非常简单。顺便说一下,考虑使用guava的Stopwatch或任何其他类似工具而不是System.currentTime*