검색 제안

추천 검색어 가져오기

추천 검색어를 가져오려면 ContextualSearchRuntime에서 getSearchSuggestions 메서드를 호출합니다. 이 메서드는 추천 검색어를 가져오는 데 사용할 텍스트를 지정할 수 있는 단일 GetSearchSuggestionsOptions 를 사용합니다.

반환된 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 요청은 위치 기반 추천 검색어도 지원합니다. GetSearchSuggestionsOptions 객체를 LocationSuggestionsContext로 빌드합니다.

다음 단계에 따라 LocationSuggestionsContext 클래스를 빌드합니다.

  1. (필수) 요청된 영역과 일치하도록 latitude, longitude, radiusMeters를 설정합니다.
  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를 사용하여 추천 검색어 칩을 표시합니다. 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를 사용하여 추천 검색어 칩을 표시합니다. 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)

다음: 검색 결과 표시