Nhận các cụm từ tìm kiếm đề xuất
Để nhận đề xuất tìm kiếm, hãy gọi phương thức getSearchSuggestions
trên ContextualSearchRuntime
. Phương thức này lấy một GetSearchSuggestionsOptions duy nhất, trong đó bạn có thể chỉ định văn bản cần sử dụng để nhận đề xuất tìm kiếm.
Đối tượng SearchSuggestions
được trả về không chứa bất kỳ thuộc tính nào có thể truy cập được. Tệp này chứa tất cả thông tin cần thiết để hiển thị các đề xuất tìm kiếm, nhưng không rõ ràng.
Swift sử dụng kiểu Swift Concurrency, Objective-C sử dụng hàm 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.
}];
Nhận các cụm từ tìm kiếm đề xuất dựa trên vị trí
Yêu cầu getSearchSuggestions
cũng hỗ trợ các đề xuất tìm kiếm dựa trên vị trí. Tạo đối tượng GetSearchSuggestionsOptions
bằng LocationSuggestionsContext.
Hãy làm theo các bước sau để tạo lớp LocationSuggestionsContext
:
- (Bắt buộc) Đặt
latitude
,longitude
vàradiusMeters
để khớp với khu vực được yêu cầu. - (Không bắt buộc) Đặt
startTime
khớp với thời gian được yêu cầu mà nội dung đề xuất tìm kiếm sẽ áp dụng. - (Không bắt buộc) Đặt danh sách
geoTypes
bằng các loại địa lý được yêu cầu. Các loại đã cung cấp dùng để điều chỉnh thứ hạng và lọc kết quả mà dịch vụ trả về. Các giá trị được hỗ trợ được ghi lại trong 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.
}];
Màn hình
Để hiển thị khối đề xuất tìm kiếm, hãy sử dụng các phương thức sau (kiểu sẽ khác nhau nếu bạn đang sử dụng UIKit so với SwiftUI).
Swift UIKit
Hiển thị khối đề xuất tìm kiếm bằng cách sử dụng 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
Hiển thị khối đề xuất tìm kiếm bằng cách sử dụng 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
Bạn có thể hiển thị các cụm từ tìm kiếm được đề xuất bằng SearchSuggestionsView
SearchSuggestionsView(suggestions: suggestions, options: options)