Copied to clipboard

Flag this post as spam?

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


  • William Corry 34 posts 171 karma points
    Jun 04, 2015 @ 17:23
    William Corry
    0

    Umbraco 7 examine search with reserved words

    I have set up a search using examine and all works fine until you with the dreaded reserved words like 'is' 'and' etc. I put in a quick hack so any search term below 4 was ignored but this did not solve 'is and' not that it is a common search term but no my luck that what someone might test with that combo.

    I saw something about it being caused by lucene but have not idea how to implment a fix this is what I have in the ExamineSettings.config:

    <add name="MySearchIndexer" type="UmbracoExamine.UmbracoContentIndexer, UmbracoExamine"
          supportUnpublished="false"
          supportProtected="false"
          interval="10"
          analyzer="Lucene.Net.Analysis.Standard.StandardAnalyzer, Lucene.Net"
          indexSet="MySearch"/>
    
    
    <add name="MySearchSearcher" type="UmbracoExamine.UmbracoExamineSearcher, UmbracoExamine"
       analyzer="Lucene.Net.Analysis.Standard.StandardAnalyzer, Lucene.Net" indexSet="MySearch" enableLeadingWildcards="true"/>
    

    And ExamineIndex.config:

    <IndexSet SetName="MySearch" IndexPath="~/App_Data/ExamineIndexes/MySearch/">
         <IndexAttributeFields>
             <add Name="id" />
             <add Name="nodeName"/>
             <add Name="updateDate" />
             <add Name="writerName" />
             <add Name="nodeTypeAlias" />
         </IndexAttributeFields>
         <IndexUserFields>
             <add Name="bodyText"/>
             <add Name="tags"/>
         </IndexUserFields>
         <IncludeNodeTypes>
              <add Name="KnowledgebaseItem" />
         </IncludeNodeTypes>
    </IndexSet>
    

    Then on the search result partial:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @using Examine
    @using Examine.SearchCriteria
    @using Examine.LuceneEngine.SearchCriteria
    
    @{
    var searchString = Request["search"];
    if (!string.IsNullOrEmpty(Request.QueryString["search"]))
    {
    
        //Fetching what eva searchterm some bloke is throwin' our way
        var q = Request.QueryString["search"].Trim();
        if (q.Length < 4)
        {
            q += " data";
        }
        //Fetching our SearchProvider by giving it the name of our searchprovider
        var Searcher = Examine.ExamineManager.Instance.SearchProviderCollection["MySearchSearcher"];
    
        var searchCriteria = Searcher.CreateSearchCriteria(Examine.SearchCriteria.BooleanOperation.Or);
        var query = searchCriteria
            .Field("nodeName", q.Boost(2)).Or()
            .Field("nodeName", q.Fuzzy(0.5f)).Or()
            .Field("bodyText", q.Boost(1)).Or()
            .Field("bodyText", q.Fuzzy(0.5f)).Or()
            .Field("tags", q.Fuzzy(0.8f));
    
    
        //Searching and ordering the result by score, and we only want to get the results that has a minimum of 0.05(scale is up to 1.)
        var searchResults = Searcher.Search(query.Compile()).OrderByDescending(x => x.Score).TakeWhile(x => x.Score > 0.05f);
    
        //Printing the results
        foreach (var item in searchResults)
        {
            var node = Umbraco.Content(item.Fields["id"]);
                <h4>
                    <a href="@node.NiceUrl">
                        @node.Name
                    </a>
                </h4>
                @Html.Raw(node.bodyText)
        }
    }
    }
    

    I have read something about Lucene.Net.Analysis.StopAnalyzer.ENGLISHSTOPWORDS_SET but have been unable to get this to work andy suggestions would be welcome.

  • William Corry 34 posts 171 karma points
    Jun 04, 2015 @ 20:55
    William Corry
    0

    Should point out the error I get when searching if it helps:

    Object reference not set to an instance of an object.
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
    
    Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
    
    Source Error:
    
    
    Line 14: 
    Line 15:         var searchCriteria = Searcher.CreateSearchCriteria(Examine.SearchCriteria.BooleanOperation.Or);
    Line 16:         var query = searchCriteria
    Line 17:             .Field("nodeName", q.Boost(2)).Or()
    Line 18:             .Field("nodeName", q.Fuzzy(0.5f)).Or()
    
Please Sign in or register to post replies

Write your reply to:

Draft