Nhận 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 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. Nội dung này chứa tất cả thông tin cần thiết để hiển thị đề xuất tìm kiếm, nhưng nếu không thì sẽ không rõ ràng.
Swift sử dụng kiểu đồng thời Swift, còn 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ụ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 một đối tượng GetSearchSuggestionsOptions bằng LocationSuggestionsContext.
Làm theo các bước sau để tạo lớp LocationSuggestionsContext:
- (Bắt buộc) Đặt
latitude,longitudevàradiusMeterssao cho khớp với khu vực được yêu cầu. - (Không bắt buộc) Đặt
startTimecho phù hợp với thời gian được yêu cầu mà các đề xuất tìm kiếm sẽ áp dụng. - (Không bắt buộc) Đặt danh sách
geoTypesbằng các loại dữ liệu địa lý được yêu cầu. Các loại được cung cấp được dùng để điều chỉnh việc xếp hạng và lọc kết quả do 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ị các chip đề xuất tìm kiếm, hãy sử dụng các phương thức sau (kiểu này sẽ khác nếu bạn đang dùng UIKit so với SwiftUI).
Swift UIKit
Hiển thị các khối tìm kiếm được đề xuất bằ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ị các khối tìm kiếm được đề xuất bằ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
Các cụm từ tìm kiếm đề xuất có thể xuất hiện cùng với SearchSuggestionsView
SearchSuggestionsView(suggestions: suggestions, options: options)