Copied to clipboard

Flag this post as spam?

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


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

    Ez Search sorting results

    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());
    

    Tried solution 1 adding to the query:

    if (!string.IsNullOrEmpty(model.SortOrder))
    {
        switch (model.SortOrder)
        {
            case "price-low":
                query.And().OrderByDescending(new string[] { "price" });
                break;
            case "price-high":
                query.And().OrderBy(new string[] { "price" });
                break;
            default:
                query.And().OrderBy(new string[] { "price" });
                break;
        }
    }
    

    Tried Solution 2 append to the results:

    if (!string.IsNullOrEmpty(model.SortOrder))
    {
            switch (model.SortOrder)
               {
                    case "price-low":
                        //query.And().OrderByDescending(new string[] { "price" });
                        model.AllResults = results.OrderBy(r => r.Fields, "price" );
                        break;
                    case "price-high":
                        model.AllResults = results.OrderByDescending(r => r.Fields["price"]);
                        break;
                    default:
                        model.AllResults = results.OrderByDescending(r => r.Fields["price"]);
                        break;
                }
            }
    

    And Solution 3 in contentResult Helper

    @helper RenderContentResult(SearchViewModel model, IPublishedContent result)
    {
    
    foreach (var field in model.AllResults .Where(field => result.HasValue(field)).OrderBy(r => result.GetPropertyValue<int>("price")))
    {
    
  • Shannon Deminick 1525 posts 5271 karma points MVP 2x
    Jan 12, 2017 @ 00:08
    Shannon Deminick
    1

    Sorting search results with Linq is not how you should be sorting anything that comes from Examine.

    In Examine's query structure, it has an OrderBy clause which tells Lucene to order by, if you are ordering by in Linq that means it's going to load all search results into memory and perform poorly

    https://github.com/Shazwazza/Examine/wiki/Sorting-results

  • Andrew Bright 84 posts 244 karma points
    Jan 25, 2017 @ 21:44
    Andrew Bright
    0

    Hi thank you for coming back to me however how would I order a list of properties in my scenrio without knowing their prices first? I am searching against a doc type and tried implementing the below can you advise?

    // Perform the search
    var searcher = ExamineManager.Instance.CreateSearchCriteria();
    
    var query = searcher.NodeTypeAlias("umbPropertyDetails");
    
    if (!string.IsNullOrEmpty(model.SortOrder))
            {
                switch (model.SortOrder)
                {
                    case "price-low":
                        query.And().Field("price", "myValue???").And().OrderBy("customfield???");
                        break;
                    case "price-high":
                        query.And().Field("price", model.MaxPrice.ToString("D6")).And().OrderByDescending("price");
                        break;
                    default:
                        query.And().Field("price", model.MinPrice.ToString("D6")).And().OrderBy("price");
                        break;
                }
            }
    
Please Sign in or register to post replies

Write your reply to:

Draft