다음을 통해 공유


How to Perform a Guided Search [API]

For the latest version of Commerce Server 2007 Help, see the Microsoft Web site.

You can search a catalog using a guided search, also called a specifications search. With this search method you can perform a progressive search of products by specifying parameter constraints, such as a category, product, or property. To perform a guided search, you search for properties by progressively reducing the number of products that match your search criteria. You add and delete search clauses depending on input from the user. Guided searches are useful when you do not want to search all of your products for a property, but you need to identify a group of products instead.

If you are searching multiple catalogs, the properties specified in the PropertiesToReturn property must exist in all searched catalogs. To determine whether a property exists in all catalogs, use one of the GetSearchableProperties methods on the CatalogContext object.

  1. Use the BeginSpecificationSearch method on the ProductCatalog object to get a SpecificationSearch object.

  2. Use the SearchOptions property on the SpecificationSearch object to specify search options.

  3. Use the InventoryOptions property to specify inventory options, if any.

  4. Add one or more search clauses by using the SearchClauses property.

  5. Use one of the Search methods to perform the search.

  6. You can delete search clauses by using the RemoveSearchClause method.

  7. Continue to add and remove search clauses to refine the search based on customer input.

  8. Alternatively, you can reinitialize the SpecificationSearch object for each search.

Example

This example code shows how to begin a guided search and then add and delete search clauses based on the input from the customer. You can continue to add and delete clauses or you can reinitialize the search object.

public static void GuidedSearch(CatalogContext context)
{
    /*
            Get the desired catalog.
            Implement the code to display your catalog information 
            on your site and get the input from the user.
    */
    // The user has selected a category of items to browse.
    // Start the search by returning the items in this category.
    SpecificationSearch search = catalog.BeginSpecificationSearch(categoryName);
    search.AddSearchClause(String.Format("{0} = categoryName", CatalogItemsDataSetSchema.CategoryName));
    CatalogItemsDataSet items = search.Search();

    /*  
         Implement the code to display the returned items
         on your site and get input from the user.
   */
    // Add additional search clauses depending on the input from the user.
    // Search for items in this set that have a list price less than $100.
    search.AddSearchClause(String.Format("{0} < 100", CatalogItemsDataSetSchema.ListPrice));
    items = search.Search();
   
    // The customer wants to look at more expensive items.
    // Remove the last search clause.
    search.RemoveSearchClause();
    // Add another clause that specifies a higher price range and search again.
    search.AddSearchClause(String.Format("{0} < 250", CatalogItemsDataSetSchema.ListPrice));
    items = search.Search();
    // Continue to add and delete search clauses based on the customer input.
    // You can also reinitialize the search object.
    
}

See Also

Other Resources

Searching Catalogs by Using the Catalog API