Share via


Using Search Filters

 

This documentation is no longer available on MSDN. It is available as a CHM or PDF download. For the newest Geocoding, Imagery, Routing and Traffic services, see Bing Maps REST Services. 

When making requests to the Bing Maps Search Service, you can refine your searches using search filters. Search filters allow you to limit your results to those that meet certain criteria.

With search filters, you can write complex searches such as:

  • Search for any businesses that match the keyword “cafe” with a rating of 4/10 or greater in New York City.

  • Search for all Polynesian restaurants in Honolulu with a romantic atmosphere.

  • Search for all businesses categorized as Malls & Shopping Centers, Museums or Movie Theaters in Chicago.

Note

Filters are only supported for English-United States business searches.

Search Filters

Supported search filters are listed below. Additionally, a table of these filter properties and their associated property values are found in the Search Filter Property Table topic.

General Filters

The following filters are applicable to all business listings:

  • Category

  • Neighborhood

  • User Rating

  • Payment Method

  • Parking Options

Category-specific Filters

Additionally, certain business listing categories have additional properties that can be used for filtering:

  • Restaurants

    • Price

    • Cuisine

    • Atmosphere

    • Reservation

  • Hotels

    • Rate

    • Amenities

Search filters are applied to a search by setting the SearchOptions.Filters Property to one of the types derived from the FilterExpressionBase Class.

The derived types are:

Applying a Single Filter

The following example demonstrates a search with a single filter. The code below searches for all business listings for the query "cafe in new york" that have a user-rating greater than or equal to 4.

public void SingleFilterExample()
{
   // 1) Create the SearchRequest

   // 1a) Set the required SearchRequest parameters
   SearchService.SearchRequest searchRequest = new SearchService.SearchRequest();
   searchRequest.Culture = "en-US";
   searchRequest.Query = "cafe in new york";

   //Identify your Bing Maps Key 
   key = “Bing Maps Key”;
   searchRequest.Credentials = new SearchService.Credentials();
   // A Bing Maps Key is required for all calls to Bing Maps SOAP Services
   searchRequest.Credentials.ApplicationId = key; 
   // 1b) Set the User Category Filter
   SearchService.FilterExpression userRatingFilterExpression = new SearchService.FilterExpression();
   userRatingFilterExpression.PropertyId = 3;  // User Rating Property ID is 3
   userRatingFilterExpression.FilterValue = 4; // User rating scale is between 0 and 10
   userRatingFilterExpression.CompareOperator = SearchService.CompareOperator.GreaterThanOrEquals;

   // Filters are specified by setting the SearchOptions.Filters property
   searchRequest.SearchOptions = new SearchService.SearchOptions(); 
   searchRequest.SearchOptions.Filters = userRatingFilterExpression;

   // 2) Execute the search by calling the Bing Maps Search Service
   SearchService.SearchServiceClient client = null;
   try
   {
      // 2a) Create the service client proxy and open the connection.
      client = new SearchService.SearchServiceClient("BasicHttpBinding_ISearchService");
      client.Open();

      // 2b) Call Search()
      SearchService.SearchResponse response = client.Search(searchRequest);

      // 2c) Print out the matching results.
      if (response.ResultSets != null && response.ResultSets.Length > 0)
      {
         Console.WriteLine("Cafes in NYC with rating of 4.0 or greater:");
         foreach (SearchService.SearchResultBase searchResult in response.ResultSets[0].Results)
         {
            Console.WriteLine(searchResult.Name);
         }
      }
   }
   catch (Exception ex)
   {
      // If an exception has occurred for any reason, 
      Console.WriteLine("An exception occurred :{0}", ex.ToString());
      throw;
   }
   finally
   {
      // 2d) Close service client proxy.
      if (client != null)
      {
         client.Close();
      }
   }
}

Applying Multiple Filters

