추천 검색어 가져오기
검색 추천을 받으려면 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
요청은 위치 기반 검색 추천도 지원합니다. LocationSuggestionsContext를 사용하여 GetSearchSuggestionsOptions
객체를 빌드합니다.
LocationSuggestionsContext
클래스를 빌드하려면 다음 단계를 따르세요.
- (필수) 요청된 영역과 일치하도록
latitude
,longitude
,radiusMeters
를 설정합니다. - (선택사항) 검색 추천이 적용되어야 하는 요청된 시간과 일치하도록
startTime
를 설정합니다. - (선택사항) 요청된 지역 유형으로
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)