Copied to clipboard

Flag this post as spam?

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


  • Michael Sims 119 posts 387 karma points
    May 18, 2013 @ 21:18
    Michael Sims
    0

    Razor Search Example

    I'm having trouble creating a search facility on my website using razor. Here is what I have so far:

    In the ExamineIndex.config file:

     

        <IndexSet SetName="WebsiteIndexSet" IndexPath="~/App_Data/TEMP/ExamineIndexes/WebsiteIndexSet/">
          <IndexAttributeFields>
            <add Name="id" Type="Number" />
            <add Name="nodeName" EnableSorting="true" />
            <add Name="createDate" EnableSorting="true" Type="DateTime" />        
            <add Name="updateDate" EnableSorting="true" Type="DateTime" />
            <add Name="writerName" />
            <add Name="path" />
            <add Name="nodeTypeAlias" />
            <add Name="parentID" />
          </IndexAttributeFields>   

          <IndexUserFields>
            <add Name="pageTitle" />
            <add Name="pageShort" />
            <add Name="pageIntroduction" />
            <add Name="pageContent" />
            <add Name="uBlogsyContentTitle" />
            <add Name="uBlogsyContentSummary" />
            <add Name="uBlogsyContentBody" />
          </IndexUserFields>      

          <IncludeNodeTypes>
         <add Name="uBlogsyPost" />
         <add Name="Page" />
         <add Name="Event" />
         <add Name="Home" />
         <add Name="Level1" />
         <add Name="Dashboard" />
         <add Name="Events" />
    </IncludeNodeTypes>

          <ExcludeNodeTypes>
         <add Name="File" />
    </ExcludeNodeTypes>

        </IndexSet>

    In the ExamineSettings.config file:
          <add name="WebsiteIndexer" type="UmbracoExamine.UmbracoContentIndexer, UmbracoExamine"
               supportUnpublished="false"
               supportProtected="true"
               interval="10"
               indexSet="WebsiteIndexSet"
               analyzer="Lucene.Net.Analysis.Standard.StandardAnalyzer, Lucene.Net"/>
      
          <add name="WebsiteSearcher" type="UmbracoExamine.UmbracoExamineSearcher, UmbracoExamine" 
             analyzer="Lucene.Net.Analysis.Standard.StandardAnalyzer, Lucene.Net"
             indexSet="WebsiteIndexSet" />
    In my search.cshtml file:
     string term = Request["s"]; // Search term
      var criteria = ExamineManager.Instance.SearchProviderCollection["WebsiteSearcher"].CreateSearchCriteria();
     Examine.SearchCriteria.IBooleanOperation filter = null;
      
      // Create array of node fields to be searched
      var fields = new string[] { "nodeName", "pageTitle", "pageShort", "pageContent", "uBlogsyContentTitle", "uBlogsyContentSummary", "uBlogsyContentBody" };
      // Split & iterate each word within the search term
      foreach (var t in term.Split(' '))
      {
        if (filter == null)
        {
          filter = criteria.GroupedOr(fields, t);
        }
        else
        {
          filter = filter.And().GroupedOr(fields, t);
        }
      }
      if(filter == null)
      {
        filter = criteria.GroupedOr(fields, term);
      }
      else
      {
        filter = filter.And().GroupedOr(fields, term);
      }
      // Execute the search query & order by score
      var searchResults = Examine.ExamineManager.Instance
        .SearchProviderCollection["WebsiteSearcher"]
        .Search(filter.Compile())
        .OrderByDescending(x => x.Score);
    The problem:
    So I have some content items that have the title "Bible in a year".
    When I search for Bible I get 50 results
    However when I search:
    Bible in
    I get 0 results
    Any ideas what I am doing wrong?

     

  • gary 385 posts 916 karma points
    May 19, 2013 @ 16:07
    gary
    0

    hi Michael

    is your split on var t only taking first entry? Do you need to add string params for the second, third etc?

    Only had a quick glance, but that came to mind so thought it may help.

    apologies if not useful

     

    gary

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies