Copied to clipboard

Flag this post as spam?

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


  • k 256 posts 654 karma points
    Apr 08, 2015 @ 14:44
    k
    0

    Ezsearch - sorting

    Hello,

    I am using ezsearch package on a websire and I want to do a sorting in the results. Can someone please advise on how to proceed.

    Thanks in advance,

    kusum

     

     

     

  • Jan Skovgaard 11280 posts 23678 karma points MVP 10x admin c-trib
    Apr 08, 2015 @ 15:14
    Jan Skovgaard
    0

    Hi Kusum

    What kind of sorting do you want to do? And is it when the search results have been rendered initially or is it before the search has been executed?

    And what exact version of Umbraco are you using? (It's just nice to know).

    /Jan

  • Dennis Aaen 4499 posts 18254 karma points admin hq c-trib
    Apr 08, 2015 @ 15:28
    Dennis Aaen
    1

    Hi Kusum,

    If you want to sort the search results been rendered. Then I think that you should take a look at the line 211. In there you can add the OrderBy. In the example down here I sort by nodeName. 

    @foreach (var result in model.PagedResults.OrderBy(r => r.Fields["nodeName"])){ 
    ....
    }

    Hope this helps,

    /Dennis

     

  • k 256 posts 654 karma points
    Apr 09, 2015 @ 07:53
    k
    0

    Hi Dennis ,

    Thanks for the help. But I want to sort by latest date. I am trying CreateDate or UpdateDate but it's not working. If you could advise.

    Thanks,

    kusum

     

     

     

  • k 256 posts 654 karma points
    Apr 09, 2015 @ 07:58
    k
    0

    Hello jan,

    I am using umbraco7 and ezsearch 1.2

    Thanks,

    kusum

     

     

  • Jan Skovgaard 11280 posts 23678 karma points MVP 10x admin c-trib
    Apr 09, 2015 @ 08:05
    Jan Skovgaard
    0

    Hi Kusum

    Ok, what does your code look like? Could you please share?

    /Jan

  • Dennis Aaen 4499 posts 18254 karma points admin hq c-trib
    Apr 09, 2015 @ 08:10
    Dennis Aaen
    0

    Hi Kusum,

    Then I think that you can do something like this,

    For using the create date.

    @foreach (var result in model.PagedResults.OrderBy(r => r.Fields["createDate"])){ 

    }

    For using the update date.

    @foreach (var result in model.PagedResults.OrderBy(r => r.Fields["updateDate"])){ 

    }

    Hope this helps,

    /Dennis

  • Ismail Mayat 4511 posts 10090 karma points MVP 2x admin c-trib
    Apr 09, 2015 @ 10:31
    Ismail Mayat
    1

    Guys,

    You can sort this way however you are using linq2objects and therefore stuff is being loaded into memory if you do not have too much content then it should be fine.  

    Ideally you want lucene to do the sorting for you, although that involves a little bit more work.  The createDate and updateDate fields which are standard umbraco fields are in lucene index already as sortable fields so you can sort on them however in ezSearch you will need to drill into the code and at the point where the query is built and the sortby criteria there.  

    If you want to sort by fields that are not injected in as sortable you then have 2 options to inject those fields as sortable, the easy way which is to modify the examine config file and for the field add sortable=true or using document.writing event see http://thecogworks.co.uk/blog/posts/2013/april/examiness-hints-and-tips-from-the-trenches-part-10-document-writing-redux/ for information.

    Regards

    Ismail

  • k 256 posts 654 karma points
    Apr 09, 2015 @ 14:29
    k
    0

    Hi Dennis,

    It worked thanks a lot. And if i want to sort by an alias exemple "date". I have tried many options but not working. If you please help.

    Thanks,

    kusum

     

     

     

  • Dennis Aaen 4499 posts 18254 karma points admin hq c-trib
    Apr 09, 2015 @ 15:11
    Dennis Aaen
    0

    Hi kusum,

    I just talked to the creator of the ezSeach package on Twitter, and here what he said. "it's possible but you'd need to modify the lucene query. It's built to order by most relevant which is what most people would need."

    I am not a .NET developer myself, I am working on the frontend :-)

    Hope this can help you further.

    /Dennis

     

  • Ismail Mayat 4511 posts 10090 karma points MVP 2x admin c-trib
    Apr 10, 2015 @ 11:43
    Ismail Mayat
    0

    Kusum,

    If you look at the source code of the macro partial https://github.com/mattbrailsford/ezSearch/blob/master/Src/Our.Umbraco.ezSearch/Web/UI/Views/MacroPartials/ezSearch.cshtml line 135. You have

    var criteria2 = criteria.RawQuery(query.ToString());

    Just before this line you could do

    criteria.OrderBy("fieldToOrderBy");

    The thing to note however as I pointed out earlier is that if the field you want to order on is standard umbraco field then it should already be in the index as sortable. If you want to use your own custom field like alias "date" then you have 2 options, see http://thecogworks.co.uk/blog/posts/2013/april/examiness-hints-and-tips-from-the-trenches-part-10-document-writing-redux/ there is easy way which is to update ExamineIndex.config and rebuild the index or use code to inject in sortable field.  Can you paste the contents of your ExamineIndex.config we can then see from there is the config way is the way to go.

    Regards

     

    Ismail

  • Andrew Bright 84 posts 244 karma points
    Jan 11, 2017 @ 13:41
    Andrew Bright
    0

    I have the same issue tried various different things but no luck I have essentially adapted the ezSearch for my purpose and all that is remaining is the order by please help:

    Query builder:

    // Perform the search
    var searcher = ExamineManager.Instance.CreateSearchCriteria();
    
    var query = searcher.NodeTypeAlias("umbPropertyDetails"); 
    query.Not().Field("umbracoNaviHide", "1");
    
    // Set search path
            var contentPathFilter = model.RootContentNodeId > 0
                ? string.Format("__IndexType:{0} +searchPath:{1} -template:0", UmbracoExamine.IndexTypes.Content, model.RootContentNodeId)
                : string.Format("__IndexType:{0} -template:0", UmbracoExamine.IndexTypes.Content);
    
            var mediaPathFilter = model.RootMediaNodeId > 0
                ? string.Format("__IndexType:{0} +searchPath:{1}", UmbracoExamine.IndexTypes.Media, model.RootMediaNodeId)
                : string.Format("__IndexType:{0}", UmbracoExamine.IndexTypes.Media);
    
        // Ensure page contains all search terms in some way
        if (model.Location != "any")
        {
            query.And().Field("regionID", model.Location);
        }
        if (model.MinPrice >= 0 && model.MaxPrice > 0)
        {
            var paddedLower = model.MinPrice.ToString("D6");
            var paddedHigher = model.MaxPrice.ToString("D6");
            //groupedOr.AppendFormat(String.Format(searchField + ":[{0} TO {1}]", model.MinPrice, model.MaxPrice));
            query.And().Range("price", paddedLower, paddedHigher, true, true);
        }
    
        if (model.MinBedrooms > 0)
        {
            query.And().Field("propertyBedrooms", model.MinBedrooms.ToString());
        }
        var results = ExamineManager.Instance.Search(query.Compile()) 
        .Where(x => (
             !Umbraco.IsProtected(int.Parse(x.Fields["id"]), x.Fields["path"]) ||
             (
                 Umbraco.IsProtected(int.Parse(x.Fields["id"]), x.Fields["path"]) &&
                 Umbraco.MemberHasAccess(int.Parse(x.Fields["id"]), x.Fields["path"])
             )) && (
                 (x.Fields["__IndexType"] == UmbracoExamine.IndexTypes.Content && Umbraco.TypedContent(int.Parse(x.Fields["id"])) != null) ||
                 (x.Fields["__IndexType"] == UmbracoExamine.IndexTypes.Media && Umbraco.TypedMedia(int.Parse(x.Fields["id"])) != null)
             ))
        .ToList();
        model.AllResults = results;
    
        model.TotalResults = results.Count;
        model.TotalPages = (int)Math.Ceiling((decimal)model.TotalResults / model.PageSize);
        model.CurrentPage = Math.Max(1, Math.Min(model.TotalPages, model.CurrentPage));
    
        // Page the results
        model.PagedResults = model.AllResults.Skip(model.PageSize * (model.CurrentPage - 1)).Take(model.PageSize).OrderBy(r => r.Fields["price"]);
    
        LogHelper.Debug<string>("[ezSearch] Searching Lucene with the following query: " + query.ToString());
    
  • Ismail Mayat 4511 posts 10090 karma points MVP 2x admin c-trib
    Jan 12, 2017 @ 08:39
    Ismail Mayat
    0

    Andrew,

    You are sorting by linq not examine. In fact after your main search you are additionally filtering with linq. The protected stuff you could inject that into your indexing using gathering node then you could use examine to query filter if doc is protected or not.

    Additionally the price field that you are ordering by you need to inject that into the index as well then you can order using examine.

    Regards

    Ismail

  • Andrew Bright 84 posts 244 karma points
    Mar 07, 2017 @ 21:18
    Andrew Bright
    0

    Sorry just seen this Im sorry this went over my head a bit can you advise what needs amending please so this re-orders by price.

    Many Thanks

Please Sign in or register to post replies

Write your reply to:

Draft