title: How to optimize Hits Object looping in Lucene.Net
body: <p>I am trying to build a custom search engine using Lucene.Net.</p>
  
  <p>All seems to be going fine but i'm facing a problem while looping the Hits Object returned by the Search function. Whenever I search for an item it takes around 0.07 seconds for the Search Function and between 20 to 90 seconds to loop into the returned object.</p>
  
  <p>ex: (for a total of 11384 results returned)<br>
  Search took 0.068 seconds.<br>
  Object Looping took 25.50 seconds.</p>
  
  <p>Now the second time I loop this same object it takes much less time (for the same above case it took  0.141 seconds.</p>
  
  <p>Here's the Search function:</p>
  
  <pre><code>public List&lt;SearchResultId&gt; SearchId(string searchTerm)
      {
          IndexSearcher indexSearcher = new IndexSearcher(_indexDir);
          List&lt;SearchResultId&gt; searchList = new List&lt;SearchResultId&gt;();
  
          try
          {
              QueryParser queryParser = new QueryParser(_fieldName, new StandardAnalyzer());
  
              Query query = queryParser.Parse(searchTerm);
              Hits hits = indexSearcher.Search(query);
              int numHits = hits.Length();
              SearchResultId search;
              for (int i = 0; i &lt; numHits; ++i)
              {
                  search = new SearchResultId();
                  search.score = hits.Score(i);
                  search.id = hits.Doc(i).Get(LuceneIndexer.DOC_ID_FIELD_NAME);
                  searchList.Add(search);
              }
          }
          catch
          {
              indexSearcher.Close();
          }
          indexSearcher.Close();
          return searchList;
      }
  </code></pre>
  
  <p>Is this normal to happen? Is there a way to optimize this process?</p>
  
  <p>Thanks</p>