This tip explains how to search for products in any catalog of CatalogManager by using free text as search criteria. The following method demonstrates it.
Note: We can perform FreeTextSearch on those Properties only whose freeTextSearchable property is set to TRUE.
This method returns a DataSet with all the informations of the Products as results and takes two parameters :
1. Here the catalogName is the the name of the Catalog on which you want the search to be performed or Products of which catalog you want to be searched through.
2. The second parameter is the freeText on which the search will be done.(This string should contain words by blank space separated)
public DataSet GetProductsByClause(string catalogName, string freeText) { CatalogContext catalogContext = GetCatalogContext(); //Prepare the catalog search CatalogSearch freeTextSearch = catalogContext.GetCatalogSearch(); CatalogSearchOptions searchOptions = new CatalogSearchOptions(); //Prepare the options for the search //Set the property names only whose values you want to display searchOptions.PropertiesToReturn = "SKU, Color, Finish, Grade"; //Set the string PropertyName by which the sorting will be done searchOptions.SortProperty = "ProductID"; //Set whether to sort descending or ascending manner searchOptions.SortDescending = true; //Set the starting record from which the result will be displayed searchOptions.StartingRecord = 1; //Set the number of total records to be returned searchOptions.RecordsToRetrieve = 50; //Set the paging by giving pagenumber and pageSize as parameters to SetPaging() method searchOptions.SetPaging(1, 5); // Specify the categories to search. All categories that match the // expression are searched. This example searches all categories // that have "technical" as part of their descriptions. freeTextSearch .CateoriesClause = @"Description like '%white%'"; freeTextSearch .SearchOptions = searchOptions; //Mention the CatalogNames on which the search will be performed freeTextSearch .CatalogNames = catalogName; //UseAdvancedFreeTextSearch property should be set TRUE to perform the AdvancedFreeTextSearch freeTextSearch .UseAdvancedFreeTextSearch = true; //Split the freeText to string array of all the words separated by blank spaces and create the clause //set this clause to the AdvancedFreeTextSearchPhrase property of the CatalogSearch char[] separator = { ' ' }; string[] allWords = freeText.Split(separator); StringBuilder clause = new StringBuilder(); foreach (string word in allWords) { if (string.Compare(allWords[0], word, true) == 0) { clause.Append(" \"" + word + "\" "); } else { clause.Append(" OR \"" + word + "\" "); } } freeTextSearch .AdvancedFreeTextSearchPhrase = clause.ToString(); //Perform the search CatalogItemsDataSet searchResults = freeTextSearch .Search(); return searchResults; }