検索候補

サジェスト検索を取得する

検索候補を取得するには、ContextualSearchRuntimegetSearchSuggestions メソッドを呼び出します。このメソッドは、検索候補の取得に使用するテキストを指定できる GetSearchSuggestionsOptions を 1 つ受け取ります。

返される SearchSuggestions オブジェクトには、アクセス可能なプロパティが含まれていません。検索候補のレンダリングに必要な情報はすべて含まれていますが、それ以外は不透明です。

Swift は Swift の同時実行スタイルを使用し、Objective-C はコールバック関数を使用します。

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

位置情報に基づく検索候補を取得する

getSearchSuggestions リクエストは、位置情報ベースの検索候補もサポートしています。LocationSuggestionsContext を使用して GetSearchSuggestionsOptions オブジェクトを作成します。

LocationSuggestionsContext クラスをビルドする手順は次のとおりです。

  1. (必須)latitudelongituderadiusMeters をリクエストされたエリアに合わせて設定します。
  2. (省略可)検索候補を適用するリクエストされた時間に合わせて startTime を設定します。
  3. (省略可)リクエストされた地域タイプを含む geoTypes リストを設定します。指定されたタイプは、サービスから返される結果のランキングとフィルタリングを調整するために使用されます。サポートされている値については、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.
                            }];

ディスプレイ

検索候補チップを表示するには、次の方法を使用します(スタイルは、UIKit と SwiftUI のどちらを使用しているかによって異なります)。

Swift UIKit

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

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

検索候補は SearchSuggestionsView で表示できます。

SearchSuggestionsView(suggestions: suggestions, options: options)

次へ: 検索結果を表示する