The following example demonstrates a search with multiple filters. The code below searches for all business listings for the query "restaurants in honolulu" that serve Polynesian food and have a romantic atmosphere.

public void MultipleFiltersAndExample()
{
   // 1) Create the SearchRequest

   // 1a) Set the required SearchRequest parameters
   SearchService.SearchRequest searchRequest = new  SearchService.SearchRequest();
   searchRequest.Culture = "en-US";
   searchRequest.Query = "restaurants in honolulu";

   //Identify your Bing Maps Key
   string key = "Bing Maps Key";
   searchRequest.Credentials = new SearchService.Credentials();
   // A Bing Maps Key is required for all calls to the Bing Maps SOAP Services 
   searchRequest.Credentials.ApplicationId = key; 

   // 1b) Set the filters

   // Cuisine Property Filter
   SearchService.FilterExpression cuisineFilterExpression = new  SearchService.FilterExpression();
   cuisineFilterExpression.PropertyId = 22;  // Cuisine Property ID is 3
   cuisineFilterExpression.FilterValue = 62; // 62 = Polynesian
   cuisineFilterExpression.CompareOperator =  SearchService.CompareOperator.Equals;

   // Atmosphere Property Filter
   SearchService.FilterExpression atmosphereFilterExpression = new  SearchService.FilterExpression();
   atmosphereFilterExpression.PropertyId = 23; // Atmosphere Property ID is 23
   atmosphereFilterExpression.FilterValue = 10; // 10 = Romantic
   atmosphereFilterExpression.CompareOperator =  SearchService.CompareOperator.Equals;

   // Combine the Cuisine and Atmosphere property filters
   SearchService.FilterExpressionClause combinedFilterExpressionClause = new SearchService.FilterExpressionClause();
   combinedFilterExpressionClause.Expressions = 
      new SearchService.FilterExpressionBase[]
      {
         cuisineFilterExpression,
         atmosphereFilterExpression
      };
   combinedFilterExpressionClause.LogicalOperator = SearchService.LogicalOperator.And;


   // Filters are specified by setting the SearchOptions.Filters property
   searchRequest.SearchOptions = new SearchService.SearchOptions(); 
   searchRequest.SearchOptions.Filters = combinedFilterExpressionClause;

   // 2) Execute the search by calling the Bing Maps Search Service
   SearchService.SearchServiceClient client = null;
   try
   {
      // 2a) Create the service client proxy and open the connection.
      client = new SearchService.SearchServiceClient("BasicHttpBinding_ISearchService");
      client.Open();

      // 2b) Call Search()
      SearchService.SearchResponse response = client.Search(searchRequest);

      // 2c) Print out the matching results.
      if (response.ResultSets != null && response.ResultSets.Length > 0)
      {
         Console.WriteLine("Restaurants in Honolulu that serve Pacific Rim food and are also on the waterfront:");
         foreach (SearchService.SearchResultBase searchResult in response.ResultSets[0].Results)
         {
            Console.WriteLine(searchResult.Name);
         }
      }
   }
   catch (Exception ex)
   {
      // If an exception has occurred for any reason, 
      Console.WriteLine("An exception occurred :{0}", ex.ToString());
      throw;
   }
   finally
   {
      // 2d) Close service client proxy.
      if (client != null)
      {
         client.Close();
      }
   }
}

Applying a Category Filter

The following example demonstrates a search with a category filter. The code below searches for all business listings in Chicago that are categorized as Malls & Shopping Centers, Museums or Movie Theaters.

