Copied to clipboard

Flag this post as spam?

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


  • Victor Vranceanu 2 posts 72 karma points
    Nov 14, 2018 @ 20:46
    Victor Vranceanu
    0

    Filter products by property

    I want to filter products from a specific collection based on properties from Product Content Type (Extended Content). I tried to get all products from the collection and filter them with LINQ but considering that there are more than 1k products it takes too much time. Do you know a better solution for this?

    Thanks, Victor

  • Joe 32 posts 136 karma points
    Nov 15, 2018 @ 02:52
    Joe
    0

    If you're talking about Linq, then I guess you're using Merchello.Context.Service.ProductService to do the search, which do query on DB, thus being slow. Consider doing the search on Examine Indexer (treat it as local cache) instead.

    Step 1: Add your own key to Merchello's Product Examine index

            #region Customizing Examine Indexer
            // Basing on https://our.umbraco.com/packages/collaboration/merchello/merchello/61912-Searching-for-products-by-partial-sku-and-getting-IPublishedContent-back#comment-210571
            //Add event to allow searching by site section
            ProductIndexer.GatheringNodeData += ProductIndexerSite_GatheringNodeData;
    

    Step 2: Then you can do the search on the Index

    var searcher = Examine.ExamineManager.Instance.SearchProviderCollection["MerchelloProductSearcher"];
    // Search Style 2: Showing a "AND" case for each category, combining with "OR" case inside keyword
    // Basing on And vs Or at the 1st filtering. See https://our.umbraco.com/documentation/reference/searching/examine/overview-explanation#fluent-api
    var searchCriteria = searcher.CreateSearchCriteria(BooleanOperation.And);
    // Here, add your own Lucene filters
    var productVariantResults = searcher.Search(searchCriteria).OrderByDescending(x => x.Score).ToList();
    

    In my case, I added all collectionKeys into Product Indexer field "staticCollectionKeys", so my Examine/Lucene query looks like this:

                        foreach (string normalField in normalFields)
                        {
                            builder.Append($"staticCollectionKeys: (+{normalField.Replace(" ", " +")}) ");
                        }
    

    Study on Examine Indexer & Lucene Query, which will help you to boost the performance.

  • Joe 32 posts 136 karma points
    Nov 15, 2018 @ 02:59
    Joe
    0

    Or you can also look into Andrew Wilson's solution, which is much cleaner and easier if it's good enough for your requirement. https://our.umbraco.com/packages/collaboration/merchello/merchello/94099-filters#comment-297596

    My requirement on search is too complicated, search-by-collection is just one of the requirements, so I need to use Lucene query and adding custom Examine index field.

Please Sign in or register to post replies

Write your reply to:

Draft