title: Lucene IndexSearcher#search(Query,int), impact of parameter n
body: <p>I have a Lucene index with ~ 9 Mio. documents, they are queried using a <code>BooleanQuery</code> which is combined of several <code>TermQueries</code> (which are combined as <code>AND</code>). Very basic. My searches usually yield in results of about 10 documents, but it might happen in rare cases, that I get considerably more results. What I'm doing, is something like the following:</p>
  
  <pre><code>TopDocs searchResult = searcher.search(query, MAX_RESULT_COUNT);
  for (int i = 0; i &lt; searchResult.scoreDocs.length; i++) {
      Document document = searcher.doc(searchResult.scoreDocs[i].doc);
      // read data from index fields and create result instances here
  }
  </code></pre>
  
  <p>In my great naivety, I initially set <code>MAX_RESULT_COUNT</code> to <code>Integer.MAX_VALUE</code>, because I thought, if searches usually only get 10 results, the maximum value wouldn't matter and that value would never be reached anyways. Great mistake. As I discovered, search is magnitudes faster, the lower I set <code>MAX_RESULT_COUNT</code>.</p>
  
  <p>Now, as mentioned, I could set a comparatively low value (e.g. 100) to obtain optimal search performance. But in some rare cases, there might be more results, which I also have to grab.</p>
  
  <p>Can somebody explain to me, why this value has such a great impact on search speed, <strong>although</strong> the number of actually available results is magnitudes lower? Is there an elegant way to grab all available results and still get highest possible performance?</p>