Saran penelusuran

Mendapatkan Penelusuran yang Disarankan

Untuk mendapatkan saran penelusuran, panggil metode getSearchSuggestions di ContextualSearchRuntime. Metode ini menggunakan satu GetSearchSuggestionsOptions tempat Anda dapat menentukan teks yang akan digunakan untuk mendapatkan saran penelusuran.

Objek SearchSuggestions yang ditampilkan tidak berisi properti yang dapat diakses. File ini berisi semua informasi yang diperlukan untuk merender sugesti penelusuran, tetapi tidak transparan.

Swift menggunakan gaya Swift Concurrency, Objective-C menggunakan fungsi callback.

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.
                            }];

Mendapatkan Penelusuran yang Disarankan Berbasis Lokasi

Permintaan getSearchSuggestions juga mendukung saran penelusuran berbasis lokasi. Buat objek GetSearchSuggestionsOptions dengan LocationSuggestionsContext.

Ikuti langkah-langkah berikut untuk membangun class LocationSuggestionsContext:

  1. (Wajib) Tetapkan latitude, longitude, dan radiusMeters agar cocok dengan area yang diminta.
  2. (Opsional) Setel startTime agar cocok dengan waktu yang diminta saat saran penelusuran harus diterapkan.
  3. (Opsional) Tetapkan daftar geoTypes dengan jenis geografis yang diminta. Jenis yang disediakan digunakan untuk menyesuaikan peringkat dan pemfilteran hasil yang ditampilkan oleh layanan. Nilai yang didukung didokumentasikan dalam LocationSuggestionsContext.

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.
                            }];

Display

Untuk menampilkan chip saran penelusuran, gunakan metode berikut (gayanya bervariasi jika Anda menggunakan UIKit versus SwiftUI).

Swift UIKit

Menampilkan chip saran penelusuran menggunakan SearchSuggestionsViewController.

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

Menampilkan chip saran penelusuran menggunakan SearchSuggestionsViewController.

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

Saran penelusuran dapat ditampilkan dengan SearchSuggestionsView

SearchSuggestionsView(suggestions: suggestions, options: options)

Berikutnya: Menampilkan hasil penelusuran