다음을 통해 공유


How to Perform a Free-Text Search

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

You can use the free-text search to search for words and phrases in the values of searchable properties. For example, you can search for all products that have the text "red strap" in their descriptions. When you add a property definition to your catalog, you can specify whether it is available for free-text search.

The free-text search retrieves all instances of the property in your catalog. You must update the full text index after making changes to catalogs that are marked as searchable. For more information about updating the full text index, see What Is a Full-Text Catalog?

You use the CatalogSearch object to create a free-text search. You can search across multiple catalogs, within a single catalog, or within a category in a catalog. The properties of the CatalogSearch object are used to control the search, including the search domain, the type of search, language, sorting, and paging.

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

You can combine a free-text search with an SQL WHERE clause search. For information about the SQL WHERE clause search, see How to Perform a Search by Using the SQL WHERE Clause.

Note

The ProductId property is a built-in property and cannot be used in a free-text search. To perform a free-text search using the ProductId property, create a custom property of the String data type and give it the value of the ProductId property. Make this custom property free-text searchable. Your search will work on the contents of this property.

Note

Because of the WHERE clause limit of 4000 characters in Microsoft SQL Server 2000, the SQL WHERE clause in the Commerce Server Catalog and Inventory System is also limited. This causes a limit of approximately 1400 characters for a basic catalog search when inventory query data is added to the WHERE clause text. This WHERE clause limit does not exist in Microsoft SQL Server 2005.

  1. Use the GetCatalogSearch method on the CatalogContext object to get a CatalogSearch object.

  2. Use the FreeTextSearchPhrase property on the CatalogSearch object to specify the search text.

  3. Use the CatalogSearchOptions object to specify the search options.

  4. Use one of the Search methods on the CatalogSearch object to perform the search.

  5. Iterate through the returned dataset to access the individual catalog items.

  1. Use the GetCatalogSearch method on the CatalogContext object to get a CatalogSearch object.

  2. Use the FreeTextSearchPhrase property on the CatalogSearch object to specify the search text.

  3. Set the UseAdvancedFreeTextSearch property on the CatalogSearch object to true.

  4. Use the SqlWhereClause property on the CatalogSearch object to specify the search parameters.

  5. Use the CatalogSearchOptions object to specify the search options.

  6. Use one of the Search methods on the CatalogSearch object to perform the search.

  7. Iterate through the returned dataset to access the individual catalog items.

Example

This example describes how to search a catalog. The example uses both the free-text search and the SQL WHERE clause search. The example first creates the search options. It then gets a CatalogSearch object to perform the search. The example uses the CategoriesClause property to indicate that all categories in the specified catalogs that have "technical" as part of their descriptions should be searched.

The example uses the SQL WHERE clause to search for items in the catalog that have a price less than $10.00. It sets the UseAdvancedFreeTextSearch property to true to include a free text search. The search string is "sql books". The search returns all items less than $10.00 that have the string "sql books" as part of a property value.

public static CatalogItemsDataSet SearchCatalog(CatalogContext context, string catalogName)
{
    // Search the catalog.
    // Create the search options.
    CatalogSearchOptions searchOptions = new CatalogSearchOptions();
    searchOptions.PropertiesToReturn = "CategoryName, DefinitionName, i_classtype, ProductId, VariantId, DisplayName";
    searchOptions.SetPaging(1, 20);
    searchOptions.SortProperty = "CategoryName";
    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 = catalogName;

    // 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.
    catalogSearch.CateoriesClause = @"Description like '%technical%'";

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

    // Also use the free text search.
    catalogSearch.UseAdvancedFreeTextSearch = true;
    // Return all rows that contain the phrase "sql books". 
    catalogSearch.AdvancedFreeTextSearchPhrase = "\"sql books\""; 
    catalogSearch.SearchOptions = searchOptions;
  
    // Perform the search.    
    int totalRecords = 0;
    CatalogItemsDataSet catalogItems = catalogSearch.Search(out totalRecords);
    Console.WriteLine(totalRecords);

    // Access each of the returned items.
    foreach (CatalogItemsDataSet.CatalogItem catalogItem in catalogItems.CatalogItems)
    {
        Console.WriteLine(catalogItem.CategoryName);
        Console.WriteLine(catalogItem.DisplayName);
    }
    return catalogItems;
}

See Also

Other Resources

Searching Catalogs by Using the Catalog API