title: How do you configure Lucene in Sitecore to only index the latest version of an item on the master db?
body: <p>I recognise this is a moot point on the web database, so this question applies to the master db...</p>
  
  <p>I have a custom index set up in Sitecore 6.4.1 as follows:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;index id="search_content_US" type="Sitecore.Search.Index, Sitecore.Kernel"&gt;
      &lt;param desc="name"&gt;$(id)&lt;/param&gt;
      &lt;param desc="folder"&gt;_search_content_US&lt;/param&gt;
      &lt;Analyzer ref="search/analyzer" /&gt;
      &lt;locations hint="list:AddCrawler"&gt;
          &lt;search_content_home type="Sitecore.Search.Crawlers.DatabaseCrawler, Sitecore.Kernel"&gt;
              &lt;Database&gt;master&lt;/Database&gt;
              &lt;Root&gt;/sitecore/content/usa home&lt;/Root&gt;
              &lt;Tags&gt;home content&lt;/Tags&gt;
          &lt;/search_content_home&gt;
      &lt;/locations&gt;
  &lt;/index&gt;
  </code></pre>
  
  <p>I query the index like this (I am using techphoria414's <code>SortableIndexSearchContext</code> from this answer: <a href="https://stackoverflow.com/questions/7314195/how-to-sort-filter-using-the-new-sitecore-search-api">How to sort/filter using the new Sitecore.Search API</a>):</p>
  
  <pre class="lang-c# prettyprint-override"><code>private SearchHits GetSearchResults(SortableIndexSearchContext searchContext, string searchTerm)
      {
          CombinedQuery query = new CombinedQuery();
          query.Add(new FullTextQuery(searchTerm), QueryOccurance.Must);
          return searchContext.Search(query, Sort.RELEVANCE);
      }
  
  ...
  
  SearchHits hits = GetSearchResults(searchContext, searchTerm);
  </code></pre>
  
  <p><code>hits</code> is a collection of search hits from my index. When I iterate through <code>hits</code> I can see that there are many duplicates of the same items in Sitecore, 1 per version of the item.</p>
  
  <p>I then do the following to get a <code>SearchResultCollection</code>:</p>
  
  <pre class="lang-c# prettyprint-override"><code>SearchResultCollection results = hits.FetchResults(0, hits.Length);
  </code></pre>
  
  <p>This combines all of the duplicates into a single <code>SearchResult</code> object. This object represents 1 version of a particular item, and has a property called <code>SubResults</code> which is a collection of <code>SearchResult</code>s that represent all of the other item versions.</p>
  
  <p>Here's my problem:</p>
  
  <p>The version of the item represented by the <code>SearchResult</code> is <strong>NOT</strong> the current published version of the item! It appears to be a randomly selected version (whichever the search method hit first in the index). The latest version <strong>is</strong> included in the <code>SubResults</code> collection, however.</p>
  
  <p>E.g.:</p>
  
  <pre class="lang-c# prettyprint-override"><code>SearchResult
   |
   |- Version 8 // main result
   ...
   |- SubResults
        |
        |- Version 9 // latest version
        |- Version 3
        |- Version 5
        ... // all versions in random order
  </code></pre>
  
  <p><strong>How do I prevent this from happening on the master db? Either by preventing Lucene from indexing old versions of items, or by doing some manipulation of the result set to get the latest version from the <code>SubResults</code>?</strong></p>
  
  <p>As an aside, why does Lucene bother to index old versions of items anyway? Surely this is pointless for searching content on your website as the old versions are not visible?</p>