Arama önerileri

Önerilen aramaları alma

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

Döndürülen SearchSuggestions nesnesi, erişilebilir özellikler içermiyor. Arama önerilerini oluşturmak için gerekli tüm bilgileri içerir ancak aksi takdirde opak olur.

Swift, Swift Concurrency 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 ile 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 değerini, arama önerilerinin uygulanması istenen zamana göre ayarlayın.
  3. (İsteğe bağlı) İstenen coğrafi türlerle geoTypes listesini ayarlayın. Sağlanan türler, hizmet tarafından döndürülen sonuçların sıralamasını ve filtrelenmesini ayarlamak için kullanılır. Desteklenen değerler, LocationSuggestionsContext'te belgelenmiştir.

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 önerisi çipleri göstermek için aşağıdaki yöntemleri kullanın (UIKit ve SwiftUI kullanmanıza bağlı olarak stil değişir).

Swift UIKit

SearchSuggestionsViewController'ı kullanarak arama önerisi çiplerini görüntüleyin.

// 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örüntüleyin.

// 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