Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculate time of lucene search

Tags:

java

time

lucene

How can i get the exact time taken by lucene to search a string. I wanted to show something like this : enter image description here

About x results in xx seconds.

how can i get exact time ? is their any utility under lucene library for this ? or do i need to calculate this on my own ?

EDIT : assuming their is no utility for this under lucene core library :

this is a search code :

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

how can i get exact time of search (which line should i wrap with timer) ?

one more edit : I tried This :

        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);

I am getting time : 0 on console for every search.

does that mean... Lucene is taking no time in search ? (i have only 100 documents in my index)

And one more : tried this ;

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

            long time = (end - start) / 1000;

this prints :

time : 10261
time : 12285
time : 14513
time : 1309
like image 961
JAVAGeek Avatar asked Dec 29 '25 04:12

JAVAGeek


1 Answers

Searching in Lucene can mean different things to different people. E.g. just the searching (indexSearcher.search(...)) VS searching + iterating over all documents (or just a page of documents). Or with VS without sorting. It would likely just add unnecessary clutter to the already complex API.

It is really simple to wrap your search call with time measuring code. And, by the way, consider using guava's Stopwatch or any other similar tool instead of System.currentTime*.

like image 95
mindas Avatar answered Dec 30 '25 16:12

mindas