Arama önerileri

Önerilen aramaları alma

Arama önerileri almak için ContextualSearchRuntime üzerinde getSearchSuggestions yöntemini çağırın. Bu yöntem, arama önerileri almak için kullanılacak metni belirtebileceğiniz tek bir GetSearchSuggestionsOptions alır.

Döndürülen SearchSuggestions nesnesi, erişilebilir herhangi bir özellik içermiyor. Arama önerilerini oluşturmak için gerekli tüm bilgileri içerir ancak diğer açılardan opaktır.

Swift, Swift eşzamanlılık stilini, Objective-C ise geri çağırma işlevini kullanır.

Swift

getSearchSuggestions(withOptions:)

Task {
    let suggestions = try await searchRuntime.getSearchSuggestions(
            withOptions: GetSearchSuggestionsOptions(withTextContext:['Sample Query', 'Another query string']))
}

Objective-C

getSearchSuggestionsWithOptions

  [searchRuntime
      getSearchSuggestionsWithOptions:
            [[GetSearchSuggestionsOptions alloc] initWithTextContext: @[@'Sample Query', @'Another query string' ]]
                             completionHandler:^(SearchSuggestions *_Nullable suggestions,
                                                 NSError *_Nullable error) {
                              // error will be not null if there is an error.
                              // On success, suggestions will be non null.
                            }];

Konuma dayalı önerilen aramalar alma

getSearchSuggestions isteği, konuma dayalı arama önerilerini de destekler. LocationSuggestionsContext içeren bir GetSearchSuggestionsOptions nesnesi oluşturun.

LocationSuggestionsContext sınıfını oluşturmak için aşağıdaki adımları uygulayın:

  1. (Zorunlu) latitude, longitude ve radiusMeters değerlerini istenen alanla eşleşecek şekilde ayarlayın.
  2. (İsteğe bağlı) startTime öğesini, arama önerilerinin uygulanması gereken istenen saatle eşleşecek şekilde ayarlayın.
  3. (İsteğe bağlı) geoTypes listesini istenen coğrafi türlerle ayarlayın. Sağlanan türler, hizmet tarafından döndürülen sonuçların sıralamasını ve filtrelemesini ayarlamak için kullanılır. Desteklenen değerler LocationSuggestionsContext bölümünde açıklanmıştır.

Swift

getSearchSuggestions(withOptions:)

Task {
    let locationContext: LocationSuggestionsContext = {
      let context = LocationSuggestionsContext()
      context.latitude = 40.748595
      context.longitude = -73.992365
      context.radiusMeters = 2500
      context.startTime = try?
        Date.ISO8601FormatStyle.iso8601.parse("2024-03-14T19:20:30.000+02:00")
      context.geoTypes = ["restaurant", "cafe"]
      return context
    }()
    let suggestions = try await searchRuntime.getSearchSuggestions(
            withOptions: GetSearchSuggestionsOptions(locationContext: [
              locationContext
            ]))
}

Objective-C

getSearchSuggestionsWithOptions


  LocationSuggestionsContext *locationContext;
  locationContext = [[LocationSuggestionsContext alloc] init];
  locationContext.latitude = 40.748595;
  locationContext.longitude = -73.992365;
  locationContext.radiusMeters = @2500;
  [searchRuntime
      getSearchSuggestionsWithOptions:
            [[GetSearchSuggestionsOptions alloc] initWithLocationContext: locationContext]
                             completionHandler:^(SearchSuggestions *_Nullable suggestions,
                                                 NSError *_Nullable error) {
                              // error will be not null if there is an error.
                              // On success, suggestions will be non null.
                            }];

Görüntülü Reklam Ağı

Arama önerileri çiplerini görüntülemek için aşağıdaki yöntemleri kullanın (UIKit ve SwiftUI kullanıyorsanız stil değişiklik gösterir).

Swift UIKit

SearchSuggestionsViewController'ı kullanarak arama önerisi çiplerini gösterin.

// Create the SearchSuggestionsViewController with specified display options.
var searchSuggestionsController = SearchSuggestionsViewController(options: viewOptions)
...
// Add the view controller to the existing view hierarchy
searchSuggestionsController.addToViewContoller(self)
view.addSubview(searchSuggestionsController.view)
...
// Assign a specific set of search suggestions to this view controller.
searchSuggestionsController.searchSuggestions = suggestions

Objective-C

SearchSuggestionsViewController'ı kullanarak arama önerisi çiplerini gösterin.

// Create the SearchSuggestionsController with specified display options.
SearchSuggestionsViewController *_searchSuggestionsController =
      [[SearchSuggestionsViewController alloc] initWithOptions:_customizationOptions];
...
// Add the view controller to the existing view hierarchy
[_searchSuggestionsController addToViewController:self];
[self.view addSubview:_searchSuggestionsController.view];
...
// Assign a specific set of search suggestions to this view controller.
_searchSuggestionsController.searchSuggestions = suggestions

SwiftUI

Arama önerileri, SearchSuggestionsView ile gösterilebilir.

SearchSuggestionsView(suggestions: suggestions, options: options)

Sonraki: Arama sonuçlarını görüntüleme