Copied to clipboard

Flag this post as spam?

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


  • Chris Knowles 141 posts 222 karma points
    Nov 07, 2011 @ 14:21
    Chris Knowles
    0

    Examine Search

    Hi,

    Am trying to create a product search on length, height and depth but am really struggling with the basics here. I am a Razor noob too. I'm using Umbraco 4.7.1 on IIS6 + SQL Server 2005

    I have the following code but get and 'Object reference not set to an instance of an object' error. I thought i'd start by getting it to search on length only as it shouldn't be too difficult to add the others later. 

    @using Examine
    @using Examine.SearchCriteria
    @using UmbracoExamine
    
    @{
        var searchString = Request.QueryString["l"];
    
        var searcher = ExamineManager.Instance.SearchProviderCollection["ProductSearchIndexSet"];
        var searchCriteria = searcher.CreateSearchCriteria(UmbracoExamine.IndexTypes.Content);
        var filter = searchCriteria
            .Field("productLength", searchString)
            .And()
            .Field("parentID", "1107")
            .Not()
            .Field("umbracoNaviHide", "1")
            .Compile();
    
        var searchResults = searcher.Search(filter);
    
    }
    <ul>
    @foreach (var c in searchResults)
    {
       <li><a href="@umbraco.library.NiceUrl(c.Id)">@c.Fields["nodeName"]</a></li>
    }
    </ul>

    Can someone help me out and show me the error of my ways?

    Thanks

    Chris

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Nov 07, 2011 @ 14:22
    Sebastiaan Janssen
    0

    What is the REAL error you're getting?? (add ?umbDebugShowTrace=true to the URL and scroll down to the red error in the trace).

  • Chris Knowles 141 posts 222 karma points
    Nov 07, 2011 @ 14:24
    Chris Knowles
    0

    Seb,

    umbracoMacro Error Loading Razor Script (file: Search Results) Object reference not set to an instance of an object.    at ASP._Page_macroScripts_Search_cshtml.Execute() in d:\Inetpub\wwwroot\myproj\macroScripts\Search.cshtml:line 9
      at System.Web.WebPages.WebPageBase.ExecutePageHierarchy()
      at System.Web.WebPages.WebPage.ExecutePageHierarchy(IEnumerable`1 executors)
      at System.Web.WebPages.WebPage.ExecutePageHierarchy()
      at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
      at umbraco.MacroEngines.RazorMacroEngine.ExecuteRazor(MacroModel macro, INode currentPage)
      at umbraco.MacroEngines.RazorMacroEngine.Execute(MacroModel macro, INode currentPage)

    Is this helpful?

    Thanks

    Chris

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Nov 07, 2011 @ 14:40
    Sebastiaan Janssen
    0

    Sort of.. have a look at what is going on at line 9 of your code, it's the filter, right? 

    So what I'd do is start with an empty filter and add criteria 1 by 1.

  • Chris Knowles 141 posts 222 karma points
    Nov 07, 2011 @ 14:51
    Chris Knowles
    0

    Line 9 is 

    var searchCriteria = searcher.CreateSearchCriteria(UmbracoExamine.IndexTypes.Content);

    I have tried removing the Index Type but this doesn't help. Is there somewhere with proper documentation on how to set this up in either Razor or as a usercontrol? I have watched the video on UmbracoTV but couldn't get that working and can't find a guide that i can get working.

    Thanks

    Chris

  • Barry Fogarty 493 posts 1129 karma points
    Nov 08, 2011 @ 01:13
    Barry Fogarty
    0

    Doesnt seem to be a problem with the searcher.

    Try a simpler query like

    var filter = searchCriteria
                   
    .Field("productLength", "50")
                   
    .Compile();

    and try to get that to work.  If so refer to http://umbraco.com/follow-us/blog-archive/2011/9/16/examining-examine.aspx for information on how to build up queries.  It is also useful to download the Luke tool http://code.google.com/p/luke/ to ensure your indexes are all populated and you can return results from there first.

    FYI I have my searcher and criteria set up as follows (simplified - some query building steps removed):

    var criteria = ExamineManager.Instance
                .SearchProviderCollection["MySearcher"]
                .CreateSearchCriteria(UmbracoExamine.IndexTypes.Content, Examine.SearchCriteria.BooleanOperation.Or);

    Examine.SearchCriteria.IBooleanOperation filter = null;
     filter = criteria.Field("nodeName", searchTerm)
    ISearchResults results = ExamineManager.Instance.SearchProviderCollection["MySearcher"].Search(filter.Compile());

    Not sure if this will help but something else to try perhaps

  • Chris Knowles 141 posts 222 karma points
    Nov 08, 2011 @ 11:23
    Chris Knowles
    0

    Barry,

    I have downloaded luke and get some interesting results, i need to do a range search on 3 fields, my current code is as follows:

    @using Examine
    @using Examine.SearchCriteria
    @using UmbracoExamine
    
    @{
        var prodLength = Request.QueryString["l"];
        var prodWidth = Request.QueryString["w"];
        var prodDepth = Request.QueryString["d"];
        var prodLengthMin = 0;
        var prodLengthMax = 99999;
        var prodWidthMin = 0;
        var prodWidthMax = 99999;
        var prodDepthMin = 0;
        var prodDepthMax = 99999;
    
        if (!String.IsNullOrEmpty(prodLength))
        {
            string[] arrL = prodLength.Split('-');
            prodLengthMin = Convert.ToInt32(arrL[0]);
            prodLengthMax = Convert.ToInt32(arrL[1]);
        }
        if (!String.IsNullOrEmpty(prodWidth))
        {
            string[] arrW = prodLength.Split('-');
            prodWidthMin = Convert.ToInt32(arrW[0]);
            prodWidthMax = Convert.ToInt32(arrW[1]);
        }
        if (!String.IsNullOrEmpty(prodDepth))
        {
            string[] arrD = prodLength.Split('-');
            prodDepthMin = Convert.ToInt32(arrD[0]);
            prodDepthMax = Convert.ToInt32(arrD[1]);
        }
    
        var searchProvider = "ProductSearcher";
        var searchCriteria = ExamineManager.Instance.SearchProviderCollection[searchProvider].CreateSearchCriteria(UmbracoExamine.IndexTypes.Content, Examine.SearchCriteria.BooleanOperation.Or);
    
        var filter = searchCriteria
            .Range("productLength", prodLengthMin, prodLengthMax)
            .And()
            .Range("prodWidth", prodWidthMin, prodWidthMax)
            .And()
            .Range("prodDepth", prodDepthMin, prodDepthMax)
            .Not()
            .Field("umbracoNaviHide", "1")
            .Compile();
        var searchResults = ExamineManager.Instance.SearchProviderCollection[searchProvider].Search(filter);
    
    }
    • @prodLengthMin
    • @prodLengthMax
    • @prodWidthMin
    • @prodWidthMax
    • @prodDepthMin
    • @prodDepthMax
    • @foreach (var c in searchResults) {
    • @c.Fields["nodeName"]
    • }

    I set the parameters wide enough to begin with so that it should find everything if they don't provide a parameter but i'm not sure why they aren't returning any results as the products do have these values in the correct fields.

    This is my first examine/razor project and i am struggling badly. I wouldn't mind a usercontrol to do it in but i haven't been able to get one working in a way that i can understand.

    Thank you for your help so far.

    Chris

  • Barry Fogarty 493 posts 1129 karma points
    Nov 08, 2011 @ 22:15
    Barry Fogarty
    0

    Try outputting the actual Lucene query that is run in your macro, like so:

    @searchCriteria.ToString()

    put it after you do the compile().

     

    If you are getting results in Luke then you can compare the queries in this way.  I don't know anything about range queries so not able to help specifically on your query.  Sounds like you are getting there through.

     

  • Chris Knowles 141 posts 222 karma points
    Nov 09, 2011 @ 11:00
    Chris Knowles
    0

    Barry,

    The reason the range doesn't work is because it's a text search rather than a numerical one so what need to happen is that the number needs to be padded out with zero's so i can do a proper comparison.

    Thanks for your all your help.

    Chris

  • Barry Fogarty 493 posts 1129 karma points
    Nov 09, 2011 @ 11:18
    Barry Fogarty
    0

    No worries glad I could help.  NB If any of the responses answered your original question, don't forget to mark the solution to assist others that end up on this thread.

  • Chris Knowles 141 posts 222 karma points
    Nov 10, 2011 @ 22:44
    Chris Knowles
    0

    I followed this post and got the answer working in the end - http://our.umbraco.org/forum/using/ui-questions/16888-Examine-Search-Range-Values

     

    Thanks

Please Sign in or register to post replies

Write your reply to:

Draft