Today I saw a forum post stating how can we achieve catalog search which has different sorting options on different properties, i.e., the user wants to have ascending order on one property and descending on second property at one shot. At the beginning I thought commerce server doesn’t support this feature but it does support with searchOptions (thanks for Gael). Commerce Server provides SearchOptions by which we can get search narrow down results (i.e, we can select the properties you want to return, number of rows, sort either descending or ascending, etc) in a single call.

Through SortProperty we can pass the properties name and order and make sure you keep property in [] braces. If you miss braces then you will receive “Search Failed” error. Here is the example where I am trying to fetch all products where the list price is greater than 10$ and the sorting on category name (desc), productId (asc) and variantId (desc). 

CatalogContext context = CommerceContext.Current.CatalogSystem;
// Search the catalog.
// Create the search options.
CatalogSearchOptions searchOptions = new CatalogSearchOptions();
searchOptions.PropertiesToReturn = "CategoryName, DefinitionName, i_classtype, ProductId, VariantId, DisplayName";
searchOptions.SortProperty = "[CategoryName]DESC, [ProductId]ASC, [VariantId]DESC";
CatalogSearch catalogSearch = context.GetCatalogSearch();

// Specify the catalogs to search.
// This is a comma-separated list of catalogs to search,
// for example, "Catalog1,catalog2".
catalogSearch.CatalogNames = "Adventure Works Catalog";

// Return all items with price greater than $10.00.
catalogSearch.SqlWhereClause = "cy_list_price>10";

catalogSearch.SearchOptions = searchOptions;

// Perform the search.
int totalRecords = 0;
CatalogItemsDataSet catalogItems = catalogSearch.Search(out totalRecords);
results.DataSource = catalogItems.Tables[0];
results.DataBind();

Related Articles


If you like this post, please click on our sponsor advertisement.


4 Responses to “Catalog Search – Ascending on first property and descending on second property”

  • Catalin:

    Hello,

    Very interesting post!
    However, how does this kind of sorting take place? If first sorts the results based on the first criteria, and then, for those results
    which are equal it applies the second criteria?

    And a second question:
    How do you obtain the search results based only on free text search ranking (without any other sorting option)
    in CS 2009 extensibility kit (Microsoft.Commerce.Portal.SearchController.cs, public static List DoProductQuerySearch

    Thank you in advance

  • Catalin:

    I solved it!
    For anyone interested to know.
    FreeTextSearch_Rank is the property you need to add to the web part’s sort options.
    You also need to add it in MetadataDefinitions.xml, among the properties.

    • Kris:

      Hi Catalin,

      Could you please post what is to be added to the metadatadefinitions.xml in order for the FreeTextSearch_Rank to be returned in the result set?

      Regards,

      Kris

Leave a Reply

Follow me on Google+
Add to circles

In 0 people's circles

Sign up for Newsletter