Note that there are some explanatory texts on larger screens.

plurals
  1. POLucene 3 iterating over all hits
    primarykey
    data
    text
    <p>I'm in the process of updating a tool that uses a Lucene index. As part of this update we are moving from Lucene 2.0.0 to 3.0.2. For the most part this has been entirely straightforward. However, in one instance I cant seem to find a straightforward conversion.</p> <p>Basically I have a simple query and I need to iterate over <em>all</em> hits. In Lucene 2 this was simple, e.g.:</p> <pre><code>Hits hits = indexSearcher.search(query); for(int i=0 ; i&lt;hits.length() ; i++){ // Process hit } </code></pre> <p>In Lucene 3 the API for <code>IndexSearcher</code> has changed significantly and although I can bash together something that works, it is only by getting the top <code>X</code> documents and making sure that <code>X</code> is sufficiently large. </p> <p>While the number of hits (in my case) is typically between zero and ten, there are anomalous situation where they could number much higher. Having a fixed limit therefor feels wrong. Furthermore, setting the limit really high causes OOME which means that space for all <code>X</code> possible hits is allocated immediately. As this operation is carried out <em>alot</em>, something reasonably efficient is desired.</p> <p><strong>Edit:</strong></p> <p>Currently I've got the following to work:</p> <pre><code>TopDocs hits = indexSearcher.search(query, MAX_HITS); for (int i=0 ; i&lt;hits.totalHits ; i++) { // Process hit } </code></pre> <p>This works fine except that </p> <p><strong>a)</strong> what if there are more hits then <code>MAX_HITS</code> ?</p> <p>and </p> <p><strong>b)</strong> if MAX_HITS is large then I'm wasting memory as room for each hit is allocated before the search is performed.</p> <p>As most of the time there will only be a few hits, I don't mind doing follow up searches to get the subsequent hits, but I cant seem to find a way to do that.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload