3
votes

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
1
Just wrap a timer around the call that performs the search.adrianbanks
that means ..NO utility under lucene core ??JAVAGeek
100 documents is very small amount. Try to use System.nanoTime()mishadoff
@mishadoff.. i did that.. but how can i use this for displaying on result.... i mean what unit should i choose so that ..i do not get 0 on result page...JAVAGeek

1 Answers

2
votes

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*.