Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Robin Nicholl 137 posts 277 karma points
    Oct 12, 2015 @ 00:41
    Robin Nicholl
    0

    Umbraco 7.3 Examine Search macro script error

    Could anyone tell me why this is not working on 7.3 (works find on 7.1.4):

    @using Examine.LuceneEngine.SearchCriteria
    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @{ string searchTerm = Request.QueryString["searchTerm"];@*Request.Form["searchTerm"];*@
       if( String.IsNullOrWhiteSpace(searchTerm ) ) {
          searchTerm = "";
       }
       var searcher = ExamineManager.Instance.SearchProviderCollection["MysiteSearcher"];
       var searchCriteria = searcher.CreateSearchCriteria();
       var query = searchCriteria.GroupedOr( new[] { "nodeName", "name", "title", "pageHeading", "itemHeading", "bodyText", "bodyText2", "itemDetails", "itemDetailsColumn2" }, searchTerm ).Compile();
       var searchResults = searcher.Search(query).Where(r => r["__IndexType"] == "content").Take(8).ToList();
    }
    <p>searchTerm: <strong>&ldquo;@searchTerm&rdquo;</strong></p>
    @if( searchResults.Any() ) {
      <ul class="search-results-box @searchResults.Count()">
        @foreach( var result in searchResults ) {
          var node = Umbraco.TypedContent(result.Id);
          var pathIds = result["__Path"].Split(',');
          var path = Umbraco.TypedContent(pathIds).Where(p => p != null).Select(p=> new {p.Name}).ToList();
          var baseurl = node.Url.Split('/')[2];
          if( baseurl == "mysite.com" ) {
            <li>
               <section class="results-box">
                  <h3 class="baseurl @baseurl">
                     <a href="@node.Url">@node.Name</a>
                  </h3>
                  <p><i class="fa fa-search"></i>&nbsp;<a href="@node.Url" class="results-url">@node.Url</a></p>
                  @if( result.Fields.ContainsKey("title" ) ) {
                     <p class="results-title"><strong>@result["title"]</strong></p>
                  }
                  @if( result.Fields.ContainsKey("bodyText" ) ) {
                     <p>@Html.Raw(result["bodyText"].Truncate(250))</p>
                  }
               </section>
               <hr />
            </li>
          }
        }
      </ul>
     } else {
         <p>There are no results matching your search criteria:
            @if( !String.IsNullOrWhiteSpace(searchTerm ) ) {
               <text>'@searchTerm'</text>
            }
         </p>
     }
    

    Always results in "Error loading Partial View script (file: ~/Views/MacroPartials/SearchResults.cshtml). The error seems to start at ExamineManager.Instance.SearchProviderCollection["MysiteSearcher"]; (if I remove from .SearchProviderCollection onwards it doesn't error (doesn't work either!).

    Thanks

  • Lars-Erik Aabech 349 posts 1100 karma points MVP 7x c-trib
    Oct 12, 2015 @ 06:35
    Lars-Erik Aabech
    0

    Hi Robin,

    You should find the detailed exception message in ~/App_Data/Logs/UmbracoTraceLog.txt.

    I suspect you've ether had ~/Config/ExamineSettings.config overwritten so that your "MysiteSearcher" doesn't exist, or you'll have to go to the Developer dashboard, Examine Management and reindex.

  • Robin Nicholl 137 posts 277 karma points
    Oct 12, 2015 @ 10:15
    Robin Nicholl
    0

    Hi Lars-Erik

    Thanks for your reply. Unfortunately, both ExamineIndex.config and ExamineSettings.config were merged very carefully, and "MysiteSearcher" does still exist. I have also cleared the index (several times) and reindexed (using Examine Index Admin).

    The UmbracoTraceLog says:

    [P3844/D6/T91] WARN  umbraco.macro - Error loading Partial View (file: ~/Views/MacroPartials/SearchResults.cshtml). Exception: System.IndexOutOfRangeException: Index was outside the bounds of the array.
       at ASP._Page_Views_MacroPartials_SearchResults_cshtml.Execute() in d:\vhosts\mysite\Views\MacroPartials\SearchResults.cshtml:line 33
       at System.Web.WebPages.WebPageBase.ExecutePageHierarchy()
       at System.Web.Mvc.WebViewPage.ExecutePageHierarchy()
       at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
       at Umbraco.Core.Profiling.ProfilingView.Render(ViewContext viewContext, TextWriter writer)
       at Umbraco.Web.Mvc.ControllerExtensions.RenderViewResultAsString(ControllerBase controller, ViewResultBase viewResult)
       at Umbraco.Web.Macros.PartialViewMacroEngine.Execute(MacroModel macro, IPublishedContent content)
       at umbraco.macro.LoadPartialViewMacro(MacroModel macro)
       at umbraco.macro.renderMacro(Hashtable pageElements, Int32 pageId)
    

    But I can't see anything wrong at line 33, which is:

    var baseurl = node.Url.Split('/')[2];
    

    Thanks

  • Lars-Erik Aabech 349 posts 1100 karma points MVP 7x c-trib
    Oct 12, 2015 @ 11:44
    Lars-Erik Aabech
    100

    It's pretty easy to spot now that you've got the real exception from the log.
    Index outside the bounds of the array means that the array you get from Url.Split('/') doesn't have as many as 3 entries.

    You should check the length of that array before you assign the third part to baseurl.

    For example:

    var urlParts = node.Url.Split('/');
    var baseUrl = "";
    if (urlParts.Length > 2) {
        baseUrl = urlParts[2];
    }
    
  • Robin Nicholl 137 posts 277 karma points
    Oct 12, 2015 @ 12:38
    Robin Nicholl
    0

    Thanks… and doh!

    I should obviously have realised this. but I was expecting node.Url to contain the full path, eg http://mysite.com/whatever (as it did prior to my upgrading this site to 7.3), whereas now it just returns eg /whatever

    Is this a change, or have I missed something? This is a multisite installation, and I was using this to filter results to just those from mysite.com

  • Lars-Erik Aabech 349 posts 1100 karma points MVP 7x c-trib
    Oct 12, 2015 @ 12:42
    Lars-Erik Aabech
    0

    It changed some time, but not quite sure which version.

    You can control it in umbracoSettings.config. Add urlProviderMode to the web.routing element at the bottom, and set it to one of these:

    AutoLegacy,
    Relative,
    Absolute,
    Auto,
    

    Snipped from 7.2.6 core code:

    /// <summary>
    /// Specifies the type of urls that the url provider should produce.
    /// 
    /// </summary>
    /// 
    /// <remarks>
    /// 
    /// <para>
    /// The <c>AutoLegacy</c> option is equivalent to <c>Auto</c> but it also respects the legacy <c>useDomainPrefixes</c> setting.
    ///             When that setting is true, then all urls are absolute. Otherwise, urls will be relative or absolute, depending on hostnames.
    /// </para>
    /// 
    /// <para>
    /// The <c>Relative</c> option can lead to invalid results when combined with hostnames, but it is the only way to reproduce
    ///             the true, pre-4.10, always-relative behavior of Umbraco.
    /// </para>
    /// 
    /// <para>
    /// For the time being, the default option is <c>AutoLegacy</c> although in the future it will be <c>Auto</c>.
    /// </para>
    /// 
    /// </remarks>
    
  • Robin Nicholl 137 posts 277 karma points
    Oct 12, 2015 @ 12:51
    Robin Nicholl
    0

    Thanks, Lars-Erik. I just figured out that I could use node.UrlAbsolute() – but it might be better to change it 'globally' from umbracoSettings… in case I've used it elsewhere ;)

  • Lars-Erik Aabech 349 posts 1100 karma points MVP 7x c-trib
    Oct 12, 2015 @ 12:54
    Lars-Erik Aabech
    0

    Might be smart. :)

    Glad you got it working.

Please Sign in or register to post replies

Write your reply to:

Draft