public void MultipleCategoryFiltersOrExample()
        {
            // 1) Create the SearchRequest

            // 1a) Set the required SearchRequest parameters
            SearchService.SearchRequest searchRequest = new SearchService.SearchRequest();
            searchRequest.Culture = "en-US";
            searchRequest.StructuredQuery = new SearchService.StructuredSearchQuery();
            searchRequest.StructuredQuery.Location = "chicago";

            string key = "Bing Maps Key";
            searchRequest.Credentials = new SearchService.Credentials();
            // A Bing Maps Key is required for all calls to the Bing Maps SOAP Services 
            searchRequest.Credentials.ApplicationId = key; 

            // 1b) Set the filters

            // Malls and Shopping Centers 
            SearchService.FilterExpression mallsAndShoppingCentersFilterExpression = new SearchService.FilterExpression();
            mallsAndShoppingCentersFilterExpression.PropertyId = 1;  // Category Property ID is 1
            mallsAndShoppingCentersFilterExpression.FilterValue = 1614; // 62 = Malls and Shopping Centers
            mallsAndShoppingCentersFilterExpression.CompareOperator = SearchService.CompareOperator.Equals;

            // Movie Theaters
            SearchService.FilterExpression movieTheatersFilterExpression = new SearchService.FilterExpression();
            movieTheatersFilterExpression.PropertyId = 1; // Category Property ID is 1
            movieTheatersFilterExpression.FilterValue = 1777; // 1777 = Movie Theaters
            movieTheatersFilterExpression.CompareOperator = SearchService.CompareOperator.Equals;

            // Museums
            SearchService.FilterExpression museumsFilterExpression = new SearchService.FilterExpression();
            museumsFilterExpression.PropertyId = 1; // Category Property ID is 1
            museumsFilterExpression.FilterValue = 1785; // 1785 = Museums
            museumsFilterExpression.CompareOperator = SearchService.CompareOperator.Equals;

            // Combine all of these filters into a single "OR" expression
            SearchService.FilterExpressionClause combinedFilterExpressionClause = new SearchService.FilterExpressionClause();
            combinedFilterExpressionClause.Expressions =
               new SearchService.FilterExpressionBase[]
      {
         mallsAndShoppingCentersFilterExpression,
         movieTheatersFilterExpression,
         museumsFilterExpression
      };
            combinedFilterExpressionClause.LogicalOperator = SearchService.LogicalOperator.Or;

            // Filters are specified by setting the SearchOptions.Filters property
            searchRequest.SearchOptions = new SearchService.SearchOptions();
            searchRequest.SearchOptions.Filters = combinedFilterExpressionClause;
            searchRequest.SearchOptions.ListingType = SearchService.ListingType.Business; // Filters only work for businesses

            // 2) Execute the search by calling the Bing Maps Search Service
            SearchService.SearchServiceClient client = null;
            try
            {
                // 2a) Create the service client proxy and open the connection.
                client = new SearchService.SearchServiceClient("BasicHttpBinding_ISearchService");
                client.Open();

                // 2b) Call Search()
                SearchService.SearchResponse response = client.Search(searchRequest);

                // 2c) Print out the matching results.
                if (response.ResultSets != null && response.ResultSets.Length > 0)
                {
                    Console.WriteLine("Malls & Shopping Centers, Museums and Movie Theaters in Chicago:");
                    foreach (SearchService.SearchResultBase searchResult in response.ResultSets[0].Results)
                    {
                        Console.WriteLine(searchResult.Name);
                    }
                }
            }
            catch (Exception ex)
            {
                // If an exception has occurred for any reason, 
                Console.WriteLine("An exception occurred :{0}", ex.ToString());
                throw;
            }
            finally
            {
                // 2d) Close service client proxy.
                if (client != null)
                {
                    client.Close();
                }
            }
        }

Relevant Search Filters

The SearchResultSet.AvailableFilters Property can be used to further refine the search by providing relevant filters. This property contains any filters that are considered relevant to a business search that returns one or more results. By default, the Bing Maps Search Service returns filters that are applicable to all business searches. Additionally, the Bing Maps Search Service may recommend additional category-specific properties for filtering if it determines that they are relevant to the search. This is useful when developing applications that integrate interactive search. Users of your application can use these filters to narrow down a set of results to those most relevant to them.

See Also

SearchServiceClient.Search Method