How to FIX: Sitecore Built-in Lucene Search in Content Tree always returns First Version instead of Latest

As workaround for this bug, compile the class below and replace the standard SearchSystemIndex processor in web.config file

<!--<processor type="Sitecore.Pipelines.Search.SearchSystemIndex, Sitecore.Kernel"/>-->
<processor type="Sitecore.Support.Pipelines.Search.SearchSystemIndex, YourAssemblyName"/>

The class


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Sitecore.Diagnostics;
using Sitecore.Globalization;
using Sitecore.Pipelines.Search;
using Sitecore.Search;
using Sitecore.Text;

namespace Sitecore.Support.Pipelines.Search
{
  public class SearchSystemIndex
{
    // Methods
    public void Process(SearchArgs args)
    {
        Assert.ArgumentNotNull(args, "args");
        Assert.IsNotNull(SearchManager.SystemIndex, "System index");
        using (IndexSearchContext context = SearchManager.SystemIndex.CreateSearchContext())
        {
            SearchHits hits;
            QueryBase query = args.Query ?? new FullTextQuery(args.TextQuery);
            try
            {
                if ((args.Root != null) && (args.Type != SearchType.ContentEditor))
                {
                    SearchContext context2 = new SearchContext(Sitecore.Context.User, args.Root) {
                        ContentLanguage = args.ContentLanguage
                    };
                    hits = context.Search(query, 100, context2);
                }
                else
                {
                  SearchContext context3 = new SearchContext(Sitecore.Context.User)
                  {
                        ContentLanguage = args.ContentLanguage
                    };
                    hits = context.Search(query, 100, context3);
                }
            }
            catch (Exception exception)
            {
                Log.Error("Invalid lucene search query: " + args.TextQuery, exception, this);
                return;
            }
            foreach (SearchResult result in (IEnumerable<SearchResult>)hits.FetchResults(0, args.Limit))
            {
              SearchResult lastVersion = this.GetLastVersion(result);
              if (lastVersion != null && !args.Result.Contains(result))
                {
                  args.Result.AddResult(lastVersion);
                }
            }
        }
    }

    private SearchResult GetLastVersion(SearchResult result)
    {
      SearchResult lastVersion = null;
      if (result.Subresults != null && result.Subresults.Count > 0)
      {
        lastVersion = result.Subresults.Last();
      }
      else
      {
        lastVersion = result;
      }
      return lastVersion;
    }
}

}

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s