Copied to clipboard

Flag this post as spam?

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


  • John C Scott 473 posts 1183 karma points
    Jan 31, 2012 @ 01:24
    John C Scott
    0

    Examine with Razor

    I'm trying to write an examine query with Razor (both are new to me).

    This is what I have so far:

    @using Examine;
    @using umbraco.MacroEngines;
    @using System.Xml.XPath;
    @inherits umbraco.MacroEngines.DynamicNodeContext
    @{
    if (Request.QueryString["s"!= null &Request.QueryString["s"!= "" )
     {
    var searchTerm Request.QueryString["s"];
    var Searcher ExamineManager.Instance.SearchProviderCollection["WebsiteSearcher"];
    var searchCriteria Searcher.CreateSearchCriteria(BooleanOperation.Or);
    var query searchCriteria.GroupedOr(new string["contentTitle""contentIntro""summaryParagraph""nodeName"}searchTerm).Compile();
    var pagesToList Searcher.Search(query);

    This feels like it is  almost there, but the one bit I am not certain about is that I get an error...

    The name 'BooleanOperation' does not exist in the current context

    Firstly is this on the right track can I do this?

    and also how do I get that to exist in the current context :) ?

     

     

  • Peter Gregory 408 posts 1614 karma points MVP 3x admin c-trib
    Jan 31, 2012 @ 06:17
    Peter Gregory
    1

    Hey John!

    Try adding

    @using Examine.SearchCriteria;

    This should give you access to BooleanOperation.Or

    See if that resolves your issue.

  • John C Scott 473 posts 1183 karma points
    Jan 31, 2012 @ 14:50
    John C Scott
    0

    thanks Peter - that is absolutely brilliant - that works now

    I did feel terribly guilty tweeting knowing that v5 is so near !

    thank you again :)

     

  • Eric Schrepel 161 posts 226 karma points
    Feb 22, 2013 @ 01:55
    Eric Schrepel
    0

    Good discussion, as Examine was driving me a little nuts and the umbraco.tv videos are for older versions, not as easy to implement as the above. My version shown below, but TWO KEY steps that I discovered only through other forum articles:

    1. My search results were erratic, found out it was case-sensitivity (as mentioned here). Edit the /config/ExamineSettings.config file to replace "WhitespaceAnalyzer" with "Standard.StandardAnalyzer" for both the ExternalSearcher provider and ExternalIndexer, then rebuild the External index (easiest by install the "Examine Index" Umbraco package). The two "analyzer" lines in the .config file should read:
          analyzer="Lucene.Net.Analysis.Standard.StandardAnalyzer, Lucene.Net"
      Would be nice on next releases to have that be the default since case-sensitivity is rarely desired and hard to diagnose
    2. The LUKE Java tool to analyze indexes is great, but the most current verison (4.0 ALPHA) doesn't work on Umbraco Examine indexes (at least not those generated in Umbraco 6); use LUKE 3.5, works like a charm. In LUKE you can also tell if turning off the case-sensitivity (step 1) worked, as all index words should appear in lower case. Also, I found it handy to compare the total document count in LUKE with the total node count in BackOffice just to ensure it completed its task (create macro of "<p>@Model.Descendants().Count()</p>" and run it at your Home page node).

    Happy to continue this discussion :)  My razor code below:

    @using Examine
    @using Examine.SearchCriteria
    @using UmbracoExamine
    
    @{
      var searchString = Request["search"];
      if (!String.IsNullOrEmpty(searchString))
      {
        var searchProvider = ExamineManager.Instance.DefaultSearchProvider.Name;
    
        var searcher = ExamineManager.Instance.SearchProviderCollection[searchProvider];
    
        @* BooleanOperation.Or per our.umbraco.org/forum/core/general/28187-Examine-with-Razor *@
        var searchCriteria = searcher.CreateSearchCriteria(BooleanOperation.Or);
    
        var query = searchCriteria.GroupedOr(new string[] { "nodeName", "pageTitle", "pageSummary", "pageContent"}, searchString).Compile();
    
    
        foreach (var c in searcher.Search(query))
        {
            <h4>
              <a href="@umbraco.library.NiceUrl(c.Id)">
                @* For our purposes, pageTitle looks better in search results than nodeName,
                   so if there is one, use it by default *@
                @(c.Fields.Keys.Contains("pageTitle") ? c.Fields["pageTitle"] : c.Fields["nodeName"])
              </a>
            </h4>
            @* We also like to display a bit of the page content (stole code from somewhere, sorry
               not cited well.) *@
            if(c.Fields.Keys.Contains("pageContent"))
            {
              var snippet = c.Fields["pageContent"];
              if (snippet.Length>240)
              {
                snippet = snippet.Substring(0,240)+"...";
              }
              <p>@Html.Raw(snippet)</p>
            }
    
        }
      }
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft